Curl 卷曲集语言头

Curl 卷曲集语言头,curl,http-headers,user-agent,http-accept-language,Curl,Http Headers,User Agent,Http Accept Language,如何为cURL请求设置语言头?e、 g.现在我得到了facebook.com的荷兰语主页,可能是因为我的服务器使用的是荷兰语/默认语言send by headers 在这种情况下,我更喜欢英语而不是荷兰语,所以我尝试在curl中设置一个httpheader,但没有意义?我做错了什么,或者我应该设置什么 (zend符号) 提前谢谢 您必须在请求中添加标题选项 您将有如下内容: $options = array( CURLOPT_RETURNTRANSFER => true,

如何为cURL请求设置语言头?e、 g.现在我得到了facebook.com的荷兰语主页,可能是因为我的服务器使用的是荷兰语/默认语言send by headers

在这种情况下,我更喜欢英语而不是荷兰语,所以我尝试在curl中设置一个httpheader,但没有意义?我做错了什么,或者我应该设置什么

(zend符号)


提前谢谢

您必须在请求中添加标题选项

您将有如下内容:

$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);
您只需将此行添加到$options数组:

    CURLOPT_HTTPHEADER     => array("Accept-Language: en-US;q=0.6,en;q=0.4"),

我来到这一页是为了寻找一种方法,在命令行中将语言头传递给curl。如果要在bash one行程序中设置标题,请使用
-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">
      ...
$curl-s-H'接受语言:es'-XGET“http://www.google.com"
...

此请求在以下情况下正常工作:

  • 英式
  • 西班牙文
  • 葡萄牙人
我不了解优先级,但这是正常工作所必需的。

关于优先级,可以在此处找到更多信息

我们通过使用libcurl的curlthin库实现了这一点:

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());

添加
-v
以查看发送和接收的标题非常有用(例如,如果您正在查看服务器是否根据指定的语言使用正确的HTTP代码重定向您)。
curl -s -H 'Accept-Language: en-US,en;q=0.9,it;q=0.8'  -XGET "http://www.google.com" | lynx -dump -stdin
curl -s -H 'Accept-Language: es-CO,es;q=0.9,it;q=0.8'  -XGET "http://www.google.com" | lynx -dump -stdin
curl -s -H 'Accept-Language: pt-BR,pt;q=0.9,it;q=0.8'  -XGET "http://www.google.com" | lynx -dump -stdin
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());