如何从curl句柄中删除以前设置的请求标头referer字段?

如何从curl句柄中删除以前设置的请求标头referer字段?

问题描述:

First I initialize curl handle:

$ch = curl_init();

Next I set the url and referer headers:

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_REFERER,$referer);

And finally execute statement:

curl_exec($ch);

Now I can use another url without closing and reopening the handle, so:

curl_setopt($ch,CURLOPT_URL,$another_url);

And here headache begins, because I do not know how to disable referer header that would be send do server, of course I've tried to put false and null into CURLOPT_REFERER but it causes the referer field to be empty, that is a Referer: is still send to the server but with no value (is this even correct with http specs?).

Is there any option to remove header altogether without closing and reinstantiating curl handle ?

I'd like to avoid it because curl keeps a connection open for some time, if I would constantly close the handle while downloading from the same host it could take more time.

首先我初始化curl句柄: p>

  $ ch = curl_init  (); 
  code>  pre> 
 
 

接下来我设置了url和referer标题: p>

  curl_setopt($ ch,CURLOPT_URL,  $ url); 
curl_setopt($ ch,CURLOPT_REFERER,$ referer); 
  code>  pre> 
 
 

最后执行语句: p>

  curl_exec($ ch); 
  code>  pre> 
 
 

现在我可以使用另一个url而不关闭并重新打开句柄,所以: p>

  curl_setopt($ ch,CURLOPT_URL,$ another_url); 
  code>  pre> 
 
 

这里头痛开始,因为我不知道如何禁用referer header 发送服务器,当然我已经尝试将 false code>和 null code>放入CURLOPT_REFERER但它会导致referer字段为空,即 Referer:仍然发送到服务器但没有值(这对于http规范来说是否正确?)。 p>

是否有任何选项可以完全删除标题而不关闭并重新实例化curl h andle? p>

我想避免它,因为curl保持连接打开一段时间,如果我在从同一主机下载时不断关闭句柄,则可能需要更多时间。 / p> div>

You can remove completely the referer field, or any other field normally handled by curl, by passing it without anything after ":" to CURLOPT_HTTPHEADER:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Referer:"));

And it won't appear at all in the header.

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTHTTPHEADER

The Referer header should be either the full URI or a URI relative to one requested:

http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#z14

It seems like a blank Referer header meets the spec, so you could just:

curl_setopt($ch,CURLOPT_REFERER,'');

The header will still appear, but it will be blank.