【问题标题】:Square-Connect Webhook PHP post no dataSquare-Connect Webhook PHP 发布无数据
【发布时间】:2015-05-17 20:20:17
【问题描述】:

我已启用 Square-Connect Webhooks。我正在接收到我的服务器脚本的 POST。然而 $_POST 数组似乎是空的。

这是我的代码:

<?php
  $sRM = $_SERVER['REQUEST_METHOD'] ;
  $sH = null ;
  
  if ( 'PUT' == $sRM )
  {
    $sH = file_get_contents( 'php://input' ) ;
  }
  else if ( 'POST' == $sRM )
  {
    $sS = '' ;
    
    foreach( $_POST as $sK => $sV )
    {
      $sH .= $sK.'=>'.$sV ;
      $sS = ', ' ;
    }
  }
  
  if ( ConnectDB() )
  {
    $_SESSION[ 'DB' ]->real_escape_string( trim( $sH ) ) ;  //  Prevent SQL Injection
    $rR = $_SESSION[ 'DB' ]->query( 'INSERT INTO `Hooks` ( `Method`,`Message` ) VALUES ( "'.$sRM.'", "'.$sH.'" ) ;' ) ;
  }
?>

谢谢, 抢

【问题讨论】:

    标签: webhooks square-connect


    【解决方案1】:

    square docs 声明 POST 正文将采用 JSON 格式,因此 PHP 不会像表单一样解析它并填充 $_POST 数组。你需要做这样的事情:

    $json = file_get_contents('php://input');
    $obj = json_decode($json);
    

    Reading JSON POST using PHP 的回答中所述。

    【讨论】:

    • 我看过那篇关于 file_get_contents( `php://input' );
    • 我看过参考资料...但它说调用将是“PUT”类型。当我看到它是“POST”类型时......然后我处理它期望名称值对。 file_get_contents('php://input');工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多