我试图通过谷歌的距离矩阵API一些HTML表单输入。我已经将它们放入变量中,并用“+”符号替换空格。当我回应变量时,他们是完美的。当我硬编码这些变量值时,api返回距离,但当我使用变量表示时,它不返回任何值。为什么谷歌距离矩阵不接受我的变量作为位置?
<?php
$start = $_POST["origin"];
$end = $_POST["destination"];
$value = strtolower(str_replace(' ', '+', $start));
echo $value;
$value2 = strtolower(str_replace(' ', '+', $end));
echo $value2;
$url = 'http://maps.googleapis.com/maps/api/distancematrix/json?
origins=$value&destinations=$value2&mode=driving&language=English-
en&key=$key"';
$json = file_get_contents($url); // get the data from Google Maps API
$result = json_decode($json, true); // convert it from JSON to php array
echo $result['rows'][0]['elements'][0]['distance']['text'];
?>
您使用的是单引号,但似乎在字符串中有php变量 - 它们需要在整个url周围没有引号/转义或使用双引号。即:'$ URL =“http://maps.googleapis.com/maps/api/distancematrix/json?origins={$value}&destinations={$value2}&mode=driving&language=English- EN&关键= {$键} “' – RamRaider
和JSON后'有空格'在上述网址 – RamRaider
@RamRaider嘿谢谢!现在它工作得很好。继续,把它扔进一个帖子,所以我可以将它标记为答案 – jameson1128