Warning: Undefined array key "night" in /www/wwwroot/dhaomu.com/wp-content/themes/b2/header.php on line 18

PHP怎么使用curl访问https?

之前在做Project的时候,用的第三方的API,不过对方的API是https方式的,在之前使用curl能获取http请求,但换为获取https请求时,返回的内容总是为空,查阅资料后发现如果要用curl访问https,自己需要添加以下两行代码:

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

封装函数

/** 获取 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, 2); // 从证书中检查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);   
}
Example:
 $url = 'https://www.example.com/example.php';   
    $data = array('name'=>'fdipzone');   
    $header = array();   
      
    $response = curl_https($url, $data, $header, 5);   
      
    echo $response;

温馨提示:
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:duhaomu@163.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

给TA赞赏
共{{data.count}}人
人已赞赏
未整理

直播带货怎么做?商家如何玩转直播带货,针对商家 内容干货 目的赚钱

2021-5-5 0:00:00

未整理

云优YUNUCMS企业网站管理系统

2021-5-7 0:00:00

0 条回复 A文章作者 M管理员

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914
----《》
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索