Curl Get Request With Parameters Example

There will be times that you will need to pull out data from a web service using PHP’s GET method. In this tutorial I will be demonstrating how you can make a GET Request using cURL.

cURL is software which you can use to make various requests using different protocols. PHP has the option to use cURL and in this article, we’ll show several examples. In this tutorial we are going to see how we can get api data using curl get request. 

Have a look some curl built in function

curl_init();      // initializes a cURL session
curl_setopt();    // changes the cURL session behavior with options
curl_exec();      // executes the started cURL session
curl_close();     // closes the cURL session and deletes the variable made by curl_init();

cURL GET request example code

ou can test cURL in your own local server as it’s the same with using a regular form with an action.

$url = "https://learnwebcode.github.io/json-example/animals-1.json";

//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Print the return data
print_r(json_decode($result, true));

Use json_decode if the return is in json format. Hope it will help you.

#php #curl