按照Google文档使用PHP创建请求(link),但在1秒内收到空响应{}
。我尝试使用另一种语言发送相同的请求,大约需要10秒才能收到正确的响应。我尝试发表评论CURLOPT_POSTFIELDS => $json
,我可以收到POST requests require a <code>Content-length</code> header
的错误回复。想知道我在下面的代码中遗漏了什么吗?
<?php
$str='{
"requests":[
{
"image":{
"source":{
"imageUri":
"https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
}
},
"features":[
{
"type":"LOGO_DETECTION",
"maxResults":1
}
]
}
]
}';
$json = json_decode($str, true);
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://vision.googleapis.com/v1/images:annotate?key=kkkkkkeeeeeyyyyy',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $json
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
echo $resp;
?>
修改
如果我在终端中使用curl并将str保存在.json文件中,我可以得到正确的结果。
终端申请
curl -v -s -H "Content-Type: application/json" https://vision.googleapis.com/v1/images:annotate?key= kkkkkkeeeeeyyyyy --data-binary @request.json
Request.json
{
"requests":[
{
"image":{
"source":{
"imageUri":
"https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
}
},
"features":[
{
"type":"LOGO_DETECTION",
"maxResults":1
}
]
}
]
}
响应
* Trying 172.217.24.202...
* Connected to vision.googleapis.com (172.217.24.202) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /Users/pakhocheung/anaconda/ssl/cacert.pem
CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-ECDSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.googleapis.com
* start date: Oct 24 08:38:00 2017 GMT
* expire date: Dec 29 00:00:00 2017 GMT
* subjectAltName: host "vision.googleapis.com" matched cert's "*.googleapis.com"
* issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
* SSL certificate verify ok.
> POST /v1/images:annotate?key=AIzaSyBFPRLC8BEtYPdfgg3B_aadpmKFRtfENbE HTTP/1.1
> Host: vision.googleapis.com
> User-Agent: curl/7.49.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 318
>
* upload completely sent off: 318 out of 318 bytes
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8
< Vary: X-Origin
< Vary: Referer
< Date: Tue, 07 Nov 2017 04:13:12 GMT
< Server: ESF
< Cache-Control: private
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< X-Content-Type-Options: nosniff
< Alt-Svc: quic=":443"; ma=2592000; v="41,39,38,37,35"
< Accept-Ranges: none
< Vary: Origin,Accept-Encoding
< Transfer-Encoding: chunked
<
{
"responses": [
{
"logoAnnotations": [
{
"mid": "/m/045c7b",
"description": "Google",
"score": 0.34495986,
"boundingPoly": {
"vertices": [
{
"x": 72,
"y": 53
},
{
"x": 400,
"y": 53
},
{
"x": 400,
"y": 164
},
{
"x": 72,
"y": 164
}
]
}
}
]
}
]
}
答案 0 :(得分:0)
CURLOPT_POSTFIELDS
应该是json编码的字符串。看起来您手动输入编码字符串然后解码它。这样的事情会更合适:
$json='{"requests":[{"image":{"source":{"imageUri":"https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"}},"features":[{"type":"LOGO_DETECTION","maxResults":1}]}]}';
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://vision.googleapis.com/v1/images:annotate?key=kkkkkkeeeeeyyyyy',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $json
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
echo $resp;
您可能需要考虑在数据结构(数组/对象)中定义数据,然后使用json_encode()
将其转换为字符串,而不是直接将数据键入字符串。虽然不是严格要求,但我发现它确实减少了json语法错误。