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);

  

posted @ 2019-10-11 16:08  让我们荡起双桨!  阅读(3445)  评论(0编辑  收藏  举报