PHP Curl Get Request with body, Header & Parameters Example

In this tutorial, You will learn how to use CURL to make GET requests with custom headers, body and parameters.

How to Use Curl Get Request with Header & Parameters in PHP?

Here are steps to make curl get request with parameters, parameters and header:

  • Step 1: Installing CURL
  • Step 2: Initializing CURL
  • Step 3: Setting the Request URL
  • Step 4: Setting the Request Headers
  • Step 5: Setting the Request Method
  • Step 6: Handling the Response
  • Step 7: Closing CURL
  • Step 8: How to use headers in CURL GET request

Step 1: Installing CURL

Before you begin, you need to ensure that CURL is installed on our server. CURL is usually installed by default on most Linux-based servers. To check if CURL is installed, you can run the following command in your terminal:

curl --version

This should display the version number of CURL installed on your server. If CURL is not installed, you can install it using the following command:

sudo apt-get install curl

Step 2: Initializing CURL

To use CURL in our PHP code, you need to first initialize it. You can do this by creating a new instance of the CURL class using the following code:

$curl = curl_init();

This will create a new CURL instance that you can use to make HTTP requests.

Step 3: Setting the Request URL

Next, you need to set the URL that you want to make a GET request to. You can do this using the curl_setopt() function and passing in the CURLOPT_URL option along with the URL as the value:

$url = "https://example.com/api/data";
curl_setopt($curl, CURLOPT_URL, $url);

This sets the URL that you want to make a GET request to.

Step 4: Setting the Request Headers

Now, you need to set the headers for our GET request. You can do this by using the curl_setopt() function and passing in the CURLOPT_HTTPHEADER option along with an array of headers as the value:

$headers = array(
    'Authorization: Bearer <access_token>',
    'Content-Type: application/json'
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

This sets the headers that you want to send with our GET request. In this example, you are setting the Authorization header with an access token and the Content-Type header to indicate that you are sending JSON data.

Step 5: Setting the Request Method

You need to specify that you are making a GET request. You can do this by using the curl_setopt() function and passing in the CURLOPT_CUSTOMREQUEST option along with the GET method as the value:

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');

This sets the request method to GET.

Step 6: Handling the Response

Now that you have set up our CURL instance and made the GET request, you need to handle the response. You can do this using the curl_exec() function, which executes the CURL request and returns the response:

$response = curl_exec($curl);

This returns the response as a string.

Step 7: Closing CURL

Finally, You need to close the CURL instance using the curl_close() function:

curl_close($curl);

This frees up any resources used by the CURL instance.

Step 8: How to use headers in CURL GET request

Here’s an example of how to use headers in a cURL GET request:

$url = 'https://example.com/api/data';
$headers = array(
  'Content-Type: application/json',
  'Authorization: Bearer your-access-token'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);

In this example, You define an array of headers that you want to include in the request. You then set the CURLOPT_HTTPHEADER option to this array, which tells cURL to include these headers in the request.

You can include any headers that are required for your API endpoint in this array. The example above includes two common headers, the Content-Type header to specify that the request body is in JSON format and the Authorization header to authenticate the request using a bearer token.

Conclusion

Let’s break down what’s happening in this code:

  • We define the URL of the API endpoint we want to request.
  • We create a new cURL resource using curl_init().
  • We set some options for the cURL request using curl_setopt(). In this example, we set the URL of the request and tell cURL to return the response instead of outputting it directly.
  • We execute the cURL request using curl_exec().
  • We close the cURL session using curl_close().

This code will make a basic GET request to the API endpoint and return the response. However, sometimes you may need to include headers in your GET request, for example, to authenticate the request or specify the data format.

In this article, You looked at how to use PHP cURL GET requests with headers. And alos have learned e basics of cURL, how to make a basic GET request, and how to include headers in a GET request using the CURLOPT_HTTPHEADER option. With these tools, you should be able to make GET requests to remote servers, including APIs, and handle the responses with ease.

Recommended PHP Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *