【发布时间】:2017-09-15 15:35:34
【问题描述】:
我有这种方法,我试图用POST 发送POST 请求正文:
public function executePost($url, $theBody, $headers){
$data_string = '{"apple": "fruit", "turnip": "vegetable"}'; // hard coded for now
\Log::info($data_string);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($headers, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
));
$result = curl_exec($ch);
\Log::info($result);
}
在接收端我没有得到我做的数据:
\Log::info(\Input::all());
我什么也没得到,即一个空数组。我似乎无法弄清楚这有什么问题。
它可以在另一台使用 WAMP 的计算机上运行,我使用的是 Ubuntu 16.04 和 PHP 5.6。
【问题讨论】:
-
能否告诉我们接收端代码?
-
尝试在接收端获取数据 $_POST = json_decode(file_get_contents("php://input"), true );和 print_r($_POST);它和 var_dump($result);
-
试过了,不行。
-
所以你首先要发送的$data_string请检查一下,这是不是一个有效的jsonjsonformatter.curiousconcept.com