PHP 字符串特殊字符

PHP string specialchars

我正在设置一个可通过 Android 上的应用程序使用的小型网络服务。 服务器中的函数是这样的:

 $lat=43.0552592;
 $lng=12.4483577;
 $radius=2000;

    $url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$raidus."&types=restaurant&sensor=false&key=".$apiKey;

    $urlW = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=43.0552592,12.4483577&radius=2000&types=restaurant&sensor=false&key=XXXXxxxXXxxXXxxxxXXX";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    // Set so curl_exec returns the result instead of outputting it.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Get the response and close the channel.
    $response = curl_exec($ch);

return $response;

问题是,如果我用 $url 查询 Google(如上例所示),它 return 是 JSON 和 'INVALID REQUEST' ,但是如果第一个 curl_opt 的查询是用 $curlW 完成的,那将非常有效。
在调试时我发现,使 $url return 得到每个 & 转换(before the curl_init!) &amp……! 因此,我几乎尝试了每个 PHP 字符串函数来强制每个 & 解码或将每个 &entities 替换为 & ,但没有任何结果。
有什么建议吗?

你做错了什么,

$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$raidus."&types=restaurant&sensor=false&key=".$apiKey

应该是

$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$radius."&types=restaurant&sensor=false&key=".$apiKey;

但我猜这不是问题所在,请尝试使用 file_get_contents() 而不是 curl,因此 $response 将是这样的:

$response = file_get_contents($url);