Curl POST请求从Google放置API返回错误。 “服务需要一把钥匙”

时间:2017-06-20 15:22:24

标签: php curl google-api

我遇到卷曲请求时遇到问题 - 将文本搜索发布到Google Places API。

代码如下,但如果我手动构建网址,请求很好&返回正确的搜索数据。看起来我的curl请求(POST)由于某种原因没有发送帖子数据。

任何人都知道我在这里做错了什么?

@EJB(name="ejb/colisDao", beanInterface=ColisDao.class)
public class RecoveryImpl  {

    public static void fillDataBase() throws IOException {
        try {         
            colisDao = (ColisDao) new InitialContext().lookup("ejb/colisDao");
        }
        colisDao.creer(c);
    }
}

这是我得到的回复,我是否应该能够查看是否从请求标头发布了数据?

<?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.

1 个答案:

答案 0 :(得分:0)

我不知道你现在有这个排序,但你可以尝试这样的事情。而不是使用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()  
)