0

Days ago I´m had a problem with the file_get_contents, because the server has the rule allow_url_fopen=0

I´ve tried to change that by making a php.ini with allow_url_fopen=1, but its not working. This is the part of the code that has the issue:

if($state == 5)
    {   
        $request_services=RequestServices::where('request_id', $current_request->id)->first();
        $address = urlencode(Input::get('address'));
        $end_address = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=$address"),TRUE);

        $end_location = $end_address['results'][0]['geometry'];
        $latitude = $end_location['location']['lat'];
        $longitude = $end_location['location']['lng'];

        $request_id = $current_request->id;
        $walk_location_last = WalkLocation::where('request_id',$request_id)->orderBy('created_at','desc')->first();

        if($walk_location_last)
        {
            $distance_old = $walk_location_last->distance;
            $distance_new = distanceGeoPoints($walk_location_last->latitude,$walk_location_last->longitude,$latitude,$longitude);
            $distance = $distance_old + $distance_new;
            $settings = Settings::where('key','default_distance_unit')->first();
            $unit = $settings->value;
            $distance = $distance;
        }
        else{
            $distance = 0;
        }

The error is in this line:

$end_address = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=$address"),TRUE);

It is calling file_get_contents with a google maps page url. I´m having a really hard time trying to change it to cURL with on what a have read is the correct and the best choice. Can you please help me by making this code to work as cURL.

Thanks in advance. I´m attaching a screen shot of the debug mode of the page

Image with error

gevorg
  • 4,835
  • 4
  • 35
  • 52
pacokent
  • 63
  • 1
  • 10
  • There could be many reasons for your problem, you might miss some extensions or there might be a configuration problem. There is also an alternative solution using `curl`. – gevorg Jul 05 '16 at 14:59

1 Answers1

0

You have to make sure following things:

HTTPS wrapper

Make sure you have HTTPS wrapper installed:

$w = stream_get_wrappers();

echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";

Result must be:

openssl: yes

http wrapper: yes

https wrapper: yes

PHP.ini

Make sure option is enabled:

allow_url_fopen = On

Alternative

As an alternative you can use cUrl:

function getSslPage($url) {
    $ch = curl_init();

    // THIS LINE DISABLES CERTIFICATE VERIFICATION!!!
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}

For more information look answers for these questions:

Community
  • 1
  • 1
gevorg
  • 4,835
  • 4
  • 35
  • 52