以下のように、Content-TypeをJSONにして、JSONを本文として送信する。
Content-Type:application/json {key1=value1,key2=value2}
<?php $json_string = file_get_contents('php://input'); $obj = json_decode($json_string); var_dump($obj);
php://input は読み込み専用のストリームで、 リクエストの body 部から生のデータを読み込みます
<?php $context = stream_context_create( array( 'http' => array( 'method'=> 'POST', 'header'=> 'Content-type: application/json; charset=UTF-8', 'content' => json_encode( array( 'key1' => 'Value1', 'key2' => 'Value2' ) ) ) ) ); file_get_contents('http://post.example.com/api/sample', false, $context);
$ curl -X POST http://post.example.com/api/sample -H 'Content-Type: application/json' -d @json_file_name $ curl -X POST http://post.example.com/api/sample -H 'Content-Type: application/json' -d '{"postdata":{"key1":"Value1","key2":"Value2"}}'