[PHP Automation-Advanced]003.CURL processing HTTPS request access

Source: Internet
Author: User
Tags ssl certificate

Introduction: Following the previous "Analog login and acquisition of data", "Analog login with verification code site", we have a basic understanding of curl, this simple talk about the request HTTPS.

In many sites, such as TalkingData, Baidu and so on, some of the request agreement is to go SSL, plain English is HTTPS, this protocol in the simulation of curl may occur when the request is unsuccessful, and so on.

Let's talk about the solution.

This talk is simple, but it can explain the problem ....

Exception prompt

Most of the exception information is prompted as follows: certificate validation failed.

SSL certificate problem, verify the CA cert is OK. Details:error:14090086:ssl routines:SSL3_GET_SERVER_CERTIFICATE:certificate Verify failed

Cut into the topic

workaround , when curl requests, add

// Skip Certificate Check   curl_setopt ($chfalse); // Check that the SSL encryption algorithm exists  from the certificate curl_setopt ($chtrue);  

Curl HTTPS POST request encapsulates code code

<?PHP/** Curl gets HTTPS request * @param String $url The requested url* @param array $data the number to be sent @param array $header request Sent header* @param int $timeout time-out, default 30s*/functionCurl_https ($url,$data=Array(),$header=Array(),$timeout=30){    $ch=Curl_init (); curl_setopt ($ch, Curlopt_ssl_verifypeer,false);//Skip Certificate Checkcurl_setopt ($ch, Curlopt_ssl_verifyhost,true);//Check that the SSL encryption algorithm exists from the certificatecurl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_httpheader,$header); curl_setopt ($ch, Curlopt_post,true); curl_setopt ($ch, Curlopt_postfields,Http_build_query($data)); curl_setopt ($ch, Curlopt_returntransfer,true); curl_setopt ($ch, Curlopt_timeout,$timeout); $response= Curl_exec ($ch); if($error=curl_error ($ch)){         die($error); } curl_close ($ch); return $response;}//called$url= ' https://www.example.com/api/message.php ';$data=Array(' name ' = ' Fdipzone ');$header=Array();$response= Curl_https ($url,$data,$header, 5);Echo $response;?>

To be continued .....

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4792496.html

[PHP Automation-Advanced]003.CURL processing HTTPS request access

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.