PHP通过CURL或file_get_contents请求第三方地址

CURL:

//访问网页数据
    public
    function get_web_content($url, $curl_data)
    {
        $options = array(
            CURLOPT_RETURNTRANSFER => true,         // return web page
            CURLOPT_HEADER         => false,        // don't return headers
            CURLOPT_FOLLOWLOCATION => true,         // follow redirects
            CURLOPT_ENCODING       => "",           // handle all encodings
            CURLOPT_USERAGENT      => "institution",     // who am i
            CURLOPT_AUTOREFERER    => true,         // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,          // timeout on connect
            CURLOPT_TIMEOUT        => 120,          // timeout on response
            CURLOPT_MAXREDIRS      => 10,           // stop after 10 redirects
            CURLOPT_POST           => 1,            // i am sending post data
            CURLOPT_POSTFIELDS     => $curl_data,    // this are my post vars
            CURLOPT_SSL_VERIFYHOST => 0,            // don't verify ssl
            CURLOPT_SSL_VERIFYPEER => false,        //
            CURLOPT_VERBOSE        => 1                //
        );

        $ch = curl_init($url);
        curl_setopt_array($ch, $options);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;

    }

file_get_contents(get方式,拼接参数)

// 如:QQ校验成功,获取QQ用户信息
$res = file_get_contents("https://graph.qq.com/user/get_user_info?access_token=" . $access_token . "&oauth_consumer_key=" . $app_id . "&openid=" . $openid);
$res = json_decode($res, true); // 解码json并转数组
发布了40 篇原创文章 · 获赞 14 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_41912505/article/details/104748633