PHP How to get HTTPS requests using Curl _php tutorial

Source: Internet
Author: User
Tags ssl certificate

How PHP uses curl to get HTTPS requests


This article mainly introduces PHP using curl to obtain HTTPS request method, involving curl for HTTPS request operation skills, very practical value, the need for friends can refer to the next

This example describes how PHP uses curl to get HTTPS requests. Share to everyone for your reference. The specific analysis is as follows:

Doing a project today requires curl to get the third-party API, the other side's API is HTTPS mode.
Before using curl to get an HTTP request, but today gets an HTTPS request, the following error message appears: Certificate validation failed.

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

Workaround for the Curl request, join:

The code is as follows:

curl_setopt ($ch, Curlopt_ssl_verifypeer, false); Skip Certificate Check
curl_setopt ($ch, Curlopt_ssl_verifyhost, true); Check that the SSL encryption algorithm exists from the certificate

Curl HTTPS Request Code

The code is as follows:

/** Curl Get HTTPS request
* @param String $url the requested URL
* @param Array $data the numbers to send
* @param the header sent when the Array $header request
* @param int $timeout time-out, default 30s
*/
function Curl_https ($url, $data =array (), $header =array (), $timeout =30) {
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_ssl_verifypeer, false); Skip Certificate Check
curl_setopt ($ch, Curlopt_ssl_verifyhost, true); Check that the SSL encryption algorithm exists from the certificate
curl_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;

}

Call
$url = ' https://www.example.com/api/message.php ';
$data = Array (' name ' = ' Fdipzone ');
$header = Array ();

$response = Curl_https ($url, $data, $header, 5);

Echo $response;
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/956981.html www.bkjia.com true http://www.bkjia.com/PHPjc/956981.html techarticle how PHP uses curl to get HTTPS requests this article mainly introduces the method that PHP uses curl to get HTTPS request, it involves the operation skill of curl for HTTPS request, it is very practical value, need ...

  • 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.