欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

PHP中CURL发送Request Payload 有大用

今天使用CURL爬一个网页时遇到了请求为Payload的情况,只要更改一下curl配置参数既可:

1、声明Content-type

CURLOPT_HTTPHEADER => array("content-type: application/json"),

2、格式化post数据

$data = array(
    "name" => "jack",
    "age" => 13
);
$postData = json_encode($data);

3、发送请求

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "url",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $postData,),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
               
普通分类: