访问通过流标头上下文发送的内容

时间:2014-01-28 15:56:20

标签: php file-get-contents

使用以下内容:

$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 );

PHP脚本如何访问content请求的http中的数据发送? (在这种情况下,验证其中的数据并发回适当的响应?)

1 个答案:

答案 0 :(得分:1)

使用Content-Type: application/json时,不会填充$ _POST数组。为了填充它,我必须使用php://input获取输入并将其解码回数组格式。

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