php通过CURL模拟get提交请求
方式一:
1 $host = "http://jisunews.market.alicloudapi.com"; 2 $path = "/news/get"; 3 $method = "GET"; 4 $appcode = "0c4e883bc09f4d0e926136ecb65fcafa"; 5 $headers = array(); 6 array_push($headers, "Authorization:APPCODE " . $appcode); 7 $querys = "channel=".$channel."&num=40&start=0"; 8 $bodys = ""; 9 $url = $host . $path . "?" . $querys; 10 11 $ch = curl_init(); 12 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); 13 curl_setopt($ch, CURLOPT_URL, $url); 14 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 15 curl_setopt($ch, CURLOPT_FAILONERROR, false); 16 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 17 // curl_setopt($curl, CURLOPT_HEADER, true); 18 if (1 == strpos("$".$host, "https://")) 19 { 20 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 21 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 22 } 23 $data = curl_exec($ch); 24 curl_close($ch);
方式二:
1 $urlCity = urlencode($cityName); 2 $url = 'restapi.amap.com/v3/config/district?keywords='.$urlCity.'&subdistrict=0&key=ebf76716220ae651fd505d8eb9812c37'; 3 //初始化一个 cURL 对象 4 $ch = curl_init(); 5 //设置你需要抓取的URL 6 curl_setopt($ch, CURLOPT_URL, $url); 7 // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。 8 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 9 //是否获得跳转后的页面 10 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 11 $data = curl_exec($ch); 12 curl_close($ch);