PHP 使用 CURL 发送 postman 的raw格式的数据,原来一直使用的是直接发送的是单一格式的字符串,今天对接的外站数据的时候他们要求的是一个对象格式的字符串,也就是postman里的raw格式的数据。
$data = "{
name: 'job',
age: 20,
hobby: [
{
before: 'Play games',
now: 'read book'
}
]
}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
)
);
$response = curl_exec($ch);
$response = json_decode($response);
值得注意的是 header 一定要有 `‘Content-Type: application/json’,没有类型接受端将无法解析。
友情提示:垃圾评论一律封号...