【问题标题】:How to handle IBM Watson asynchronous callback events in php如何在 php 中处理 IBM Watson 异步回调事件
【发布时间】:2017-10-19 10:20:32
【问题描述】:

我正在尝试让异步回调为 IBM Watson 语音转文本工作。

我得到了 curl 函数的工作

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"

据我了解,结果或通知应作为 POST 发送到回调 URL

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

上面的代码应该将完成的结果发送到callback.php

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

回显用于 url 注册,然后用于通过 POST 发送的任何数据,我将其传递到 foreach 循环中。

但是转换完成后我根本没有得到任何数据或结果。

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

我无法理解出了什么问题?当我进行同步调用时,我能够让它工作,即一个又一个 POST。

【问题讨论】:

    标签: php speech-to-text ibm-watson


    【解决方案1】:

    我认为您没有注册(白名单)您的回调,请参阅文档中的“注册回调 URL”部分:https://console.bluemix.net/docs/services/speech-to-text/async.html#async

    您通过调用POST /v1/register_callback 方法注册回调URL。注册回调 URL 后,您可以使用它来接收无限数量的作业的通知。注册过程包括四个步骤: ...

    【讨论】:

    • 我按照步骤进行了注册。 root@ip-10-0-0-149:/home/ubuntu# curl -X POST -u "username":"password" \ &gt; "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#
    • 我仍然没有收到任何 POST 信息
    • 好的,看来你注册回调端点成功了。您能否发布用于创建要通过回调通知的作业的 curl 命令?你能发布你的php日志吗?
    • 当然这里是 curl 命令。 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&amp;model=en-US_NarrowbandModel&amp;profanity_filter=false&amp;events=recognitions.completed_with_results&amp;user_token=audio&amp;results_ttl=10"
    • 我查看了日志,确实向您发送了通知,并且从您的服务器收到了 200 代码。请检查您的日志。 notification sent: {"id":"47bc8860-XXXXXXXXXXX92dd9afa","user_token":"audio","event":"recognitions.completed"} status code received: 200 job finished: 47bc8860-b2XXXXX99e192dd9afa
    【解决方案2】:

    我想出了如何处理 POST 请求。这对我来说是全新的,并不知道。

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

    我想向任何感兴趣的人指出一件事,此输出将包含此处提到的额外信息

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

    如果您只想将结果提取到 JSON 字符串中以存储在数据库或其他任何可以添加的位置,请使用此行

    json_encode($json->results[0])
    

    希望这对任何人都有帮助。

    更多关于 PHP 在 body 中处理 POST 数据的信息可以在这里找到
    How to get body of a POST in php?

    【讨论】:

    • php://input 很少需要,在您的情况下也不需要。改用标准输入
    • 我会试试的。它有什么区别?我会影响未来的请求还是多个请求?
    猜你喜欢
    • 2021-04-16
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多