PHP Curl Request HTTPS interface

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.