2015-05-14 96 views
0

我有Square-Connect Webhooks endabled。我正在接收POST到我的服務器腳本。然而,$ _POST數組似乎是空的。Square-Connect Webhook PHP post no data

這裏是我的代碼:

<?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.'") ;') ; 
 
    } 
 
?>

感謝, 羅布

回答

1

square docs狀態的POST體將是JSON格式,因此PHP不會解析它像一個表單並填充$ _POST數組。你需要這樣做:

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

如在Reading JSON POST using PHP的答案中所述。

+0

我已經看過關於file_get_contents('php:// input')的那一段; –

+0

我已經看過參考文獻......但它表示該調用將是「PUT」類型。當我看到它是「POST」類型的時候......我然後處理它,期待名稱值對。 file_get_contents('php:// input');工作。 謝謝! –