Home >Backend Development >PHP Tutorial >How to Add Custom Headers to PHP cURL Requests?

How to Add Custom Headers to PHP cURL Requests?

Barbara Streisand
Barbara StreisandOriginal
2024-12-04 15:44:11819browse

How to Add Custom Headers to PHP cURL Requests?

Custom Headers in PHP cURL Requests

In PHP, cURL allows the addition of custom headers to HTTP requests, which can be useful for various scenarios, such as emulating the behavior of external services. To add custom headers to a cURL request, you can use the CURLOPT_HTTPHEADER option.

For instance, let's consider an attempt to replicate iTunes's method of obtaining artwork, which employs the following non-standard headers:

X-Apple-Tz: 0
X-Apple-Store-Front: 143444,12

To incorporate these headers into a request, use the following cURL code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/artwork');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'X-Apple-Tz: 0',
    'X-Apple-Store-Front: 143444,12'
]);
$response = curl_exec($ch);
curl_close($ch);

Here, the CURLOPT_HTTPHEADER option is set to an array of strings, each representing a custom header to be included in the request. Refer to the PHP documentation for more details on this option: https://www.php.net/manual/en/function.curl-setopt.php.

The above is the detailed content of How to Add Custom Headers to PHP cURL Requests?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn