0

I have Square-Connect Webhooks endabled. I am receiving POST's to my server script. Yet the $_POST array seems to be empty.

Here is my code:

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

Thanks, Rob

1 Answers1

3

The square docs state that the POST body will be in JSON format, so PHP will not parse it like a form and populate the $_POST array. You need to do something like this:

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

as described in the answers to Reading JSON POST using PHP.

Community
  • 1
  • 1
Troy
  • 1,599
  • 14
  • 28
  • I had seen that piece about file_get_contents( `php://input' ); – IT Serenity May 17 '15 at 12:44
  • I had seen the reference... but it stated that the call would be of type "PUT". When I saw that it was of type "POST"... I then processed it expecting name value pairs. The file_get_contents('php://input'); worked. Thank you! – IT Serenity May 17 '15 at 13:19