cUrl set language header

2019-04-21 12:38发布

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!

2条回答
淡お忘
2楼-- · 2019-04-21 13:06

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">
      ...
查看更多
我只想做你的唯一
3楼-- · 2019-04-21 13:19

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"),
查看更多
登录 后发表回答