someone please help me know the difference about setting opt in curl php.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "abcxyz",
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: en-US,en;q=0.5",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Cookie: ht=7635aa7ceda60bf1",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0"
),
));
and
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "abcxyz",
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_COOKIE => "ht=7635aa7ceda60bf1",
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0",
CURLOPT_HTTPHEADER => array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: en-US,en;q=0.5",
"Cache-Control: no-cache",
"Connection: keep-alive"
),
));
I always get error when trying to use CURLOPT_COOKIE and CURLOPT_USERAGENT.
Ok, I did some actual testing and it seems like the 'User-Agent' specified inside CURLOPT_HTTPHEADER
takes precedence over the value CURLOPT_USERAGENT
.
Here is the test setup and results:
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, 'Curl');
curl_setopt_array($curl, array(
CURLOPT_URL => "https://webhook.site/fdb48603-6fe3-48e1-a542-75bb62355522",
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: en-US,en;q=0.5",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Cookie: ht=7635aa7ceda60bf1",
"User-Agent: Override"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
And here is the result:
I also changed the order but it doesn't matter. But if I comment out the User-Agent in CURLOPT_HTTPHEADER
then the reported user-agent is 'Curl' (or the value specified with CURLOPT_USERAGENT
)
So basically the value you specify in CURLOPT_USERAGENT
can be thought of as the default value, and the value you put inside CURLOPT_HTTPHEADER
is the way to override that default.