Home >Backend Development >PHP Tutorial >How to get https request method through curl

How to get https request method through curl

jacklove
jackloveOriginal
2018-06-09 11:29:526239browse

curl to obtain https request method

I am working on a project today and need curl to obtain a third-party API. The other party’s API is https. .

I was able to obtain http requests using curl before, but when I obtained https requests today, the following error message appeared: Certificate verification failed.

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

Solution, when making a curl request, add

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在

curl https request code

<?php
/** curl 获取 https 请求
* @param String $url        请求的url
* @param Array  $data       要發送的數據
* @param Array  $header     请求时发送的header
* @param int    $timeout    超时时间,默认30s
*/
function curl_https($url, $data=array(), $header=array(), $timeout=30){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
    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;
}
// 调用
$url = &#39;https://www.example.com/api/message.php&#39;;
$data = array(&#39;name&#39;=>&#39;fdipzone&#39;);
$header = array();
$response = curl_https($url, $data, $header, 5);
echo $response;
?>

This article explains how to obtain the https request method through curl. For more related content, please pay attention to the php Chinese website.

Related recommendations:

Example of uploading pictures and saving them to the database through php

How to send through php And receiving stream files

#How to mosaic a picture partially through php

The above is the detailed content of How to get https request method through curl. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn