0

not getting response using 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.

1
  • 1
    For all who, as I have been looking for a solution: curl_setopt ($ ch, CURLOPT_HEADER, 1) ;: Causes header information to be contained in $ response. Thus, $ response is no longer a pure JSON string. That's why this non json_decode can be converted into an Object / Array. (Ergnis is null) My approach: curl_setopt ($ ch, CURLOPT_HEADER, 0); We refrain from returning the headers and receive a pure JSON that can be further processed. Commented Jun 30, 2018 at 9:02

2 Answers 2

2

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); 
Sign up to request clarification or add additional context in comments.

17 Comments

If your code is not working then do one thing try your curl request through terminal. Might be the url which you are using is wrong.
Please share your URL on which you are requesting.
api.nhs.uk/organisations/FNM60 this link get all data using poster and that given response in it. but when i try to using given code that give only 1(one) response.
Do you want to get just that json? from this URL?
I want curl result (JSON) inside variable.
|
0

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.

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

check full code

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.