jignesh prajapati
jignesh prajapati

Reputation: 77

Curl not json response in php

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.

Upvotes: 0

Views: 1959

Answers (2)

jignesh prajapati
jignesh prajapati

Reputation: 77

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

Upvotes: 0

Sahil Gulati
Sahil Gulati

Reputation: 15141

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);

Upvotes: 2

Related Questions