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

[英]How to make a JSON Curl request to an api url, getting 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:

我需要以下列格式向url提出请求:

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.

http://ratings.api.co。在lang将被设置为en-GB的英国/业务/{lang}/{fhrsid}/{格式}中,fhrsid是一个标识符,格式为json。

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

我尝试了下面的方法,但我只是得到了null作为回报:

$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 个解决方案

#1


2  

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

您目前正在将数据发布到URL,而您说要将数据放入URL本身。

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.

也就是说,现在你提交{“lang: en-GB”,“fhrsid”:80928}到URL http://ratings.api.co。英国/业务/json,但您希望检索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.

不要使用POST作为请求类型,不要指定postfields,不要指定content-length,并将数据放入URL中。

#2


2  

Sending data as arguments is different than sending it within the URL.

将数据作为参数发送不同于在URL中发送数据。

If you require a url format of http://ratings.api.co.uk/{lang}/{fhrsid}/{format}, then you must make your curl_init string match that format.

如果您需要http://ratings.api.co的url格式。uk/{lang}/{fhrsid}/{format},然后您必须使您的curl_init字符串与该格式匹配。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/05/14/f533e5bd9e8ae095d58701d9cafc06bb.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com