php curl 转为 x-www-form-urlencoded 方式的坑

网上转变的方法基本都是写添加下面这句:

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

但加上去后却根本没效果。

要想以 x-www-form-urlencoded 方式发送,最关键其实是发送的数据格式。

方式from-data试发送的数据用的是array格式,而方式为 x-www-form-urlencoded 时需要用key=value&key=value的格式发送,发送的是string型的数据。

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

猜你喜欢

转载自blog.csdn.net/qq_35641923/article/details/81386708