PHP中使用raw格式发送POST请求

如果请求的参数格式是原生(raw)的内容,应该如何为程序构造一个POST请求函数呢?

function http_post($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-AjaxPro-Method:ShowList',
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
注意看在curl_setopt()函数中的CURLOPT_HTTPHEADER设置。既然是原生格式,那么POST传输的请求参数($data_string)可以是任何数据格式,但一般习惯使用json字符串格式。
————————————————
版权声明:本文为CSDN博主「dengwenjieyear」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dengwenjieyear/article/details/79468849

原文地址:https://www.cnblogs.com/langgezuishuai/p/11933260.html