2017-07-15 1 views
0
を使用しているURLの文字を置換する

Google Places APIを使用しています... &(アンパサンド)の場所を保存しようとしています。変数をエンコードしているようです。たとえば、ビジネス名はDave & Bustersと呼ばれていますが、パラメータとして予約されているので、リクエストURLのURLに&を使用できます。これらの2つの機能を使用して見つかった場合は&を置き換えようとしました。たぶん私は間違って使用して..

$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"; 

EDIT *私はこれを取得します。 Dave +のみ表示

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()を使用する必要があります。私はでurlencodeを使用しています

$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ストップまで出力し続ける:

いっそのこと、ちょうどhttp_build_query()を使用しています。 – Brad

+0

私もそれをやってみました。パラメータがdave&bustersアンパサンドと混同されているので、私は何の応答も得ません $ url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$long&radius = 10&type =レストラン&キーワード= $ name&key = myKey "; – Carlitos