PHP通过CURL或file_get_contents请求第三方地址
生活随笔
收集整理的這篇文章主要介紹了
PHP通过CURL或file_get_contents请求第三方地址
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
CURL:
//post訪問網頁數據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訪問網頁數據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"); //設置請求方式// curl_setopt($ch, CURLOPT_POSTFIELDS, $params);//設置提交的字符串curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //設置頭信息curl_setopt($ch, CURLOPT_URL, $url); // 要訪問的地址curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //獲取的信息以文件流的形式返回curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不進行ssl驗證curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自動設置Referer//設置超時時間為1秒,超過1秒則關閉連接//curl_setopt($ch,CURLOPT_TIMEOUT,1);//curl_setopt($ch, CURLOPT_NOSIGNAL, 1); //注意,毫秒超時一定要設置這個//curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); //超時毫秒,cURL 7.16.2中被加入。從PHP 5.2.3起可使用curl_setopt($ch, CURLOPT_HEADER, 0); // 設置是否顯示返回頭信息 1返回 0不返回curl_setopt($ch, CURLOPT_NOBODY, 0); //不想在輸出中包含body部分,設置這個選項為一個非零值$result = curl_exec($ch);curl_close($ch);return array($result);}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并轉數組?
總結
以上是生活随笔為你收集整理的PHP通过CURL或file_get_contents请求第三方地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 推进落实国家重要能源和战略资源基地建设的
- 下一篇: PHP网页定时器和跳转页面