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.

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化