【问题标题】:Process server callback data in PHP在 PHP 中处理服务器回调数据
【发布时间】:2013-10-09 12:43:09
【问题描述】:

我在处理从我用于我的应用程序 (http://Cameratag.com) 的服务发送的回调时遇到问题。

我只是想获取从 Cameratag 发送的数据并用它来处理。

下面是我正在尝试开始工作的“测试”脚本。但是我很难找出什么不起作用。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Process_video_test extends CI_Controller {
function __construct()
 { 
  parent::__construct();
  $this->load->library('email');

 }

public function index()
 {
    $raw = file_get_contents('php://input');
    $json = json_decode($raw, true);

    $uuid = $json["uuid"];

    $this->email->from('Senders@email.com', 'Senders Name');
    $this->email->to('Recievers@email.com'); 
    $this->email->subject($uuid);
    $this->email->message($uuid);   
    $this->email->send(); 

 }
}

当我将数据重新发送到此脚本时,没有任何反应。即使您访问或卷曲此脚本的 url,它也会发送一封空白电子邮件。当我重新发送回调时,我什么也得不到。

回调服务器肯定也在 POST 数据,我可以在我的服务器日志中找到以下内容:

54.224.22.73 - - [09/Oct/2013:07:07:50 -0500] "POST /process_video_test?uuid=59fbe950-0150-0131-7bd6-22000a8fabd6&camera_uuid=732c58e0-ea3f-0130-73d4-22000aecbdd8&created_at=2013-09-16%2022%3A50%3A47%20UTC&percent_complete=100&short_code=eMOhvg&metadata[email]=Coeyman%40gmail.com&state=published&recorded_from=http%3A%2F%2Fjordan.crowdengage.com%2F&publish_type=webcam&formats[][name]=vga&formats[][width]=640&formats[][height]=480&formats[][video_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2Fvga%2Fmp4&formats[][mp4_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2Fvga%2Fmp4&formats[][webm_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2Fvga%2Fwebm&formats[][thumbnail_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2Fvga%2Fthumb&formats[][small_thumbnail_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2Fvga%2Fsmall_thumb&formats[][state]=COMPLETED&formats[][completed_at]=2013-09-16%2022%3A51%3A57%20UTC&formats[][name]=360p&formats[][width]=480&formats[][height]=360&formats[][video_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2F360p%2Fmp4&formats[][mp4_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2F360p%2Fmp4&formats[][webm_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2F360p%2Fwebm&formats[][thumbnail_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2F360p%2Fthumb&formats[][small_thumbnail_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2F360p%2Fsmall_thumb&formats[][state]=COMPLETED&formats[][completed_at]=2013-09-16%2022%3A51%3A58%20UTC&formats[][name]=qvga&formats[][width]=320&formats[][height]=240&formats[][video_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2Fqvga%2Fmp4&formats[][mp4_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2Fqvga%2Fmp4&formats[][webm_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2Fqvga%2Fwebm&formats[][thumbnail_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2Fqvga%2Fthumb&formats[][small_thumbnail_url]=http%3A%2F%2Fcameratag.com%2Fvideos%2F59fbe950-0150-0131-7bd6-22000a8fabd6%2Fqvga%2Fsmall_thumb&formats[][state]=COMPLETED&formats[][completed_at]=2013-09-16%2022%3A51%3A56%20UTC HTTP/1.1" 403 18156 "-" "-"

编辑:

我尝试了以下代码作为我的索引文件:

类 Process_video_test 扩展 CI_Controller {

public function index() 
 {
    $debug_me = 1;

    $raw = file_get_contents('php://input');
    $json = json_decode($raw, true);

    if ($debug_me) {
        ob_start();
        var_dump('raw:', $raw, 'json:', $json);
        log_message('info', ob_get_contents());
        ob_end_clean();
    }
}

在我的日志文件中如下:

DEBUG - 2013-10-09 10:01:12 --> Config Class Initialized
DEBUG - 2013-10-09 10:01:12 --> Hooks Class Initialized
DEBUG - 2013-10-09 10:01:12 --> Utf8 Class Initialized
DEBUG - 2013-10-09 10:01:12 --> UTF-8 Support Enabled
DEBUG - 2013-10-09 10:01:12 --> URI Class Initialized
DEBUG - 2013-10-09 10:01:12 --> Router Class Initialized
DEBUG - 2013-10-09 10:01:12 --> No URI present. Default controller set.
DEBUG - 2013-10-09 10:01:12 --> Output Class Initialized
DEBUG - 2013-10-09 10:01:12 --> Security Class Initialized
DEBUG - 2013-10-09 10:01:12 --> Input Class Initialized
DEBUG - 2013-10-09 10:01:12 --> Global POST and COOKIE data sanitized
DEBUG - 2013-10-09 10:01:12 --> Language Class Initialized
DEBUG - 2013-10-09 10:01:12 --> Loader Class Initialized
DEBUG - 2013-10-09 10:01:12 --> Helper loaded: utility_helper
DEBUG - 2013-10-09 10:01:12 --> Helper loaded: url_helper
DEBUG - 2013-10-09 10:01:12 --> Helper loaded: menu_helper
DEBUG - 2013-10-09 10:01:13 --> Database Driver Class Initialized
DEBUG - 2013-10-09 10:01:13 --> Config file loaded: application/config/ion_auth.php
DEBUG - 2013-10-09 10:01:13 --> Email Class Initialized
DEBUG - 2013-10-09 10:01:13 --> Language file loaded: language/english/ion_auth_lang.php
DEBUG - 2013-10-09 10:01:13 --> Helper loaded: cookie_helper
DEBUG - 2013-10-09 10:01:13 --> Helper loaded: language_helper
DEBUG - 2013-10-09 10:01:13 --> Session Class Initialized
DEBUG - 2013-10-09 10:01:13 --> Helper loaded: string_helper
DEBUG - 2013-10-09 10:01:13 --> A session cookie was not found.
DEBUG - 2013-10-09 10:01:13 --> Session routines successfully run
DEBUG - 2013-10-09 10:01:13 --> Model Class Initialized
DEBUG - 2013-10-09 10:01:13 --> Model Class Initialized
DEBUG - 2013-10-09 10:01:13 --> Helper loaded: date_helper
DEBUG - 2013-10-09 10:01:13 --> REST Class Initialized
DEBUG - 2013-10-09 10:01:13 --> cURL Class Initialized
DEBUG - 2013-10-09 10:01:13 --> Controller Class Initialized
DEBUG - 2013-10-09 10:01:13 --> File loaded: application/views/includes/header.php
DEBUG - 2013-10-09 10:01:13 --> File loaded: application/views/landing.php
DEBUG - 2013-10-09 10:01:13 --> File loaded: application/views/includes/footer.php
DEBUG - 2013-10-09 10:01:13 --> File loaded: application/views/includes/template.php
DEBUG - 2013-10-09 10:01:13 --> Final output sent to browser
DEBUG - 2013-10-09 10:01:13 --> Total execution time: 0.3939

【问题讨论】:

  • 您可以var_dump($raw)var_dump($json) 看看数据是否正确。还。根据服务器的日志记录,我看到 uuid 是 GET 变量而不是 POST。
  • 当然,也许我错了,但是使用默认设置 Apache 不会在日志中显示 POST 数据。因此,如果您在此处看到 uuid,则为 GET['uuid']。
  • 我不知道如何使用var_dump,我通过浏览器看不到页面来查看结果。我试过$uuid = $_GET["uuid"];,没有任何变化。

标签: php html json codeigniter


【解决方案1】:

第 0 步。确保您的课程被调用

我从日志中看到:DEBUG - 2013-10-09 10:01:12 --&gt; No URI present. Default controller set.。这意味着Process_video_test 没有加载运行。另外,刚刚从服务器的日志中看到:*.*.*.* - - [09/Oct/2013:07:07:50 -0500] "POST /process_video_test?*** HTTP/1.1" 403 18156 "-" "-"。这意味着,您的第 3 方收到了 HTTP 错误 403 Forbidden。看起来您的服务器返回 403 错误并且不加载 /process_video_test URL

步骤 1. 启用日志记录

打开application/config/config.php 文件并将log_threshold 参数设置为3(信息性消息):

$config['log_threshold'] = 3;

确保将log_path 参数设置为'' 并检查默认情况下您的日志文件存储在哪里(只需阅读config.phplog_path 上方的注释,该注释以“错误记录目录路径”为首) .

$config['log_path'] = '';

例如,在 CodeIgniter 2.1.4 中,默认日志文件位置是application/logs/

第 2 步。捕获变量值

为了发现问题所在,您可以记录一些调试信息并进行检查。例如,您可以稍微重写您的index()

public function index() {
    $debug_me = 1;

    $raw = file_get_contents('php://input');
    $json = json_decode($raw, true);

    if ($debug_me) {
        ob_start();
        var_dump('raw:', $raw, 'json:', $json);
        log_message('info', ob_get_contents());
        ob_end_clean();
    }

    //$uuid = $json["uuid"];

    //$this->email->from('Senders@email.com', 'Senders Name');
    //$this->email->to('Recievers@email.com'); 
    //$this->email->subject($uuid);
    //$this->email->message($uuid); 
    //$this->email->send(); 

}

打开日志文件。现在您可以检查 $raw$json 变量中的内容。这不是最后一步。如果您要调试,请使用记录的调试信息更新您的问题,以便我们尝试找出您的脚本有什么问题。

待续……

【讨论】:

  • 我将我的索引函数修改为您的代码,并从第 3 方服务运行服务器回调。但我在任何地方都没有 system/logs 文件夹。
  • 我已经更新了答案。另外,当我告诉system/logs 时,我认为我错了。也更新了这个。
  • 好的,你说的我都做了。我已经更新了原始问题以显示日志文件。我没有看到任何具有 $raw 或 $json 变量的东西?
  • Process_video_test 调用了吗?正如我从日志中看到的那样,它没有被调用。
  • 是的,它被调用了。我可以通过查看我的服务器日志来确认这一点
【解决方案2】:

建议你看看这个Link。尝试记录您的错误。

如果您要发布数据,那么为什么要使用$raw = file_get_contents('php://input');?只需像这样从post 获取数据:

$uuid = $this->input->post('uuid') //or $this->input->get('uuid'); try both once.

【讨论】:

  • 我为我令人困惑的术语道歉:我没有自己发布数据。它是从 3rd 方服务中发送的。
猜你喜欢
  • 2019-11-25
  • 2014-11-25
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 2014-10-06
相关资源
最近更新 更多