I want to have a standalone PHP class where I want to have a function which calls an API through cURL and gets the response. Can someone help me in this?
Thanks.
PHP cURL GET request $get_data = callAPI('GET', 'https://api.example.com/get_url/'.$user['User']['customer_id'], false); $response = json_decode($get_data, true); $errors = $response['response']['errors']; $data = $response['response']['data'][0];
Functions of cURL in PHP curl_error — It will return the string which represents the error for the particular current session.
We can use curl -v or curl -verbose to display the request headers and response headers in the cURL command. The > lines are request headers .
1 Obtaining headers as key : value pairs. As discussed in the article on parsing response headers in PHP, we can also create an associative array containing key : value pairs. 2 Retrieving the response headers. There is no build-in way to only return the response headers using cURL in PHP. ... 3 Create an array containing each header. ...
This is a short PHP tutorial on how to return cURL output as a string. As you probably already know, the default behavior of cURL is to simply print the response out onto the page. However, there is a quick fix for this. The CURLOPT_RETURNTRANSFER option. The curl_setopt function allows you to configure various options for a cURL request.
PHP use cURL library (module) to provide access curl functions within PHP. By default, most PHP installations come with cURL support is enabled. The basic process of PHP cURL can be divided into four parts.
Further, we need to use the CURLOPT_POSTFIELDS option to set the POST data that we want to submit along with the request. Finally, we’ve used the curl_exec function to execute the cURL request. So in this way, you can make a cURL POST request. More often than not, you need to submit JSON data in a cURL POST request.
Just use the below piece of code to get the response from restful web service url, I use social mention url.
$response = get_web_page("http://socialmention.com/search?q=iphone+apps&f=json&t=microblogs&lang=fr"); $resArr = array(); $resArr = json_decode($response); echo "<pre>"; print_r($resArr); echo "</pre>"; function get_web_page($url) { $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_MAXREDIRS => 10, // stop after 10 redirects CURLOPT_ENCODING => "", // handle compressed CURLOPT_USERAGENT => "test", // name of client CURLOPT_AUTOREFERER => true, // set referrer on redirect CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect CURLOPT_TIMEOUT => 120, // time-out on response ); $ch = curl_init($url); curl_setopt_array($ch, $options); $content = curl_exec($ch); curl_close($ch); return $content; }
The crux of the solution is setting
CURLOPT_RETURNTRANSFER => true
then
$response = curl_exec($ch);
CURLOPT_RETURNTRANSFER tells PHP to store the response in a variable instead of printing it to the page, so $response will contain your response. Here's your most basic working code (I think, didn't test it):
// init curl object $ch = curl_init(); // define options $optArray = array( CURLOPT_URL => 'http://www.google.com', CURLOPT_RETURNTRANSFER => true ); // apply those options curl_setopt_array($ch, $optArray); // execute request and get response $result = curl_exec($ch);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With