2017-01-17 14 views
0

PHPでcURLを使用してjsonデータを送信したいのですが、問題はcURLがデータを投稿していないことです。PHP cUrl投稿していません

注意:cURLが正しくインストールされ、設定されています。

$ch = curl_init($url); 
//The JSON data. 
$jsonData = '{ 
    "recipient":{ 
    "id":"'.$sender.'" 
}, 
"message":{ 
    "text":"'.$message_to_reply.'" 
} 
}'; 


$jsonDataEncoded = $jsonData; 

//Tell cURL that we want to send a POST request. 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
//Attach our encoded JSON string to the POST fields. 
curl_setopt($ch, CURLOPT_POSTFIELDS, array($jsonDataEncoded)); 

//Set the content type to application/json 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
curl_exec($ch); 

JSONデータが正常に動作しているが、cURLのポストは何を掲示せず、また、警告/通知またはエラーのいずれかのタイプを与えていません。

+0

何も投稿していないことをどのように知っていますか?また、jsonはほとんどURLエンコードされていませんが、あなたはそれをしたいですか?最後に、 'CURLOPT_CUSTOMREQUEST'の使用法はアドバイスされていません。単に削除するだけです。また、リダイレクト(POST後には一般的)の後に行うことを検討してください。 –

+0

@DanielStenbergなぜなら、私はngrokを使用しており、投稿に関する統計はないからです。また、私はurlencode()を削除しましたが、まだ動作していません。 –

+0

受信側ではログに記録されていませんが、「何も投稿していません」と同じである必要はありません。 –

答えて

0

これを試してください。

curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, array(json_decode($jsonDataEncoded))); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

おそらく、すべてのデータを1つのキーに渡す必要はありません。 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");をしない、正しい:私の知る限りprint_r(json_decode(array($jsonDataEncoded)))

Array ([0] => stdClass Object ([recipient] => stdClass Object ([id] => me) [message] => stdClass Object ([text] => hello))) 
+0

JSONをポストしないで、マルチパート/フォームデータエンコーディングでデータをポストします(curl_setoptに配列を渡すと自動的にmultipart/form-dataに変換されます) – hanshenrik

+0

jsonを文字列として渡したい場合、コードが機能するはずです。 –

1

の出力print_r(array($jsonDataEncoded))

Array ([0] => { "recipient":{ "id":"me" }, "message":{ "text":"hello" } }) 





出力、あなたは3つのミス

1を行いますPOSTリクエストが必要であることをカールする方法はcurl_setopt($ch, CURLOPT_POST, true);

です

2:あなたはCURLOPT_POSTFIELDSに配列を与えるとき、その実際にあなたが望むものではないmultipart/form-dataエンコーディング、(あなたはJSONを転送したい)

3に変換:あなたの$送信者と$ message_to_replyだけのように見えますjson rawに挿入されます。 $ message_to_replyに"または'が含まれているとどうなりますか?それはjsonを無効にします。

$jsonData = array (
     'recipient' => array (
       'id' => $sender 
     ), 
     'message' => array (
       'text' => $messaage_to_reply 
     ) 
); 
$jsonDataEncoded = json_encode ($jsonData, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); 

のように、json_encodeを使用して、たとえば、適切にそれをコードする検討したが、その$送信者を提供し、$ message_to_replyは、私の知る限り、すでにあなたの元のコードが動作しない唯一の理由は、適切にJSONエンコードされています、あなたはCURLOPT_POSTFIELDSを与えるという配列は、このように、すべてのことは、それはまあ、すべての試行後curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonDataEncoded);

0

のように、その行から「アレイ」を取り除くことであろう修正するために必要なのは、ここに答えがあるさ:

$jsonData = '{ 
"recipient":{ 
    "id":"'.$sender.'" 
}, 
"message":{ 
    "text":"'.$message_to_reply.'" 
} 
}'; 

$jsonDataEncoded = $jsonData; 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded); 
//Here i removed the array// 

//Set the content type to application/json 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
// By default in PHP7 CURL_SSL_VERIFYPEER, is true. You have to make it false// 

$result = curl_exec($ch); 
関連する問題