PHP通过CURL或file_get_contents请求第三方地址
生活随笔
收集整理的這篇文章主要介紹了
PHP通过CURL或file_get_contents请求第三方地址
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
CURL:
//post訪問(wèn)網(wǎng)頁(yè)數(shù)據(jù)publicfunction get_web_content($url, $curl_data){$options = array(CURLOPT_RETURNTRANSFER => true, // return web pageCURLOPT_HEADER => false, // don't return headersCURLOPT_FOLLOWLOCATION => true, // follow redirectsCURLOPT_ENCODING => "", // handle all encodingsCURLOPT_USERAGENT => "institution", // who am iCURLOPT_AUTOREFERER => true, // set referer on redirectCURLOPT_CONNECTTIMEOUT => 120, // timeout on connectCURLOPT_TIMEOUT => 120, // timeout on responseCURLOPT_MAXREDIRS => 10, // stop after 10 redirectsCURLOPT_POST => 1, // i am sending post dataCURLOPT_POSTFIELDS => $curl_data, // this are my post varsCURLOPT_SSL_VERIFYHOST => 0, // don't verify sslCURLOPT_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;}// get訪問(wèn)網(wǎng)頁(yè)數(shù)據(jù)public function _curl($url){$ch = curl_init();$headers = array("Content-type: application/json;charset='utf-8'","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); //設(shè)置請(qǐng)求方式// curl_setopt($ch, CURLOPT_POSTFIELDS, $params);//設(shè)置提交的字符串curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //設(shè)置頭信息curl_setopt($ch, CURLOPT_URL, $url); // 要訪問(wèn)的地址curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //獲取的信息以文件流的形式返回curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不進(jìn)行ssl驗(yàn)證curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自動(dòng)設(shè)置Referer//設(shè)置超時(shí)時(shí)間為1秒,超過(guò)1秒則關(guān)閉連接//curl_setopt($ch,CURLOPT_TIMEOUT,1);//curl_setopt($ch, CURLOPT_NOSIGNAL, 1); //注意,毫秒超時(shí)一定要設(shè)置這個(gè)//curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); //超時(shí)毫秒,cURL 7.16.2中被加入。從PHP 5.2.3起可使用curl_setopt($ch, CURLOPT_HEADER, 0); // 設(shè)置是否顯示返回頭信息 1返回 0不返回curl_setopt($ch, CURLOPT_NOBODY, 0); //不想在輸出中包含body部分,設(shè)置這個(gè)選項(xiàng)為一個(gè)非零值$result = curl_exec($ch);curl_close($ch);return array($result);}file_get_contents(get方式,拼接參數(shù))
// 如:QQ校驗(yàn)成功,獲取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并轉(zhuǎn)數(shù)組?
總結(jié)
以上是生活随笔為你收集整理的PHP通过CURL或file_get_contents请求第三方地址的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 推进落实国家重要能源和战略资源基地建设的
- 下一篇: 中国有几艘航母,叫什么名字?