17

How can I set a language header for my cURL request? e.g. now I get the homepage of facebook.com in dutch, probably because my server is in the Netherlands / default language send by headers?..

I prefer english before dutch in this case so I tried to set an httpheader in curl but I make no sense? What do I do wrong or what should I have to set?

(zend notation)

CURLOPT_HTTPHEADER => 'Accept-Language: en-US;q=0.6,en;q=0.4',

Thanks in advance!

directory
  • 3,093
  • 8
  • 45
  • 85

4 Answers4

21

I arrived at this page looking for a way to pass the language header to curl at the command line. If you want to set headers in a bash one-liner, use -H Accept-Language

$ curl -s -H 'Accept-Language: es'  -XGET "http://www.google.com"

<!doctype html>
  <html itemscope="" itemtype="http://schema.org/WebPage" lang="es-419">
    <head>
      <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
      <meta content="/logos/doodles/2016/united-states-elections-2016-4829342880235520-hp.gif" itemprop="image">
      <meta content="Tu voz importa. �Encuentra tu casilla y vota! g.co/elections/dondevotar #Everyonein2016 #GoogleDoodle" property="og:description">
      ...
doub1ejack
  • 10,627
  • 20
  • 66
  • 125
  • 1
    Useful to add `-v` to see what headers are sent and received (if you're looking if the server redirects you with the right HTTP code according to the specified language for example). – Nico Prat Oct 19 '17 at 08:52
4

You have to add a header option in your request.

You will have something like this:

$options = array(
    CURLOPT_RETURNTRANSFER => true,     // return web page
    CURLOPT_HEADER         => false,    // don't return headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    .....

$ch      = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$err     = curl_errno($ch);
$errmsg  = curl_error($ch);
$header  = curl_getinfo($ch);
curl_close($ch);

All you have to do is add this line to your $options array:

    CURLOPT_HTTPHEADER     => array("Accept-Language: en-US;q=0.6,en;q=0.4"),
Ivan
  • 14,692
  • 17
  • 59
  • 96
4

This request works properly on:

  • English
curl -s -H 'Accept-Language: en-US,en;q=0.9,it;q=0.8'  -XGET "http://www.google.com" | lynx -dump -stdin
  • Spanish
curl -s -H 'Accept-Language: es-CO,es;q=0.9,it;q=0.8'  -XGET "http://www.google.com" | lynx -dump -stdin
  • Portuguese
curl -s -H 'Accept-Language: pt-BR,pt;q=0.9,it;q=0.8'  -XGET "http://www.google.com" | lynx -dump -stdin

I didn't understand about priorities, but this is necessary to works properly.
About the priorities could find more information here https://www.w3.org/International/questions/qa-lang-priorities.en

dorancemc
  • 123
  • 1
  • 5
0

We managed to do this using curlthin library which used libcurl:

string strAcceptLanguage = string.Format("{0}-{1};q=0.5,{0};q=0.3", Lang, Country.ToUpper());

// Initialize HTTP header list with first value.
var headers = CurlNative.Slist.Append(SafeSlistHandle.Null, strAcceptLanguage);

// Configure libcurl easy handle to send HTTP headers we configured.
CurlNative.Easy.SetOpt(easy, CURLoption.HTTPHEADER, headers.DangerousGetHandle());
Gregory Liénard
  • 1,071
  • 3
  • 7