PHP Curl Get Request with Parameters Example
What is cURL in PHP?
The Client for URLs is shortly called cURL, which was originally pronounced with a URL in uppercase to emphasize that it deals with URLs. It's pronounced as:
cURL, which stands for client URL, is a command-line tool that developers use to transfer data to and from a server. At the most fundamental, cURL lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send. cURL supports several different protocols, including HTTP and HTTPS, and runs on almost every platform. This makes cURL ideal for testing communication from almost any device (as long as it has a command line and network connectivity) from a local server to most edge devices.
cURL is also the name of the software project, which encompasses both the
curl command-line tool and the
libcurl development library.
curl_exec command in PHP is a bridge to use curl from the console.
curl_exec makes it easy to quickly and easily do GET/POST requests, receive responses from other servers like JSON and download files.
Role of cURL in PHP
This is a PHP module that allows PHP programs to use curl functions. When PHP's cURL support is turned on, the phpinfo() function's output will include cURL information. Before you write your first basic PHP program, you can double-check it.
A cURL is software 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. This tutorial will see how we can get API data using curl to get requests.
Have a look at some built-in curl 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();
PHP cURL Request Example Code
You can test cURL in your local server as it’s the same as using a standard form with an action.
$url = "https://api.github.com/users/hadley/orgs"; // 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.
Thank you for reading this blog.
If you want to manage your VPS / VM Server without touching the command line go and Checkout this link. ServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a matter of minutes. You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.