In this article, we are going to see how to How to use curl in PHP. There are many ways to fetch and post the data on the webpage. Normally we pass the data on the webpage using form submission, internal functions, and APIs. But sometimes we need to send the data from one server to another server. In that case, PHP provides us cURL built-in PHP extension.
cURL is an extension that you can use to make various requests using different protocols from one server to another and on the same server also PHP has the default option to use cURL, and in this article, we’ll show you several examples related to this.
When we work on some application, it is not necessary to have all the data in one database, sometime we use third party API or data in our web application. Most of the time we do things using JSON APIs But we can do the same thing using cURL which is secure and considered a good option to post and get the data.
Let’s suppose you have two applications and you want to pass the data from one application to another. Then you have two options to do the same. The first one is via API and the second is via cURL. But what if you don’t want to display your API URL to the end-user in the developer console. In that case, you can post and get the data using curl, and in that way, in the front of the website, no one can know from where the data is coming. Otherwise, if you use API then simply anyone can check the data source in-browser developer console.
The module for PHP that makes it possible for PHP programs to access curl functions within PHP. Before using the cURL in the application please make sure it is installed in the server. You can check it via phpinfo() function that will display in its output.
PHP cURL Basics
Some basic cURL functions which are commonly used during cURL integration.
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
Use of curl
cURL is a PHP extension, that allows us to receive and send information via the URL syntax. By doing so, cURL makes it easy to communicate between different websites and domains without exposing the required URL data to the end-user.
PHP curl post Request example
A POST request is usually made to send user collected data to a server. We have tried to make it easy with the below example so that you can understand it easily.
<?php $postRequest = array( 'first' => 'atcodex', 'second' => 'com' ); $cURLConnection = curl_init('http://domainName.com/api'); // URL where you want to send the data curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest); curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true); $apiResponse = curl_exec($cURLConnection); curl_close($cURLConnection);
PHP curl get Request example
A GET request retrieves data from a server. This can be a website’s HTML, an API response, or other resources.
<?php $cURLConnection = curl_init(); curl_setopt($cURLConnection, CURLOPT_URL, 'http://domainName.com/api?name=atcodex&value=1'); curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true); $phoneList = curl_exec($cURLConnection); curl_close($cURLConnection);
PHP cURL set custom Header
This is a guide on how to set custom request headers with PHP’s cuRL extension. Using cURL’s CURLOPT_HTTPHEADER option, we can change request headers such as Referer, Cookie, User-Agent, and Accept-Encoding.
Take a look at the following cURL request:
//The URL you're sending the request to. $url = 'http://localhost/test/file.php'; //Create a cURL handle. $ch = curl_init($url); //Create an array of custom headers. $customHeaders = array( 'Accept-Encoding: gzip, deflate, br', 'Cookie: PHPSESSID=ciqas3vp82m514iubpr1b0md3o;', 'Referer: https://www.google.com/' ); //Use the CURLOPT_HTTPHEADER option to use our //custom headers. curl_setopt($ch, CURLOPT_HTTPHEADER, $customHeaders); //Set options to follow redirects and return output //as a string. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //Execute the request. $result = curl_exec($ch); echo $result;
In the PHP code snippet, we are sending a GET request to a URL. It is containing customer headers that we want to send with parameters.
In this case, we set three custom headers using the CURLOPT_HTTPHEADER option:
- We set the Accept-Encoding header to gzip, deflate, and br (Brotli).
- We provided a PHPSESSID value as the Cookie header.
- The HTTP referer header is set to the Google search engine.
Receive JSON POST Data using PHP
The following example shows how you can get or fetch the JSON POST data using PHP.
- Use json_decode() function to decoded JSON data in PHP.
- The file_get_contents() function is used to received data in a more readable format.
$data = json_decode(file_get_contents('php://input'), true);
Conclusion:
In the above post, we have tried to explain most of the useful things about the curl, its use, its purpose, and its benefit. cURL is good in case you need to call an API internally in an application without exposing data transfer URL between two or more domains to the end-user. cURL comes default with the PHP, but it is good to check before using in if it is available or not you can see How to Check CURL is available using PHP before using it in the application.