您好我正在尝试使用Microsoft Vision API来获取从LINE Messenger应用程序发布的图像的描述。
现在我用PHP编写并使用curl, 我没有得到错误消息,但似乎有些不对劲。 我只收到消息; “我能看到!”。
我得到以下日志。
2016-12-30T10:25:14.550661+00:00 app[web.1]: [30-Dec-2016 19:25:14 Asia/Tokyo] stdClass Object
2016-12-30T10:25:14.550706+00:00 app[web.1]: (
2016-12-30T10:25:14.550758+00:00 app[web.1]: [code] => InvalidImageUrl
2016-12-30T10:25:14.550801+00:00 app[web.1]: [requestId] => 871afc35-7c01-478b-9c0c-c51d365bfba4
2016-12-30T10:25:14.550853+00:00 app[web.1]: [message] => Can't fetch the image.
2016-12-30T10:25:14.550855+00:00 app[web.1]: )
2016-12-30T10:25:14.550855+00:00 app[web.1]:
2016-12-30T10:25:15.299165+00:00 heroku[router]: at=info method=POST path="/callback.php" host=kasabot30.herokuapp.com request_id=2fc2bb0d-4ca5-4d0c-94c4-540345bb3726 fwd="203.104.146.152" dyno=web.1 connect=1ms service=1715ms status=200 bytes=150
2016-12-30T10:25:15.291527+00:00 app[web.1]: 10.154.74.169 - - [30/Dec/2016:10:25:13 +0000] "POST /callback.php HTTP/1.1" 200 - "-" "LineBotWebhook/1.0
2016-12-30T10:29:47.964240+00:00 app[web.1]: 10.109.176.159 - - [30/Dec/2016:10:29:47 +0000] "GET /Lenna.png HTTP/1.1" 200 473831 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
2016-12-30T10:29:47.967859+00:00 heroku[router]: at=info method=GET path="/Lenna.png" host=kasabot30.herokuapp.com request_id=f3fe060b-3740-4660-8044-e8bd4b07f6eb fwd="219.208.70.99" dyno=web.1 connect=0ms service=13ms status=200 bytes=474068
你帮我找错了吗?
除了Vision API,其他代码运行良好。
提前非常感谢。
//Function to post on Microsoft Vision API
$url = 'https://api.projectoxford.ai/vision/v1.0/analyze?visualFeatures=Description&language=en';
$api_key ='MY_KEY';
$image_url = 'https://kasabot30.herokuapp.com/Lenna.png';
$post_data = array(
"url:" => "$image_url"
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode($post_data)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Ocp-Apim-Subscription-Key:'.$api_key
));
$image_json_string = curl_exec($ch);
curl_close($ch);
$image_json = json_decode($image_json_string);
$image_description = $image_json->{"description"}->{"captions"}[0]->{"text"};
$response_format_text = [
array(
"type" => "text",
"text" => "I can see".$image_description."!"
)
];
//Function to post on LINE Message API
$ch = curl_init("https://api.line.me/v2/bot/message/reply");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;charser=UTF-8',
'Authorization: Bearer ' . $accessToken
));
$result = curl_exec($ch);
curl_close($ch);
答案 0 :(得分:1)
根据评论中讨论中获得的其他信息,发现发送到API的帖子数据结构不正确。移交图片网址的关键应该是url
,而不是url:
:
$post_data = array(
'url' => $image_url
);
通过该更改,API提供了预期的结果。