Web20 University

How to Follow Redirects with PHP cURL

Last edited on
Get up to 65% Off Hosting with FREE SSL & FREE Domains!

* Web 2.0 University is supported by it's audience. If you purchase through links on our site, we may earn an affiliate commision.

In PHP, the cURL library is a powerful tool for making HTTP requests.

Often, when you’re working with APIs or web scraping, you may encounter HTTP redirects, such as 301, 302, or 307 responses.

By default, cURL does not follow redirects, so you’ll need to configure it explicitly to handle them.

In this guide, we’ll cover how to set up PHP cURL to follow redirects, discuss the differences in HTTP redirect status codes, and provide examples for handling redirects with GET and POST requests.


Outline:

  1. Introduction to Redirects in PHP cURL
  2. Setting cURL Options to Follow Redirects in PHP
  3. Understanding HTTP Redirect Status Codes (301, 302, and 307)
  4. Examples of PHP cURL Redirect Handling
    • PHP cURL Follow 302 Redirect
    • PHP cURL Follow Redirect with POST Requests
  5. Conclusion

Setting cURL Options to Follow Redirects in PHP

To enable cURL to follow redirects automatically, you’ll use the CURLOPT_FOLLOWLOCATION option, which tells cURL to handle any Location headers in the response. This is useful when dealing with URLs that redirect to another location, whether temporarily (302) or permanently (301).

Here’s a basic example:

<?php
// Initialize a cURL session
$ch = curl_init("https://example.com");

// Set cURL options to follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);

// Close the cURL session
curl_close($ch);

echo $response;
?>

In this example:

  • CURLOPT_FOLLOWLOCATION is set to true to enable following redirects.
  • CURLOPT_RETURNTRANSFER is set to true so that cURL returns the response instead of outputting it directly.

This setup allows cURL to automatically follow any redirect responses (such as 301, 302, or 307) until it reaches the final URL.


Understanding HTTP Redirect Status Codes (301, 302, and 307)

Different HTTP status codes indicate different types of redirects. Understanding these distinctions is key to handling them appropriately.

  • 301 Redirect (Permanent): Indicates that the resource has permanently moved to a new location. Browsers and search engines typically update their records with the new URL.
  • 302 Redirect (Temporary): Indicates that the resource is temporarily located at a different URL. The client should continue to use the original URL for future requests.
  • 307 Redirect (Temporary, with Method Preservation): Similar to 302, but guarantees that the HTTP method (such as POST or GET) will not change. A 302 redirect might change a POST request to GET, while a 307 redirect ensures the request method remains the same.

For example, if a POST request triggers a 302 redirect, it may turn into a GET request in the redirect. However, if it triggers a 307 redirect, it remains a POST request.


Examples of PHP cURL Redirect Handling

Example 1: PHP cURL Follow 302 Redirect

In many cases, redirects will be of type 302, which is a temporary redirect. This code will follow 302 redirects automatically:

<?php
$ch = curl_init("https://example.com/temporary-redirect");

// Enable following redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute and fetch the response
$response = curl_exec($ch);

// Close cURL session
curl_close($ch);

echo $response;
?>

With CURLOPT_FOLLOWLOCATION set to true, cURL will automatically handle the 302 redirect. However, if the redirect changes the request from POST to GET, you may need to use a different approach (see the next example).

Example 2: PHP cURL Follow Redirect with POST Requests

If you’re making a POST request and expect a redirect, it’s essential to know that cURL will not preserve the POST method by default when handling 302 redirects. Here’s how to configure cURL to follow redirects and preserve the POST method with a 307 status code.

<?php
// Initialize a cURL session for POST request
$ch = curl_init("https://example.com/api");

// Set cURL options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['name' => 'John', 'age' => 30]));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute and fetch the response
$response = curl_exec($ch);

// Close the session
curl_close($ch);

echo $response;
?>

With this setup:

  • The CURLOPT_POST and CURLOPT_POSTFIELDS options are used to send a POST request.
  • CURLOPT_FOLLOWLOCATION is still set to true, allowing cURL to handle any redirect.

If a 307 redirect is encountered, cURL will continue to use the POST method instead of switching to GET. This ensures your request is sent with the same method as the original.


Conclusion

Handling redirects in PHP cURL is essential for applications interacting with APIs or pages that may redirect to different URLs. By setting CURLOPT_FOLLOWLOCATION to true, you can enable cURL to follow these redirects, while understanding HTTP status codes (such as 301, 302, and 307) helps in controlling the redirect behavior accurately. For POST requests, ensure you’re aware of how redirects might alter the request method, particularly with 302 vs. 307 redirects.

Use these techniques to manage cURL redirects effectively, ensuring that your applications follow the intended URL paths and handle redirects smoothly.

Get up to 65% Off Hosting with FREE SSL & FREE Domains!