记-curl post json数据,服务器后端$_POST接收不到数据
生活随笔
收集整理的這篇文章主要介紹了
记-curl post json数据,服务器后端$_POST接收不到数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
curl端:向某服務器后端接口返回數據
$url = "http://xxx.com/getdataFile.php";
?$curl = curl_init();?
????????curl_setopt($curl, CURLOPT_URL, $url); ????????curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); ????????curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); ????????if(!$data){ ? ? ? ? ? ? die();????????} ????????if(is_array($data)) ????????{ ????????????$data = json_encode($data); ????????} ????????curl_setopt($curl, CURLOPT_POST, 1); ????????curl_setopt($curl, CURLOPT_POSTFIELDS, $data); ????????curl_setopt($curl, CURLOPT_HEADER, 0); #記住這個設置向curl地址傳送json數據 ????????curl_setopt($curl, CURLOPT_HTTPHEADER,array( ????????????????'Content-Type: application/json; charset=utf-8', ????????????????'Content-Length:' . strlen($data), ????????????????'Cache-Control: no-cache', ????????????????'Pragma: no-cache' ????????)); ????????curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); ????????$res = curl_exec($curl); ????????$errorno = curl_errno($curl); ????????if ($errorno) { ? ? ? ? ? ? echo?$errorno; ????????} ????????curl_close($curl); 服務器端: 接收數據方式:$data =?json_decode(file_get_contents('php://input'), true);或者$data = json_decode($GLOBALS['HTTP_RAW_POST_DATA'],true) 注:$data = $_POST 這樣,正常的接收方式是接收不到數據的 附: ajax默認是以application/x-www-form-urlencoded方式提交。也就是常見的表單提交方式。在PHP中使用$_POST方式可以獲取,但,如果將ajax的請求頭強制指定為 application/json,那么你的?$_POST?就接收不到數據了
轉載于:https://www.cnblogs.com/leonchan/p/11251192.html
總結
以上是生活随笔為你收集整理的记-curl post json数据,服务器后端$_POST接收不到数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全合成机油品牌有哪些?韦尔斯的怎么样?
- 下一篇: 开发环境搭建教程