cUrl set language header

吃可爱长大的小学妹 提交于 2019-12-12 09:36:20

问题


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!


回答1:


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">
      ...



回答2:


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"),



回答3:


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



来源:https://stackoverflow.com/questions/15071298/curl-set-language-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!