PHP计算2个地址的距离 - 在线

问题描述 投票:0回答:2

我从stackoverflow中获取此代码来计算地址:

$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网站。

在此先感谢您的帮助

编辑:

可以找到关于这个问题的好帖子:

[Why doesn't file_get_contents work?

实际上问题是(如果我理解正确)由于服务器的参数,我得到以下消息:

警告:file_get_contents():http://在服务器配置中禁用包装器allow_url_fopen = 0

这是我们可以自己修改的参数吗?或者我们是否要求网站托管服务商修改它?

php google-distancematrix-api
2个回答
0
投票

好吧,如图所示,需要更改PHP.INI文件中的allow_url_fopen参数。我做到了,现在工作正常。在我的情况下(hostpapa托管)我可以自己做。


0
投票

距离矩阵现在是必须的,谷歌api密钥是通过没有密钥距离矩阵不起作用。

请使用以下示例代码:

$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);
© www.soinside.com 2019 - 2024. All rights reserved.