0

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);
TKasai
  • 77
  • 1
  • 1
  • 10
  • So what do you mean by "it seems something wrong"? Why don't you tell us? – arkascha Dec 30 '16 at 08:59
  • And what do you see in your http servers error log file? – arkascha Dec 30 '16 at 08:59
  • And for debugging please dump the content of `$image_json` into a log file and add it to the question above. There is an `edit` link below your question. _Use it_ to add more details. – arkascha Dec 30 '16 at 09:00
  • Sorry, I fail to see what `$image_json` contains... – arkascha Dec 30 '16 at 09:26
  • Do you know any way to display $image_json on heroku console log? – TKasai Dec 30 '16 at 10:08
  • I'm very new to heroku and PHP, cant find how to do this. – TKasai Dec 30 '16 at 10:09
  • I tried to display on message by using " "text" => "I can see".substr($image_json,0,30).$image_description."!"" . – TKasai Dec 30 '16 at 10:10
  • Just dump the content using `print_r()`. – arkascha Dec 30 '16 at 10:12
  • Note that `$image_json` is _not_ a string! – arkascha Dec 30 '16 at 10:12
  • But also `$image_json_string` would help... – arkascha Dec 30 '16 at 10:13
  • Thanks! I got 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]: – TKasai Dec 30 '16 at 10:26
  • Please don't add additional information in comments, this is not readable. Use the `edit` button provided below your question to add it. – arkascha Dec 30 '16 at 10:27
  • But this clearly looks like your issue is that you reference a non existing image... – arkascha Dec 30 '16 at 10:27
  • Thanks! Image actually exists. you can also access to it. Are there any other mistakes..? like $post_data?? – TKasai Dec 30 '16 at 10:31
  • I don't know their API documentation, but most likely your key inside the post data shoulr not be `url:` but`url`... – arkascha Dec 30 '16 at 10:35
  • Thanks! It worked! – TKasai Dec 30 '16 at 10:45
  • Please tell me how i can show my gratitude here on stackoverflow to you. – TKasai Dec 30 '16 at 10:47
  • Well, you could accept the answer I posted summarizing the solution found here. But more important is that you give back to the community here on SO! Have fun ;-) – arkascha Dec 30 '16 at 10:53

1 Answers1

1

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.

arkascha
  • 41,620
  • 7
  • 58
  • 90