【问题标题】:Why do I need curl-ca-bundle.crt?为什么我需要 curl-ca-bundle.crt?
【发布时间】:2020-03-13 15:13:47
【问题描述】:

对于大学,我制作了一个动态新闻网站,它使用 openweathermapipinfo 在导航栏中创建一个小的天气信息行。最初它抛出一个错误“加载 cafile 流失败”,通过在 xampp/Apache/bin 中安装 CA 证书来解决。

我对它的作用有一个模糊的概念 - 与确保对等方的服务器证书有效有关的东西,但我认为只有在使用“curl”库时才需要这样做?我不确定我在代码中的哪个位置使用了它,除非它与我从其中一个 URL 中提取信息的位置有关?只是想澄清一下代码'curl'在哪里使用,它在做什么以及为什么我需要这个证书。另外,如果我要将我的文件发送给另一个人,他们是否还必须将此 .crt 文件安装到 xampp/apache/bin?

$query = @unserialize (file_get_contents('http://ip-api.com/php/'));

if ($query && $query['status'] == 'success') {
foreach ($query as $data) {
     $data . "<br>";
}
}

$url="https://api.openweathermap.org/data/2.5/find?q=" . $query['city'] . "," . $query['countryCode'] . "&units=imperial&type=accurate&mode=xml&APPID=MYKEYCODE";

/*Converts an XML document to an object we can pull our info from*/

$getweather = simplexml_load_file($url);
$gettemp = $getweather->list->item->temperature['value'];
$celcius = ($gettemp - 32) * 5/9;

谢谢!

【问题讨论】:

  • 上述代码不会使用 cacert,因此它必须位于代码中的其他位置 ~ 除非在引擎盖下 simpleXML 在必要时使用 SSL 进行网络请求并查看某个默认位置
  • 我也有同样的想法@RamRaider,虽然我在代码的其他地方没有类似的东西,并且“加载 cafile 流失败”错误仅在我添加此代码时出现跨度>
  • 我尝试了上面的稍微修改的版本(使用有效的 api 密钥),一切正常 - 没有错误或警告,也没有为 cacert 在这个特定设置上设置默认位置,这导致我认为问题可能出在其他地方。
  • 感谢您的尝试!感谢你看着它。如果我注释掉上面的代码并运行文件,就没有问题。我也尝试过单独运行此代码并稍加添加,我得到相同的错误“加载 cafile 流失败”和“simplexml_load_file():无法启用加密”令人困惑!

标签: php web xampp ssl-certificate php-curl


【解决方案1】:

我尝试的稍微修改的版本如下(为简单起见使用json而不是XML):

$appkey='xxxxxxxxxx165be29428029b';
$data=(object)@unserialize( file_get_contents('http://ip-api.com/php/') );
if( $data ){
    $city=$data->city;
    $countryCode=$data->countryCode;

    $url=sprintf('https://api.openweathermap.org/data/2.5/find?q=%s,%s&units=imperial&type=accurate&mode=json&APPID=%s',$city,$countryCode,$appkey);
    $json=json_decode( file_get_contents( $url ) ) ?: false;

    if( $json ){
        $temp=$json->list[0]->main->temp;
        $celcius=( ( $temp - 32 ) * 5/9 );

        printf("<pre>City: %s\nCountry: %s\nTemperature: %sF (%sC)</pre>",$city,$countryCode,$temp,$celcius);
    }
}

结果:

City: Sheffield
Country: GB
Temperature: 42.17F (5.65C)

这有点奇怪,因为我从未去过谢菲尔德,而且离我住的地方也不远,但我知道我的 ISP 将流量路由到整个 reekin,因此地理位置等永远不会起作用。除此之外,没有错误,也不需要有效的cacert

祝你好运

【讨论】:

  • 非常感谢您尝试此操作。我以为这会做到!但后来我删除了 .crt 文件,这段代码出现了同样的错误!也许问题出在我的 XAMPP 安装上?
猜你喜欢
  • 1970-01-01
  • 2016-03-09
  • 2017-02-24
  • 1970-01-01
  • 2012-06-17
  • 2018-02-15
  • 2016-09-09
  • 1970-01-01
  • 2012-08-24
相关资源
最近更新 更多