PHP 计算与 2 个地址的距离 - 在线
PHP Calculate distance from 2 addresses - online
我从 Whosebug 中获取了这段代码来计算地址:
$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;
一旦我把文件放到我的电脑上,它就很好用了。
但是当我把它放到网上时,它不再起作用了。
有人会知道它会出什么问题吗?
我还读到,对于 API,我们需要一个密钥,但在我的例子中,它来自我想使用的 HTML/PHP 网站。
在此先感谢您的帮助
编辑:
可以找到关于这个问题的好文章 post :
[Why doesn't file_get_contents work?
实际上问题是(如果我理解正确的话)由于服务器的参数,我收到以下消息:
Warning: file_get_contents(): http:// wrapper is disabled in the
server configuration by allow_url_fopen=0
这是我们可以自己修改的参数吗?还是我们必须让虚拟主机修改它?
如问题所示,需要更改 PHP.INI 文件中的 allow_url_fopen 参数。我做到了,现在工作正常。
就我而言(hostpapa 托管)我可以自己做。
距离矩阵现在是强制性的google api key is pass without key distance matrix not work.
请使用此示例代码:
$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);
我从 Whosebug 中获取了这段代码来计算地址:
$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;
一旦我把文件放到我的电脑上,它就很好用了。 但是当我把它放到网上时,它不再起作用了。
有人会知道它会出什么问题吗? 我还读到,对于 API,我们需要一个密钥,但在我的例子中,它来自我想使用的 HTML/PHP 网站。
在此先感谢您的帮助
编辑:
可以找到关于这个问题的好文章 post :
[Why doesn't file_get_contents work?
实际上问题是(如果我理解正确的话)由于服务器的参数,我收到以下消息:
Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0
这是我们可以自己修改的参数吗?还是我们必须让虚拟主机修改它?
如问题所示,需要更改 PHP.INI 文件中的 allow_url_fopen 参数。我做到了,现在工作正常。 就我而言(hostpapa 托管)我可以自己做。
距离矩阵现在是强制性的google api key is pass without key distance matrix not work.
请使用此示例代码:
$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);