2017-07-15 74 views
0

的URL中的字符我正在使用Google Places API ...我試圖存儲具有&(Ampersand)的位置。我似乎不能編碼變量。例如,公司名稱叫做Dave & Busters,但我可以在請求url的url中使用&,因爲這是爲參數保留的。如果使用這兩個函數,我嘗試更換&。也許我用錯了,..替換使用&

$name = 'Dave & Busters'; 
if (strpos($name, '&') !== false) { 
//& SIGN FOUND 
//WONT WORK FOR URL NEED TO REPLACE 
$name = str_replace('&', 'and', $string); 

} 
$name = urlencode($name); 
$url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$long&radius=10&type=restaurant&keyword=$name&key=myKey"; 

編輯*只使用urlencode()我得到這個。戴夫+僅顯示

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.7455483,-73.5951152&radius=10&type=restaurant&keyword=Dave+&key=AIzaSyD4rcVpAgnP650-DCeL2L4zTnPSo4d81vk 

回答

4

您需要使用urlencode()在查詢字符串中使用的任何數據。

更重要的是,只要使用http_build_query()

$url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' . http_build_query([ 
    'location' => $lat . ',' . $long, 
    'radius' => 10, 
    'type' => 'restaurant', 
    'keyword' => $name, 
    'key' => 'myKey' 
]); 
+0

我使用的是用urlencode,但它持續輸出高達戴夫+僅 – Carlitos

+0

@Carlitos停止之前'進行urlencode替代的東西()'。 – Brad

+0

我也試過這樣做。我沒有得到任何迴應,因爲參數與戴夫& busters&符號混淆 $ url =「https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$long&radius = 10&類型=餐廳和關鍵字= $名稱和鑰匙=的myKey「; – Carlitos