【问题标题】:How to use callback functions in PHP如何在 PHP 中使用回调函数
【发布时间】:2021-03-20 02:44:46
【问题描述】:

我正在使用应用程序的 PHP API。来自应用程序的文档,回调函数(https://www.digisigner.com/esignature-api/esignature-api-documentation

If you need to be informed about the completion of the task 
request, you can register for an event notification. To accomplish 
this, a callback URL must be specified in your account settings.

The callback URL will be automatically called by the app when the 
task request has been completed, that is, when all the documents 
have been processed.

文档还在继续:

The app expects the response code 200 from your callback code, and 
the response text should be ‘EVENT_ACCEPTED’

我已经为事件通知注册了回调 URL(例如:http://my_project/callback.php)

我的问题:

  1. 我的 PHP 回调函数如何在任务完成后获取应用程序发送的信息?

  2. 我的 PHP 回调函数如何返回代码 200 和响应文本?

感谢您的帮助。

【问题讨论】:

  • 如果存在,它应该返回 200。你会假设在 $_POST 中传递了数据,它应该记录在 API 代码中的某个地方
  • 不要将回调 URL 与 callback function 混淆。 API 文档只是说你可以公开一个脚本来接收通知。

标签: php


【解决方案1】:

我不太确定您认为 API 需要什么作为回调。

但就响应代码和文本而言,它很可能需要标头信息。在您的回调文件中,将其插入代码开头的 之后

header($_SERVER["SERVER_PROTOCOL"]." 200 EVENT_ACCEPTED");

我使用了 $_SERVER 全局变量,因为这样更安全。另一种方法是明确的 HTTP 版本,例如:

header("HTTP/1.1 200 EVENT_ACCEPTED");

【讨论】:

    【解决方案2】:

    基本上你的callback.php应该像下一个:

    <?php
    // here can be $_GET or $_POST, depend by API that call the callback
    $parameters = $_REQUEST;
    
    /*
    
    Do what you need with callback parameters
    I mean you need to log received data in some way
    and return right response
    
    */
    
    die('DIGISIGNER_EVENT_ACCEPTED');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 2017-07-29
      相关资源
      最近更新 更多