Eamonn
Eamonn

Reputation: 1320

Accessing Content sent via stream header context

Using the following:

$auth = array(
    'iAmWhiteListed' => 'me',
    'otherStuff'     => 'mystuff'
);

$sendAuth = array(
    'http' => array(
            'method'    => 'POST',
            'content'   => json_encode( $auth ),
            'header'    => "Content-Type: application/json\r\n" . "Accept: application/json\r\n"
            )
); 

$authContext    = stream_context_create( $sendAuth );
$authResult     = file_get_contents( $url, false, $authContext );

How does a PHP script access the data send in the content of the http request? (In this case to verify the data therein and send back an appropriate response?)

Upvotes: 1

Views: 52

Answers (1)

Eamonn
Eamonn

Reputation: 1320

When using Content-Type: application/json, the $_POST array is not populated. To fill it, I had to grab the input using php://input and decode it back into array format.

$data = file_get_contents("php://input");
$_POST = json_decode($data, true);

Upvotes: 1

Related Questions