开发者

how to set "REFERER" in Zend_Http_Client?

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 bu开发者_运维知识库t I am looking for a better way. Thanks.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜