1
votes

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.

2

2 Answers

0
votes

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: ...

0
votes

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?