-1

For college, I've made a dynamic news website which uses openweathermap and ipinfo to create a little weather info line in the navbar. Initially it threw an error 'failed loading cafile stream' which was solved by installing a CA certificate in xampp/Apache/bin.

I've got a vague idea what this does - something in relation to making sure the peer's server certificate is valid, but I thought this was only necessary if you're using the 'curl' library? I'm not sure where in my code I've used this, unless it's related to where I pull info from one of the URLs? Just looking for clarification on where in the code 'curl' is used, what it's doing and why exactly I need this certificate. Also as an additional point, if I were to send my files to another person, would they also have to install this .crt file to 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;

Thank you!

  • the above code would not make use of the cacert so it must lie elsewhere in your code ~ unless under the bonnet simpleXML does the network requests using SSL if necessary and looks in some default location – Professor Abronsius Mar 13 '20 at 15:36
  • I thought the same @RamRaider, though I don't have anything else similar to this elsewhere in the code, and the 'failed loading cafile stream' error only showed up when I added this code – Jake Black Mar 13 '20 at 17:18
  • I tried a slightly modified version of the above ( with valid key for the api ) and it all worked OK - no errors or warnings and no default location set for the cacert on this particular setup which leads me to think the issue might be elsewhere. – Professor Abronsius Mar 13 '20 at 18:03
  • Thanks for trying that! Appreciate you looking at it. If I comment out the above code and run the file, there are no issues. I've also tried running this code with slight additions on its own, I get the same error 'failed loading cafile stream' and 'simplexml_load_file(): Failed to enable crypto' Confusing! – Jake Black Mar 13 '20 at 18:49

1 Answers1

0

The slightly modified version I tried was as follows ( using json rather than XML for simplicity ):

$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);
    }
}

Which yielded:

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

Which is a little odd as I have never been to Sheffield and it's nowhere near where I live but I know my ISP routes traffic all over the reekin so geo-location etc never works. That aside there were no errors and no requirement for a valid cacert

good luck

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • Thanks a lot for attempting this. I thought this was gonna do it! But then I removed the .crt file and I got the same error with this code! Maybe the problem is with my XAMPP installation? – Jake Black Mar 13 '20 at 20:04