php – Zend_Http_Client cURL授权示例

我需要创建授权脚本.必须保存Cookie并在以后用于未经授权访问该页面.谁能给我工作的例子?

解决方法:

首先,您必须执行初始请求以从服务器获取cookie.在响应中,您将保存服务器在响应头Set-Cookie中发送的cookie.存储它们的地方取决于您.我刚刚将它们添加到会话中.

    $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;

现在您已经存储了cookie,在您的下一个请求中,您可以将它们添加到请求中.

    /** 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();

如果您有任何疑问或需要我详细说明,请告诉我.

上一篇:java – 如何使用注释基于在REST中创建这些资源的用户来授权特定资源


下一篇:python – App Engine的用户名和密码登录?