dtdfl62844 2016-12-30 08:53
浏览 42
已采纳

使用cURL for Microsoft Vision API

Hi I'm trying to use Microsoft Vision API to get a description of an image which is posted from LINE messenger apps.

Now I code by PHP and use curl, I dont't get error message but it seems something wrong. I only get the message; "I can see!".

I get logs below.

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

Could you please help me to find my mistake? Except for Vision API, other codes are working well.

Thanks so much in advance.

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);
  • 写回答

1条回答 默认 最新

  • dopcpc9207 2016-12-30 10:53
    关注

    Based on the additional information gained in the discussion in the comments it turned out that the post data sent to the API was structured incorrectly. The key to hand over an image URL should be url, not url::

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

    With that change the API delivered the expected result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗