php发起http请求

    public function request($url,$method = "get",$data = []){
        $content = $data;
        $header = "Content-type:application/json";
        if(is_array($data)){
            $content = http_build_query($data);
            $header = "Content-type:application/x-www-form-urlencoded";
        }

        $opts = [
            "http" => [  //这个位置不能填https
                'method' => $method,  //请求方法
                'header' => $header,  //header头信息
                "content" => $content  //数据
            ]
        ];

        $context = stream_context_create($opts); //构建资源流上下文

        $result = file_get_contents($url,false,$context); //把文件读入一个字符串

        return $result;
    }

猜你喜欢

转载自www.cnblogs.com/jint-php7/p/12917474.html