Xitish
Xitish

Reputation: 325

How NOT to display cURL response data in php?

I have the following code that makes an API call to a URL. The response is in json format. When the response data is received, I want to further process the data using another function and NOT display the data in the console (I want to display only the final processed output). But currently, the response from the API call is being displayed in the console along with the final processed output.

The processed final data will be saved in $processedData variable.

<?php

function getDataFromApi(){
    $url = 'https://www.myurl.com/data.json';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

function processData($data){
    /**
     * Do the processing and save processed data in $processedData variable.
     */

    // Finally display the data
    echo $processedData;
}

$result = getDataFromApi();
processData($result);

How do I NOT display the intermediate cURL resplonse but only the final response?

Upvotes: 5

Views: 2172

Answers (3)

Xitish
Xitish

Reputation: 325

I found answer to my question myself so I am posting it here so that it can help others.

My code as stated in the question, by default executes the curl request and prints the output in the screen.

In the current form, the line $result = curl_exec($ch); will execute the cURL request and returns true or false based on whether the request was successful or not. So, the $result variable will NOT hold the response data (as opposed to what I assumed earlier), but either true or false.

In order not to display the response and save it in a variable (and pass it to another function), CURLOPT_RETURNTRANSFER option needs to be set to true as follows:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

This line instructs cURL to Return the response as a string instead of outputting it to the screen.

The final code would be as follows:

<?php

function getDataFromApi(){
    $url = 'https://www.myurl.com/data.json';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

function processData($data){
    /**
     * Do the processing and save processed data in $processedData variable.
     */

    // Finally display the data
    echo $processedData;
}

$result = getDataFromApi();
processData($result);

Upvotes: 5

Prashant Patel
Prashant Patel

Reputation: 272

Use below

function processData($data){
  /** * Do the processing */ 
  // Finally display the data echo $data; 
}

Upvotes: 1

Amit Sharma
Amit Sharma

Reputation: 1795

you are passing data variable but echoing the process data variable

function getDataFromApi(){
   $url = 'https://www.myurl.com/data.json';
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   $result = curl_exec($ch);
   curl_close($ch);
   $processdata = processData($data);
   return $processdata;
}


function processData($data){
  /**
   * Do the processing
   */

  // Finally display the data
  echo $data;
}
echo $getDataFromApi();

Upvotes: 4

Related Questions