json_decode 返回值为null

json_decode返回值为null有很多种情况

1.返回的json 有bom头

2.返回的json最后有逗号

3.json格式不正确

等 

这些解决方案网上都有不一一阐述解决方案

但是我发现了另一种 导致 为null的 原因

使用

$curl = curl_init();
curl_setopt($curl,CURLOPT_CUSTOMREQUEST,$method);
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl,CURLOPT_FAILONERROR,false);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_HEADER,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$bodys);
$a = curl_exec($curl);


获取到的json用postman打印很正常 但是 使用json_decode 就一直 返回null 然后一直整了一下午 使用各种网上的方法都不行

json_decode 返回值为null

这个时候我点了转化成html格式  却发现  json的字符串前 加了 返回头

json_decode 返回值为null


检查代码之后才发现 CURLOPT_HEADER 的参数设置为了true

json_decode 返回值为null

解决方案:

将该参数设置为false即可 json_decode 成功