在使用一个API的时候,文档里写的返回值类型是json,可是试了下返回的明明是xml,还小小的鄙视了一把。

可是解析xml,好麻烦的。最好是json可以直接decode 。

意外看到文档下面有一句

JSON response can be obtained by including the following headers in the request 'response-json' =true

就是讲要在post的Header里加上这么一个参数于是返回json.搞半天不知道怎么加,最后知道了,是这样

function post($url, $data){//file_get_content
   $opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => array ('Content-type: application/x-www-form-urlencoded',  'response-json:true'),
        'content' => $data
    )
    );
    $context = stream_context_create($opts);
    $result = file_get_contents($url, false, $context);
    return $result;
}

 

相关文章:

  • 2022-12-23
  • 2023-02-14
  • 2022-12-23
  • 2022-01-25
  • 2022-12-23
  • 2022-12-23
  • 2021-05-17
  • 2021-11-08
猜你喜欢
  • 2022-12-23
  • 2021-12-27
  • 2022-01-15
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
相关资源
相似解决方案