0

ローカルサーバーにPHPでJSONデータを投稿しようとしています。以下のコードがありますが、機能していません。私は重要なことを逃しましたか?

    $url = 'http://localhost/mmcv/chart1.php';

    //open connection
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,$url);        
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/json'));  


    $result = curl_exec($ch);
4

2 に答える 2

1
curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/json'));

jsonを投稿している場合でも、URLエンコードされた形式でデータを送信する必要があるため、次のように送信します。

curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/x-www-form-urlencoded'));

と使用

curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

それ以外の

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
于 2013-02-14T14:15:28.937 に答える
1

たぶんあなたの問題は受信スクリプトにあります。「mmcv/chart1.php」で次のことを試してください。

$rawInput = file_get_contents('php://input');

if(is_string($rawInput)) {
    if(!is_null($jsonInput = json_decode($rawInput, true)))
        $_POST = $jsonInput;
}
于 2013-02-14T14:44:16.400 に答える