PHP - 使用 cURL 时未设置标头

发布于 2024-12-11 01:17:06 字数 205 浏览 0 评论 0原文

当使用 cURL 访问文件时,它会忽略该文件中设置的标头。当我直接通过浏览器访问同一文件而不使用 cURL 时,它可以正常工作并且标头设置正确。有人知道这是为什么以及我如何解决这个问题吗?

我正在编写一个 API,需要在 Web 服务中设置 HTTP 标头响应,而不是在用于通过 cURL 连接到文件的文件中设置 HTTP 标头响应。希望这是有道理的。

非常感谢。

When using cURL to access a file, it is ignoring the set headers in the said file. When I access the same file directly through the browser without cURL, its works and the headers are set correctly. Anyone know why this is and how I get round this?

I am writing an API and need to set HTTP header responses in web service and not in file used to connect to the file via cURL. Hope that makes sense.

Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

開玄 2024-12-18 01:17:06

当您使用 CURL 时,请尝试将此作为您的用户代理

curl_setopt($c_link, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");

,或者

curl_setopt($c_link, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

您也可以从 Firefox 复制标头,其可能如下所示

$header = "Accept: text/xml,application/xml,application/xhtml+xml,";
  $header = "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
  $header = "Cache-Control: max-age=0";
  $header = "Connection: keep-alive";
  $header = "Keep-Alive: 300";
  $header = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
  $header = "Accept-Language: en-us,en;q=0.5";
  $header = "Pragma: ";

As You are using CURL, try this as your useragent

curl_setopt($c_link, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");

or

curl_setopt($c_link, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

You can also copy headers from firefox which might look like this

$header = "Accept: text/xml,application/xml,application/xhtml+xml,";
  $header = "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
  $header = "Cache-Control: max-age=0";
  $header = "Connection: keep-alive";
  $header = "Keep-Alive: 300";
  $header = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
  $header = "Accept-Language: en-us,en;q=0.5";
  $header = "Pragma: ";
可爱咩 2024-12-18 01:17:06

curl_setopt($ch, CURLOPT_HEADER,);

可能会帮助您。

或者如果想准确给出 http 标头,

curl_setopt($ch, CURLOPT_HTTPHEADER,array(<header parameters>));

这将是一个解决方案。

curl_setopt($ch, CURLOPT_HEADER, <true or false>);

may help you.

Or if want to give a http header exactly,

curl_setopt($ch, CURLOPT_HTTPHEADER,array(<header parameters>));

would be a solution.

他不在意 2024-12-18 01:17:06

我想会帮助你

//open connection
$ch = curl_init();


//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,"URL");
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch,CURLOPT_HTTPHEADER, array('newVar:newValue','Content-type: text/plain', 'Content-length: 100')); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 

//execute post
ob_clean();
$result = curl_exec($ch);
echo $result;
curl_close($ch);

I thins will help you

//open connection
$ch = curl_init();


//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,"URL");
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch,CURLOPT_HTTPHEADER, array('newVar:newValue','Content-type: text/plain', 'Content-length: 100')); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 

//execute post
ob_clean();
$result = curl_exec($ch);
echo $result;
curl_close($ch);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文