简体   繁体   English

在PHP中卷曲不是JSON响应

[英]Curl not json response in php

not getting response using curl. 无法使用curl获得响应。 I have put all solution but did not get response. 我已提出所有解决方案,但没有得到回应。

$ch = curl_init();
$header = array('api_key:xxxxxxxxxxxxxxxx','Content-Type: application/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

if($postdata!=""){
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}

$response = curl_exec($ch);


curl_close($ch);
$result = json_decode($response,true);  
print_r($result); // not display result

this example not displaying any result but it send to specific place. 本示例不显示任何结果,但将其发送到特定位置。

Add this in your code and then check. 将此添加到您的代码,然后检查。

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

For your used-case you have to change your code. 对于您的用例,您必须更改代码。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.nhs.uk/organisations/FNM60");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

curl_close($ch);
$result = json_decode($response,true);  
print_r($result);

here we are getting response without echo on the top of the page but we don't need to that type of out put so we have disabled using span tag. 在这里,我们在页面顶部得到没有回声的响应,但是我们不需要那种输出,所以我们已经禁用了span标签。

echo "<span style='display:none;'>"; //to hide the curl response
$ch = curl_init();
$header = array('api_key:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx','Content-Type: application/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($postdata!=""){
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$responseBody = json_decode($response);
print_r($responseBody); 
echo $response;  // display 1(one)
curl_close($ch);
echo "</span>";

and result is : 1 结果是:1

check full code 查看完整代码

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

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