What is different between set opt CURLOPT_USERAGENT and set useragent in header field curl php?
Asked Answered
L

1

7

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.

Litt answered 18/9, 2018 at 18:1 Comment(3)
Why not mention the concrete error? What made you think the user agent has an impact?Penetrant
My api need cookie and useragent for responding but Api server can not detect useragent and cookie if i use CURLOPT. Sometime i got SSL error, but it run well after i changed from curlopt into field in headerAccusatorial
Do more debugging then: Php - Debugging Curl -- At least CURLOPT_VERBOSE and _HEADER to compare the requests. Your code samples are not very telling if this is about peculiarities of a specific API.Penetrant
A
4

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:

headers reported by server

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.

Aden answered 3/8, 2022 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.