为什么我需要curl-ca-bundle.crt



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

我对这是什么有一个模糊的想法——这与确保对等方的服务器证书有效有关,但我认为只有当你使用"curl"库时,这才是必要的?我不确定我在代码中的哪里使用过这个,除非它与我从其中一个URL提取信息的位置有关?只是想澄清一下"curl"在代码中的使用位置,它在做什么,以及我为什么需要这个证书。另外,如果我要将我的文件发送给另一个人,他们是否也必须将这个.crt文件安装到xamp/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;

谢谢!

我尝试的稍微修改过的版本如下(为了简单起见,使用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: %snCountry: %snTemperature: %sF (%sC)</pre>",$city,$countryCode,$temp,$celcius);
}
}

结果:

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

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

祝好运

最新更新