一位被互联网耽误的文艺青年

有人说,生命是晚霞衬红日,青松立峭壁,万里黄沙落秋雁,三月阳春映白雪,是金刀宝马,是银貂美酒,是皓月清歌的漂泊……

一个curl请求 服务器端接收不到数据的解决方案

一个curl请求 服务器端接收不到数据的解决方案

真是奇了怪了,这么简单的一个请求,在服务器端就是接收不到数据($_POST)

 

最后定义了header application/json,服务器端用php://input,才成功接收到数据。

 

示例:

客户端:

//获取发送数据数组
function getDataArray($MsgArray)
{
$data = array(
‘time’ => $MsgArray[“time”],
‘orderid’ => ‘Xs211008A10136’,
‘custom’ => ‘金鹏汽修’,
‘custom_phone’ => ‘13112345678’,
‘custom_remark’ => ‘vip001’,
‘amount’ => ‘2’,
‘money’ => ‘955.00’,
‘extract’ => ‘发货’,
‘receipt’ => ‘普通票’,
‘payment’ => ‘现金’,
‘order_data’ =>array(
array(
‘parts_code’ => ‘2218690323’,
‘parts_name’ => ‘中网’,
‘parts_brand’ => ‘奔驰’,
‘parts_car’ => ‘204’,
‘parts_num’ => ‘1’,
‘parts_price’ => ‘360.00’,
),
array(
‘parts_code’ => ‘2218690323’,
‘parts_name’ => ‘中网’,
‘parts_brand’ => ‘奔驰’,
‘parts_car’ => ‘204’,
‘parts_num’ => ‘1’,
‘parts_price’ => ‘360.00’,
)
)
);
return $data;
}

//curl请求函数
function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_HTTPHEADER,array( //最后就是定义了header application/json,才测试成功。
‘Content-Type: application/json; charset=utf-8’,
‘Content-Length:’ . strlen($data),
‘Cache-Control: no-cache’,
‘Pragma: no-cache’
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}

$json_data = json_encode(getDataArray($MsgArray));
$res = https_request($url, urldecode($json_data));//请求开始

 

服务端:

$data=json_decode(file_get_contents(‘php://input’), true);

oldmyth