Square-Connect Webhook PHP post 无数据
Square-Connect Webhook PHP post no data
我已启用 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.'" ) ;' ) ;
}
?>
谢谢,
罗布
square docs 声明 POST 正文将采用 JSON 格式,因此 PHP 不会像表单一样解析它并填充 $_POST数组。你需要做这样的事情:
$json = file_get_contents('php://input');
$obj = json_decode($json);
如 Reading JSON POST using PHP 的答案中所述。
我已启用 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.'" ) ;' ) ;
}
?>
谢谢, 罗布
square docs 声明 POST 正文将采用 JSON 格式,因此 PHP 不会像表单一样解析它并填充 $_POST数组。你需要做这样的事情:
$json = file_get_contents('php://input');
$obj = json_decode($json);
如 Reading JSON POST using PHP 的答案中所述。