简体   繁体   English

如何向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: 我需要以下列格式向网址发出请求:

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.uk/business/ {lang} / {fhrsid} / {format}其中lang将设置为en-GB,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 任何帮助感激不尽

You are currently posting the data to the URL, while you say you want to put the data in the URL itself. 您当前正在将数据发布到URL,而您表示要将数据放入URL本身。

Ie 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.uk/business/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中。

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.uk/{lang}/{fhrsid}/{format} ,则必须使您的curl_init字符串与该格式匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM