将 cURL 用于 Microsoft Vision API

Use cURL for Microsoft Vision API

您好,我正在尝试使用 Microsoft Vision API 获取对从 LINE 消息应用程序发布的图像的描述。

现在我用 PHP 编码并使用 curl, 我没有收到错误消息,但似乎没有问题。 我只收到消息; "I can see!".

我在下面得到日志。

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,其他代码运行良好。

非常感谢。

https://dev.projectoxford.ai/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fa/console

//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);

根据在评论中的讨论中获得的额外信息,结果证明发送到 API 的 post 数据结构不正确。交个图的关键URL应该是url,不是url:

$post_data = array(
   'url' => $image_url
);

通过该更改,API 交付了预期的结果。