请求HTTP

解决办法一:
1,使用php curl获取http资源,不会报错。
如下:

 /*
 * curl_get获取数据
 * */
function curl_get($url){
    $testurl = $url;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) ;
    curl_setopt($ch, CURLOPT_URL, $testurl);
    //参数为1表示传输数据,为0表示直接输出显示。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //参数为0表示不带头文件,为1表示带头文件
    curl_setopt($ch, CURLOPT_HEADER,0);
    $output = curl_exec($ch);
    if(curl_exec($ch) === false){
        echo 'Curl error: ' . curl_error($ch);
    }
    curl_close($ch);
    return $output;
}

2,假如请求的目标网站是https,会报错,报错信息如下
Curl error: SSL certificate problem: unable to get local issuer certificatebool(false)
这是因为HTTPS需要证书认证,如果本地没有装,就会报这个错误,我们可以把这个认证关掉。
代码如下:

 /*
 * curl_get获取数据
 * */
function curl_get($url){
    $testurl = $url;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) ;
    curl_setopt($ch, CURLOPT_URL, $testurl);
    //参数为1表示传输数据,为0表示直接输出显示。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //参数为0表示不带头文件,为1表示带头文件
    curl_setopt($ch, CURLOPT_HEADER,0);
    // 关闭SSL验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    $output = curl_exec($ch);
    if(curl_exec($ch) === false){
        echo 'Curl error: ' . curl_error($ch);
    }
    curl_close($ch);
    return $output;
}

解决办法二:
配置https证书
另外的一种解决办法,就是配置上HTTPS,找到证书:

网址:http://curl.haxx.se/ca/cacert.pem
下载pem文件。

在PHP配置文件(php.ini)里配置PEM文件目录位置。
curl.cainfo = "path\to\cacert.pem"

参考:https://blog.csdn.net/lilongsy/article/details/85012503

相关文章:

  • 2021-06-09
  • 2021-12-03
  • 2022-12-23
  • 2021-07-14
  • 2022-12-23
  • 2022-12-23
  • 2021-05-01
  • 2021-11-07
猜你喜欢
  • 2022-12-23
  • 2021-07-22
  • 2021-12-02
  • 2021-07-26
相关资源
相似解决方案