2017-06-20 72 views
0

我在curl請求時遇到問題 - 將文本搜索發佈到Google Places API。Curl POST請求返回來自Google Places API的錯誤。 「服務需要密鑰」

代碼在下面,但是如果我手動構建URL,請求很好&返回我的正確搜索數據。似乎我的curl請求(POST)由於某種原因未發送發佈數據。

任何人有任何想法我在這裏做錯了嗎?

<?php 

$options = array(
    'url' => 'https://maps.googleapis.com/maps/api/place/textsearch/xml?', 
    'query' => 'my+query+terms', 
    'location' => '43.836296,-79.0851207', 
    'radius' =>'5000', 
    'key' => '_my_api_key_' 
    ); 

$api = array_shift($options); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_VERBOSE, TRUE); 
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE); 
curl_setopt($ch, CURLOPT_URL, $api); 
curl_setopt($ch, CURLOPT_POST, TRUE); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $options); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

$result = curl_exec($ch); 
$request = curl_getinfo($ch); 

curl_close($ch); 

echo '<pre>'; print_r($request); echo '</pre>'; 
echo '<pre>'; print_r($result); echo '</pre>'; 

return; 

這是我得到的迴應,如果網絡能夠看到數據是否從請求標頭髮布?

Array 
(
    [url] => https://maps.googleapis.com/maps/api/place/textsearch/xml? 
    [content_type] => application/xml; charset=UTF-8 
    [http_code] => 200 
    [header_size] => 418 
    [request_size] => 218 
    [filetime] => -1 
    [ssl_verify_result] => 0 
    [redirect_count] => 0 
    [total_time] => 1.047086 
    [namelookup_time] => 0.000911 
    [connect_time] => 0.002002 
    [pretransfer_time] => 0.033092 
    [size_upload] => 495 
    [size_download] => 183 
    [speed_download] => 174 
    [speed_upload] => 472 
    [download_content_length] => -1 
    [upload_content_length] => 495 
    [starttransfer_time] => 1.034263 
    [redirect_time] => 0 
    [certinfo] => Array 
     (
     ) 

    [primary_ip] => xxx.xxx.xxx.xxx 
    [primary_port] => 443 
    [local_ip] => xxx.xxx.xxx.xxx 
    [local_port] => 52630 
    [redirect_url] => 
    [request_header] => POST /maps/api/place/textsearch/xml? HTTP/1.1 
Host: maps.googleapis.com 
Accept: */* 
Content-Length: 495 
Expect: 100-continue 
Content-Type: multipart/form-data; boundary=------------------------xxxxxxx 


    ) 


REQUEST_DENIED 
This service requires an API key. 

訂正工作CODE:

<?php 

$options = array(
    'url' => 'https://maps.googleapis.com/maps/api/place/textsearch/xml?', 
    'query' => '_the_query_string_', 
    'location' => '43.836296,-79.0851207', 
    'radius' =>'5000', 
    'key' => '_my_api_key_' 
    ); 

$headers = array(
    'Accept: application/xml', 
    'Content-Type: application/xml', 
    ); 

$api = array_shift($options) . http_build_query($options); 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_VERBOSE, TRUE); // not really needed 

curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE); 

curl_setopt($ch, CURLOPT_URL, $api); 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

$result = curl_exec($ch); 

// $request = curl_getinfo($ch); // also not needed 

curl_close($ch); 

echo $result; 

return; 
+0

而非POST請求嘗試與形成爲URL的查詢字符串部分中的參數的GET請求 – RamRaider

+0

facepalm!那真是令人尷尬!是的 - 獲取請求是去這裏的方式。我會假設谷歌只是無視任何得到張貼... –

回答

0

我不硝酸鉀WIF你現在有這個排序的,但你可以嘗試這樣的事情。而不是使用POST此使用查詢字符串作爲URL

$key='AIzaSyD3tAJwB463-PpaqVS7pcMqWkkqnki0Kgk'; 
$query='Forfar'; 
$location='52,-2.5'; 
$cacert='c:/wwwroot/cacert.pem'; 
$format='xml'; 


$options = array(
    'url'  => 'https://maps.googleapis.com/maps/api/place/textsearch/'.$format.'?', 
    'query'  => $query, 
    'location' => $location, 
    'radius' => '50', 
    'key'  => $key 
); 


$url = urldecode(array_shift($options) . http_build_query($options)); 


$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLINFO_HEADER_OUT, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_CAINFO, $cacert); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 

$data = curl_exec($ch); 
$info = curl_getinfo($ch); 

curl_close($ch); 

echo '<textarea cols=100 rows=20>',print_r($data,1),print_r($info,1),'</textarea>'; 

的一部分,並且輸出

<?xml version="1.0" encoding="UTF-8"?> 
<PlaceSearchResponse> 
<status>OK</status> 
<result> 
    <name>Forfar</name> 
    <type>locality</type> 
    <type>political</type> 
    <formatted_address>Forfar DD8, UK</formatted_address> 
    <geometry> 
    <location> 
    <lat>56.6435580</lat> 
    <lng>-2.8890620</lng> 
    </location> 
    <viewport> 
    <southwest> 
    <lat>56.6225256</lat> 
    <lng>-2.9239470</lng> 
    </southwest> 
    <northeast> 
    <lat>56.6601780</lat> 
    <lng>-2.8580524</lng> 
    </northeast> 
    </viewport> 
    </geometry> 
    <icon>https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png</icon> 
    <reference>CmRbAAAACjwLAMU8k-MwG-Dmf6KhKmBG5Ms7CHJ6TZyH4g2pZlotgC93vbvl8aAAa3Xfc2sE7GnFFbr48auYaykQItR2jfSRUkr04rG0-luAyb1hm97OtSf6yCDqMzvd3z8e0q14EhA6m7z1cLhYlNt_rXB3K3-xGhQQe0bxNrmWATf8wqWydqlsp16Lrg</reference> 
    <id>7a7c726f4d8b907b1bcd39503c4348c6965916d9</id> 
    <photo> 
    <photo_reference>CmRaAAAABNJLUk7ET6Wa4JXEh9OSwj1s3dvIr25twTy_Yk7ey5n2LxGmVm_anlebTTbJ2e6ZTrtHMa4ij7vYLypy6XMhr6F9CFF0fx9XqQ_0ozgFno_VXuSWDmblc-q4-02nTaHmEhDAuJtLHpMk6RvkltH68iD2GhTCSqmyDYq3qY58O5qJoqJwlrCITg</photo_reference> 
    <width>4912</width> 
    <height>1920</height> 
    <html_attribution><a href="https://maps.google.com/maps/contrib/109648577636572315039/photos">L Hill</a></html_attribution> 
    </photo> 
    <place_id>ChIJW5AkiE1ghkgRfVEeH582Uzg</place_id> 
</result> 
</PlaceSearchResponse> 


Array 
(
    [url] => https://maps.googleapis.com/maps/api/place/textsearch/xml?query=Forfar&location=52,-2.5&radius=50&key=AIzaSyD3tAJwB463-PpaqVS7pcMqWkkqnki0Kgk 
    [content_type] => application/xml; charset=UTF-8 
    [http_code] => 200 
    [header_size] => 394 
    [request_size] => 171 
    [filetime] => -1 
    [ssl_verify_result] => 20 
    [redirect_count] => 0 
    [total_time] => 0.39 
    [namelookup_time] => 0 
    [connect_time] => 0.047 
    [pretransfer_time] => 0.203 
    [size_upload] => 0 
    [size_download] => 1410 
    [speed_download] => 3615 
    [speed_upload] => 0 
    [download_content_length] => -1 
    [upload_content_length] => 0 
    [starttransfer_time] => 0.39 
    [redirect_time] => 0 
    [certinfo] => Array() 
) 
相關問題