hellosheikh
hellosheikh

Reputation: 3015

PHP Curl, Request data accept in a variable

Hello I am having a very weird problem. I am writing this code to get the data from the koncat api

$url = 'https://api.kontakt.io/beacon/?device';
$privKey = 'key here';

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);

curl_setopt($cURL, CURLOPT_HTTPHEADER, array("Api-Key: ".$privKey,
              "Accept: application/vnd.com.kontakt+json;version=5",
          "Content-Type: application/x-www-form-urlencoded"));



$result = curl_exec($cURL);

The problem is Data is successfully printing on the screen but I don't know which variable here is printing out the data. I didn't use echo command in my code. If I print out the result variable it prints out only '1' meaning success.

Upvotes: 1

Views: 260

Answers (1)

Charlotte Dunois
Charlotte Dunois

Reputation: 4680

The php documentation about curl_exec says that it will return true or fail on failure. curl_exec will also print the result directly into the output.

If you don't want that, you need to set the curl option CURLOPT_RETURNTRANSFER to 1. This will tell curl_exec to return the result on sucess.

Documentation about CURLOPT_RETURNTRANSFER:

CURLOPT_RETURNTRANSFER  TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.   

More information: http://php.net/curl_exec http://php.net/curl_setopt

Upvotes: 1

Related Questions