doujuxin7392 2012-01-30 12:03
浏览 32
已采纳

Zend_Http_Client cURL授权示例

I need to create authorization script. Cookies must be saved and used later to access the page without authorization. Can anyone give me work example?

  • 写回答

1条回答 默认 最新

  • drsxzut183207938 2012-03-18 02:26
    关注

    First, you have to do an initial request to get the cookies from the server. In the response, you will save the cookies the server sends in the response header Set-Cookie. Where you store them is up to you. I just added them to a session.

        $uri    = 'http://example.com';
        $client = new Zend_Http_Client();
        $client->setUri($uri);
        $client->setAdapter('Zend_Http_Client_Adapter_Curl');
        $adapter = $client->getAdapter();
        $adapter->setCurlOption(CURLOPT_SSL_VERIFYPEER, false);
    
        /** First Request to get required cookies **/
        $response = $client->request();
    
        /** Get response cookie strings and store them (session, db, file, etc) **/
        $cookies  = (array) $response->getHeader('Set-cookie');
        $session  = new Zend_Session_Namespace();
        $session->storedCookies = $cookies;
    

    Now that you have the cookies stored, on your next request you can add them to the request.

        /** Start new Zend_Http_Client instance **/
        $uri      = 'http://example.com';
        $client   = new Zend_Http_Client();
        $client->setUri($uri);
        $client->setAdapter('Zend_Http_Client_Adapter_Curl');
        $adapter  = $client->getAdapter();
        /** This setCurlOption is optional **/
        $adapter->setCurlOption(CURLOPT_SSL_VERIFYPEER, false);
    
    
        $session = new Zend_Session_Namespace();
        $cookies = $session->storedCookies;
        /** Add Stored Cookie strings to Zend_Http_Client instance **/ 
        foreach ($cookies as $cookieStr) {
            $client->setCookie(Zend_Http_Cookie::fromString($cookieStr, $uri));
        }
    
        /** Perform request using stored cookies **/
        $response = $client->request();
    

    If you have questions or need me to elaborate on anything, just let me know.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源