日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

记-curl post json数据,服务器后端$_POST接收不到数据

發(fā)布時(shí)間:2024/10/12 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 记-curl post json数据,服务器后端$_POST接收不到数据 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

curl端:向某服務(wù)器后端接口返回?cái)?shù)據(jù)

  $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);   #記住這個(gè)設(shè)置向curl地址傳送json數(shù)據(jù) ????????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); 服務(wù)器端:   接收數(shù)據(jù)方式:$data =?json_decode(file_get_contents('php://input'), true);或者$data = json_decode($GLOBALS['HTTP_RAW_POST_DATA'],true)   注:$data = $_POST 這樣,正常的接收方式是接收不到數(shù)據(jù)的   附:     ajax默認(rèn)是以application/x-www-form-urlencoded方式提交。也就是常見的表單提交方式。在PHP中使用$_POST方式可以獲取,但,如果將ajax的請(qǐng)求頭強(qiáng)制指定為 application/json,那么你的?$_POST?就接收不到數(shù)據(jù)了

轉(zhuǎn)載于:https://www.cnblogs.com/leonchan/p/11251192.html

總結(jié)

以上是生活随笔為你收集整理的记-curl post json数据,服务器后端$_POST接收不到数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。