php curl 转为 application/x-www-form-urlencoded;charset=UTF-8 方式请求

1、application/x-www-form-urlencoded;charset=UTF-8请求POST方法;;;public function curl_post($url , $data=array()){;;;$headers = array(;;Content-type:application/x-www-form-urlencoded;charset=UTF-

1、application/x-www-form-urlencoded;charset=UTF-8请求POST方法

   public function curl_post($url , $data=array()){
   	$headers = array(
		  "Content-type:application/x-www-form-urlencoded;charset=UTF-8"
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    // POST数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // 把post的变量加上
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
  }

2、注意:

传统方式from-data试发送的数据用的是array格式,而方式为 x-www-form-urlencoded 时需要用key=value&key=value的格式发送,发送的是string型的数据。需要使用http_build_query($data)把数组转为字符串

from-data数据的为:
​​​​​​$data = [
  'name' => 'xiaoming',
  'sex' => 1
];
x-www-form-urlencoded时的数据则要变为
http_build_query($data);

本文标题为:php curl 转为 application/x-www-form-urlencoded;charset=UTF-8 方式请求

基础教程推荐