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.

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

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算