When you request HTTP, you only need to
File_get_contents ("http://www.sojson.com/open/api/weather/json.shtml?city= $Position");
Yes, but it turns out this interface is now an HTTPS protocol.
And this way, it's going to be 403.
First Look at PHP there is no curl extension, I am 7.2
I'm using the Laravel community encapsulation method.
Public Static functionCurl$url,$params=false,$ispost= 0,$https= 0) { $httpInfo=Array(); $ch=Curl_init (); curl_setopt ($ch, Curlopt_http_version,curl_http_version_1_1); curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/41.0.2272.118 safari/537.36 '); curl_setopt ($ch, Curlopt_connecttimeout, 30); curl_setopt ($ch, Curlopt_timeout, 30); curl_setopt ($ch, Curlopt_returntransfer,true); if($https) {curl_setopt ($ch, Curlopt_ssl_verifypeer,FALSE);//examination of the source of the certification certificatecurl_setopt ($ch, Curlopt_ssl_verifyhost,FALSE);//Check that the SSL encryption algorithm exists from the certificate } if($ispost) {curl_setopt ($ch, Curlopt_post,true); curl_setopt ($ch, Curlopt_postfields,$params); curl_setopt ($ch, Curlopt_url,$url); } Else { if($params) { if(Is_array($params)) { $params=Http_build_query($params); }
curl_setopt ($ch, Curlopt_url,$url. ‘?‘ .$params); Here is a list of parameters, add you a? } Else{curl_setopt ($ch, Curlopt_url,$url); } } $response= Curl_exec ($ch); if($response===FALSE) { //echo "CURL Error:". Curl_error ($ch); return false; } $httpCode= Curl_getinfo ($ch,Curlinfo_http_code); $httpInfo=Array_merge($httpInfo, Curl_getinfo ($ch)); Curl_close ($ch); return $response; }
Send Request
$result = self ::Curl(' url ', ' parameter ', true);
The data received needs to be transformed
$json = json_decode($result);
My usage is that the interface to the weather forecast should be called for me
$result = Curl::curl ($url, "city= Beijing");
PHP Curl Request HTTPS interface