11

POSTJSON の形式でデータを受信しようとしています。次のようにカールしています。

curl -v --header 'content-type:application/json' -X POST --data '{"content":"test content","friends":[\"38383\",\"38282\",\"38389\"],"newFriends":0,"expires":"5-20-2013","region":"35-28"}' http://testserver.com/wg/create.php?action=post

PHP 側のコードは次のとおりです。

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

    $content    = $data->{'content'};
    $friends    = $data->{'friends'};       // JSON array of FB IDs
    $newFriends = $data->{'newFriends'};
    $expires    = $data->{'expires'};
    $region     = $data->{'region'};    

しかし、私はprint_r ( $data)何も返されません。POSTこれは、フォームなしでAを処理する正しい方法ですか?

4

1 に答える 1

26

送信している JSON データは有効な JSON ではありません。

シェルで ' を使用すると、想定どおり \" を処理しません。

curl -v --header 'content-type:application/json' -X POST --data '{"content":"test content","friends": ["38383","38282","38389"],"newFriends":0,"expires":"5-20-2013","region":"35-28"}'

期待どおりに動作します。

<?php
$foo = file_get_contents("php://input");

var_dump(json_decode($foo, true));
?>

出力:

array(5) {
  ["content"]=>
  string(12) "test content"
  ["friends"]=>
  array(3) {
    [0]=>
    string(5) "38383"
    [1]=>
    string(5) "38282"
    [2]=>
    string(5) "38389"
  }
  ["newFriends"]=>
  int(0)
  ["expires"]=>
  string(9) "5-20-2013"
  ["region"]=>
  string(5) "35-28"
}
于 2013-02-16T20:47:20.227 に答える