如何设置“REFERER”在 Zend_Http_Client 中?

发布于 2024-10-19 15:51:01 字数 492 浏览 2 评论 0原文

你好 我正在使用 Zend_Http_Client 和适配器 Zend_Http_Client_Adapter_Curl ,我尝试使用设置 REFERER

  $client = new Zend_Http_Client('http://www.example.com',array('adapter'=>'Zend_Http_Client_Adapter_Curl');
        $client->getAdapter()->setCurlOption('CURLOPT_REFERER','http://www.google.com');
$client->request('POST');
echo $client->getLastRequest();

在我的请求中我看到除 REFERER 标头之外的所有类型的标头都已设置? 可以使用 $client->setHeaders('Referer',$url); 来完成但我也在寻找更好的方法。 谢谢。

Hi
I am using Zend_Http_Client with adapter Zend_Http_Client_Adapter_Curl , I tried setting REFERER using

  $client = new Zend_Http_Client('http://www.example.com',array('adapter'=>'Zend_Http_Client_Adapter_Curl');
        $client->getAdapter()->setCurlOption('CURLOPT_REFERER','http://www.google.com');
$client->request('POST');
echo $client->getLastRequest();

In my Request I see all kinds of header getting set except REFERER header ??
It can be done using $client->setHeaders('Referer',$url); also but I am looking for a better way.
Thanks.

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

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

发布评论

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

评论(1

黯然#的苍凉 2024-10-26 15:51:01

您的代码有趣的是我只是尝试运行它并遇到错误。所以我无法测试它。因此,我尝试了另一种方法:

    $adapter = new Zend_Http_Client_Adapter_Curl();
    $adapter->setCurlOption(CURLOPT_REFERER, 'http://www.google.com');

    $client = new Zend_Http_Client('http://www.example.com');      
    $client->setAdapter($adapter);            

    $client->request('POST');
    var_dump($client->getLastRequest());

上面的代码结果是:

string 'GET /domains/example/ HTTP/1.1    
Accept: */*    
Referer: http://www.google.com     <-- THE REFERER
Host: www.iana.org    
Connection: close    
Accept-encoding: gzip, deflate    
User-Agent: Zend_Http_Client


' (length=180)

所以在这种情况下,Referer 标头似乎设置正确。

编辑:
根据OP的要求,我也测试了:

    $adapter = new Zend_Http_Client_Adapter_Curl();

    $client = new Zend_Http_Client('http://www.example.com');
    $client->setAdapter($adapter);

    $client->getAdapter()->setCurlOption(CURLOPT_REFERER, 'http://www.google.com');

    // This line below results in error:  
    // $client->getAdapter()->setCurlOption('referer', 'http://www.google.com');

    $client->request('POST');
    var_dump($client->getLastRequest());

这也像以前一样工作。

What is interesting about your code is that I just tried to run it and a got errors. So I could not test it. For this reason, I tried another way:

    $adapter = new Zend_Http_Client_Adapter_Curl();
    $adapter->setCurlOption(CURLOPT_REFERER, 'http://www.google.com');

    $client = new Zend_Http_Client('http://www.example.com');      
    $client->setAdapter($adapter);            

    $client->request('POST');
    var_dump($client->getLastRequest());

The above code results in:

string 'GET /domains/example/ HTTP/1.1    
Accept: */*    
Referer: http://www.google.com     <-- THE REFERER
Host: www.iana.org    
Connection: close    
Accept-encoding: gzip, deflate    
User-Agent: Zend_Http_Client


' (length=180)

So in this case it seems Referer header is set correctly.

EDIT:
On the OP's request I also tested:

    $adapter = new Zend_Http_Client_Adapter_Curl();

    $client = new Zend_Http_Client('http://www.example.com');
    $client->setAdapter($adapter);

    $client->getAdapter()->setCurlOption(CURLOPT_REFERER, 'http://www.google.com');

    // This line below results in error:  
    // $client->getAdapter()->setCurlOption('referer', 'http://www.google.com');

    $client->request('POST');
    var_dump($client->getLastRequest());

This also works as before.

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