无法使用Zend_Http_Client存储第二个客户端请求的会话

时间:2011-03-09 19:16:15

标签: php zend-framework session cookies zend-http-client

我正在向同一个客户端发送多个请求。 zend_Http_Client无法重定向,因为我们的网站提供了javascript重定向。从那个javascript我得到重定向url并再次调用客户端,现在我被拒绝访问。这是因为我无法从第一个客户端请求存储会话。

我正在使用以下代码..

$client = new Zend_Http_Client(
        $loginUrl, 
        array(
            'keepalive' => true,
            'timeout'   => 1000
        )
    );
$response = $client->request()->getBody();
$redirectUrl = $this->_getResponseRedirectUrl($response);

$client->resetParameters();         
$client->setUri($redirectUrl);
$response = $client->request()->getBody();

$resultUrl = $this->_getResponseRedirectUrl($response);

任何人都可以告诉我如何为第二次请求存储会话。

2 个答案:

答案 0 :(得分:0)

在尝试以下步骤之前,请确保您的服务器身份验证基于会话Cookie。

解决方案为我工作:

您需要将第一个休息时间的Cookie添加到第二个请求中。在这种情况下,服务器会将您的第二个请求视为经过身份验证的请求。在登录到站点后的响应中,您的cookie将具有以下一组值,例如cookie-name,cookie-value,Expiration Date,Path,Secure和HTTPOnly。从cookie字符串中分解出cookie-name和cookie-value

登录响应后的会话Cookie示例:

"JSESSIONID=DQl3NKXXmy3yntp3NW2GMlcn8pLn9PR9rl0lnR6vbtfdVpyHWdnq!598502565; path=/"
"CK_LanguageID_252085=1; expires=Friday, 23-Mar-2012 05:31:03 GMT; path=/; secure"
"CK_TimeZone_252085=4; expires=Friday, 23-Mar-2012 05:31:03 GMT; path=/; secure"

使用以下模式为进一步的服务器通信创建新的cookie字符串:

"JSESSIONID=DQl3NKXXmy3yntp3NW2GMlcn8pLn9PR9rl0lnR6vbtfdVpyHWdnq!598502565;  CK_LanguageID_252085=1; CK_TimeZone_252085=4"

添加一个方便的方法来从zend_http_client“Set-cookie”数组创建cookie字符串。

/**
 * Get clean cookie string with only name,value pair
 * This method filer all the follwoing cookie information
 * Expiration Date, Path, Secure and HTTPOnly
 * @access public
 * @param {Array} $cookies
 * @return {String} $cookieString
 */
public function getCookieString(array $cookies){
  $cookieString = null;  
  foreach($cookies as $cookie){
      $part = explode(';',$cookie);
      $cookieString = ($cookieString == null)? $part[0] : $cookieString . '; ' . $part[0];
    }
  return $cookieString;
}

使用Zend_Http_Client发出连续请求:

 //Login
 $client = new Zend_Http_Client($loginUrl); 
 $response = $client->request();

 //Get Header from response
 $headers = $response->getHeaders();
 //Create second header 
 $header = array("Cookie" => $this->getCookieString($headers["Set-cookie"]));
 $client->setHeaders($header);

 //Second request
 $client->setUri($redirectUrl);
 $response = $client->request();  

我在这里删除“$ client-> resetParameters();”因为你没有使用“$ client-> setParameterGet()”设置任何GET Params(同样适用于POST)

如果使用“$ client-> setParameterGet()”或“$ client-> setParameterPost()”,请使用“$ client-> resetParameters();”在设置第二个uri之前。

$ client-> resetParameters()接受布尔值:

  • FALSE:这是仅重置POST和GET Params的默认值。
  • TRUE:重置所有参数,包括标题,最后一个请求和最后一个响应。

答案 1 :(得分:0)

Actuall,Zend_Http_Client提供了一种非常简单的方法来完成你所做的事情,不需要在这里重新发明轮子。

$client = new Zend_Http_Client(
        $loginUrl, 
        array(
            'keepalive' => true,
            'timeout'   => 1000
        )
    );

// this is the magic line...
$client->setCookieJar();

$response = $client->request()->getBody();
$redirectUrl = $this->_getResponseRedirectUrl($response);

$client->resetParameters();         
$client->setUri($redirectUrl);
$response = $client->request()->getBody();

$resultUrl = $this->_getResponseRedirectUrl($response);

在此处阅读更多内容:http://framework.zend.com/manual/en/zend.http.cookies.html

相关问题