請不要在閱讀我的實際問題之前關閉此問題。我看到很多相關的問題,但我仍然有。 只需要在PHP中使用CURL
通過POST
發送json_encoded
數組。當我設置content-type as application/json
時,數據不會發布。我收到空的post
陣列。當我刪除內容類型的行數據發佈,但不是json。 這裏是我的代碼Json加帖子加上curl在PHP中
$data = array('id' => $post['id'], 'action' => $post['action']);
$post_data = http_build_query($data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
我這種情況下,我收到空的帖子。如果我註釋掉這一行curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
,我將收到發佈數據,但未收到json編碼。我想要json_encoded數組。有人會告訴我我做錯了什麼?
問候:
你確定目標資源可以讓你請求的響應格式? – Madbreaks
我正在向其他控制器發送請求。我如何才能知道目標資源是否允許? –