0

I took this code from stackoverflow to calculate address :

$from = urlencode($from);
    $to = urlencode($to.', Челябинс');
    $data = file_get_contents('http://maps.googleapis.com/maps/api/distancematrix/json?origins='.$from.'&destinations='.$to.'&language=ru-RU&sensor=false&mode=driving');//&key=AIzaSyDAJ05io1966D_KmF7lbRsucehr8GtUBqk');
    $data = json_decode($data);
    $time = 0;
    $distance = 0;
    foreach($data->rows[0]->elements as $road) {
        $time += $road->duration->value;
        $distance += $road->distance->value;
    }
    $table[0]=$distance;
    $table[1]=$time;

It is working very good once I have the file on my computer. But when I put it online, it is not working anymore.

Somebody would have an idea of what could go wrong with it? I also read that for APIs we need a key, but in my case it is from a HTML/PHP website I want to use it.

Thanks in advance for your help

Edit :

Could find a good post about this issue :

[Why doesn't file_get_contents work?

In fact the problem is(if I understood correct) due to a parameter of server, I get the following message :

Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0

Is this a parameter we can modify ourself?or do we have to ask web hoster to modify it?

Siegfried.V
  • 1,508
  • 1
  • 16
  • 34
  • "not working anymore" <--- _How_ is it not working any more, what are you seeing vs what you're expecting to see. – Jonnix Aug 10 '17 at 13:54
  • google maps quota exceeded ? the fine print would tell you how many calls you can make (free) per period. As well, the actual response from the API call could help figuring out wtf. – YvesLeBorg Aug 10 '17 at 14:02
  • Well, a friend told me limit is 2000 a day(in fact I checked, it is 25.000)... I do maybe 10 a day. How can I get API response, I will check it. – Siegfried.V Aug 10 '17 at 14:04

2 Answers2

0

Well as shown in question, needed to change allow_url_fopen parameter in PHP.INI file. I did it and now working fine. In my case (hostpapa hosting) I could do it myself.

Siegfried.V
  • 1,508
  • 1
  • 16
  • 34
0

Distance matrix is now compulsory google api key is pass without key distance matrix not work.

Please, use this sample code:

$distance_data = file_get_contents(
    'https://maps.googleapis.com/maps/api/distancematrix/json?&origins='.urlencode($origin).'&destinations='.urlencode($destination).'&key=AIzaSyD6YkF-BVV1LNLOz5n3zeL9bi1farzUX8k'
);
$distance_arr = json_decode($distance_data);
Henry Woody
  • 14,024
  • 7
  • 39
  • 56
  • Welcome to SO. Could you format your code properly, please? E.g. by adding 4 blanks in front of every code line. Thanks. – CKE Sep 19 '18 at 09:07