1

I am trying to get asynchronous callback to work for IBM Watson speech to text.

I got the curl function work

curl -X POST -u "c94c7025-09f5-4cee-94dd-8f73348b60d8":"4TOriExZooKh" \
--header "Content-Type: audio/wav" \
--data-binary @uploads/001528fe-9545-4c3f-9d0d-aec4cd61caee.wav \
"https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions?callback_url=http://34.234.215.13/callback.php&model=en-US_NarrowbandModel&profanity_filter=false&events=recognitions.completed_with_results&user_token=audio&results_ttl=10"

From my understanding is that the results or notification should be sent as POST to the call back URL

https://console.bluemix.net/docs/services/speech-to-text/async.html#create

The above code should be sending the completed results over to callback.php

callback.php
<?php
header('Content-Type:text/plain');
echo $_GET["challenge_string"];
foreach ($_POST as $key => $value)
    error_log($key . "--" . $value);

The echo is for the url registration and then for any data sent over POST I am passing it into the foreach loop.

But I am not getting any data or results at all after the conversion is done.

access log
[14/Oct/2017:00:22:39 +0000] "POST /callback.php HTTP/1.1" 200 166 "-" "Jersey/2.22.1 (Apache HttpClient 4.5)"

error log
[:error] [pid 2046] [client 169.48.114.147:54645] PHP Notice:  Undefined index: challenge_string in /var/www/html/callback.php on line 10

I am not able to understand what is wrong? I was able to get it work when I make synchronous calls ie one POST after another.

kelunik
  • 6,750
  • 2
  • 41
  • 70
Abilash Amarasekaran
  • 817
  • 2
  • 10
  • 26

2 Answers2

0

I believe you are not registering (whitelisting) your callback, please see the section "Registering a callback URL" within the documentation: https://console.bluemix.net/docs/services/speech-to-text/async.html#async

You register a callback URL by calling the POST /v1/register_callback method. Once you register a callback URL, you can use it to receive notifications for an indefinite number of jobs. The registration process comprises four steps: ...

Daniel Bolanos
  • 770
  • 3
  • 6
  • I followed the steps and have it registered. `root@ip-10-0-0-149:/home/ubuntu# curl -X POST -u "username":"password" \ > "https://stream.watsonplatform.net/speech-to-text/api/v1/register_callback?callback_url=http://xxx/callback.php" { "url": "http://xxxx/callback.php", "status": "already created" } root@ip-10-0-0-149:/home/ubuntu#` – Abilash Amarasekaran Oct 16 '17 at 19:54
  • and still I am not getting any POST info – Abilash Amarasekaran Oct 16 '17 at 20:10
  • good, it seems like you registered the callback endpoint successfully. Can you please post the curl command you used to create a job for which you want to be notified via callback? can you post the your php logs? – Daniel Bolanos Oct 17 '17 at 00:16
  • Sure here is the curl command. `curl -X POST -u "username":"paswword" \ --header "Content-Type: audio/wav" \ --data-binary @uploads/001528fe-9545-4c3f-9d0d-aec4cd61caee.wav \ "https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions?callback_url=http://xxxx/callback.php/results&model=en-US_NarrowbandModel&profanity_filter=false&events=recognitions.completed_with_results&user_token=audio&results_ttl=10"` – Abilash Amarasekaran Oct 17 '17 at 00:43
  • I looked at the logs, the notification was indeed sent to you and a 200 code was received from your server. Please check your logs. `notification sent: {"id":"47bc8860-XXXXXXXXXXX92dd9afa","user_token":"audio","event":"recognitions.completed"} status code received: 200 job finished: 47bc8860-b2XXXXX99e192dd9afa` – Daniel Bolanos Oct 17 '17 at 01:25
  • I love you just for logs. The jobs are working properly and being completed, what I did not get was any data into POST or `$_POST`. I think I figured out how to get the data from an different search. `$request_body = file_get_contents("php://input"); $data = array(); parse_str($request_body, $data); var_dump($data);` I am not an expert in PHP and the above concept is new to me. Also I am puzzled as to how is CallBack suppose to work? Is it suppose to send the results over POST into `$_POST` or stream to `php://input` – Abilash Amarasekaran Oct 17 '17 at 02:28
  • the callback is just a `POST` to your endpoint, you need to be able to handle that request, nothing special about it. – Daniel Bolanos Oct 18 '17 at 13:53
0

I figured out how to handle the POST request. This is completely new to me and did not know about it.

callback.php
<?php
$request_body = file_get_contents('php://input');
$json = json_decode($request_body);

One thing I would like to point out to any one interested is that this output will have extra info as mentioned here

https://console.bluemix.net/docs/services/speech-to-text/async.html#job

If you want to extract only the results into JSON string to store in DB or anywhere else you can add use this line

json_encode($json->results[0])

Hope this helps any one.

More info about PHP handling POST data in body can be found here
How to get body of a POST in php?

Abilash Amarasekaran
  • 817
  • 2
  • 10
  • 26