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

How to Add Custom Headers to cURL Requests in PHP?

DDD
DDDOriginal
2024-12-02 21:54:10242browse

How to Add Custom Headers to cURL Requests in PHP?

Customizing Headers in cURL PHP Requests

In certain situations, modifying request headers becomes necessary, especially when emulating specific communication protocols. This article demonstrates how to add custom headers to cURL HTTP requests in PHP.

Solution:

To add custom headers to a cURL request, use the CURLOPT_HTTPHEADER option. This option accepts an array of header lines. For example, to set the headers mentioned in the question:

$ch = curl_init();

$headers = [
    'X-Apple-Tz: 0',
    'X-Apple-Store-Front: 143444,12'
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Once the headers are set, the request can be executed using curl_exec().

Reference:

For detailed information on this option, refer to the PHP manual: https://www.php.net/manual/en/function.curl-setopt.php

The above is the detailed content of How to Add Custom Headers to cURL Requests in PHP?. 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