dqdpz60048 2012-05-14 14:41
浏览 242
已采纳

如何向api url发出JSON Curl请求,获取null

This should be simple I think but I just can't fathom it.

I need to make a request to a url in the following format:

http://ratings.api.co.uk/business/{lang}/{fhrsid}/{format} where lang will be set to en-GB the fhrsid is an identifier and format is json.

I tried the following but I am just getting null in return:

$data = array("lang" => "en-GB", "fhrsid" => "80928");                                                                    
$data_string = json_encode($data);            


$ch = curl_init('http://ratings.api.co.uk/business/json');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))                                                 );                                                                                                                   

$Jsonresult = curl_exec($ch);
curl_close($ch);

var_dump(json_decode($Jsonresult));

Any help gratefully received

  • 写回答

2条回答 默认 最新

  • dotxxh0998 2012-05-14 14:46
    关注

    You are currently posting the data to the URL, while you say you want to put the data in the URL itself.

    I.e. now you submit {"lang:"en-GB","fhrsid":80928} to the URL http://ratings.api.co.uk/business/json, but instead you want to retrieve the URL http://ratings.api.co.uk/business/en-GB/80928/json.

    Don't use POST as request type, don't specify postfields, don't specify content-length, and do put the data in your URL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?