Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 谷歌地图里程功能返回两个地址之间的里程_Php_Google Maps_Yii2_Google Distancematrix Api - Fatal编程技术网

Php 谷歌地图里程功能返回两个地址之间的里程

Php 谷歌地图里程功能返回两个地址之间的里程,php,google-maps,yii2,google-distancematrix-api,Php,Google Maps,Yii2,Google Distancematrix Api,对于每个循环,退出工作 public static function getMiles($to, $from) { $from = urlencode($from); $to = urlencode($to); $data = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=$from&destinatio

对于每个循环,退出工作

public static function getMiles($to, $from)
{

    $from = urlencode($from);
    $to = urlencode($to);
    $data = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=$from&destinations=$to&key=MyKey");

    $time = 0;
    $distance = 0;

    /* return as an object for easier notation */
    $json=json_decode( $data, false );
    $origins = $json->origin_addresses;
    $destinations=$json->destination_addresses;

    /*  if there are multiple rows, use a loop and `$rows[$i]` etc */
    $elements=$json->rows[0]->elements;

    foreach( $elements as $i => $obj ){
    } 
    //extract the mileage as meters
    $miles = $obj->distance->value;

    //converting the meters to miles
    $distance= round(($miles * '0.000621371192'), 1);
    //returning miles
    return $distance;

}
最终结果是返回两个地址之间的里程数。
错误消息是“为foreach()提供的参数无效”。

您的函数在我这方面工作正常。实际上,只有当我从距离矩阵API请求中混淆API键时,才会出现“为foreach()提供的参数无效”错误

尝试使用有效的API密钥运行以下代码(替换“您的密钥”):

您得到的输出应该是
英里数:659

我建议你仔细检查一下你的钥匙、项目和账单账户是否完好


希望这有帮助

那是我的错。经过进一步挖掘,我能够确定我的信用卡已经过期。重置我的过期日期,代码将重新运行。
感谢穆罕默德·奥马尔·阿斯拉姆(Muhammad Omer Aslam)帮我找到正确答案。

这意味着
$elements=$json->rows[0]->elements
不是数组。当我添加以下内容时,请尝试使用
print_r()
检查数组中的内容:/*作为对象返回以便更容易地表示*/$json=json\u解码($data,false);$origins=$json->origin_addresses;$destinations=$json->destination_addresses;echo“$destinations”;die();结果是“array”。当我使用print_r($destinations)时,我会看到一个空白屏幕。谢谢Evan,是计费帐户很高兴听到你在重新启用计费后解决了这个问题。:)
function getMiles($to, $from)
{
    $from = urlencode($from);
    $to = urlencode($to);
    $data = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=$from&destinations=$to&key=YOUR_KEY");

    $time = 0;
    $distance = 0;

    /* return as an object for easier notation */
    $json=json_decode( $data, false );
    $origins = $json->origin_addresses;
    $destinations=$json->destination_addresses;
    /*  if there are multiple rows, use a loop and `$rows[$i]` etc */
    $elements=$json->rows[0]->elements;

    foreach( $elements as $i => $obj ){
    } 
    //extract the mileage as meters
    $miles = $obj->distance->value;

    //converting the meters to miles
    $distance= round(($miles * '0.000621371192'), 1);
    //returning miles
    return $distance;
}

echo "\nMiles: " . getMiles("paris,france", "berlin,germany");