2

I'm sending multiple requests to same client. zend_Http_Client not able to redirect because our site giving a javascript redirect. And from that javascript I'm getting the redirect url and again calling the client , now I'm getting access denied. This is beacuse I'm not able to store the session from the first client request.

I'm using the following code..

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

Can anybody tell me how to store the session for the second request.

Charles
  • 50,943
  • 13
  • 104
  • 142
rakesh
  • 21
  • 2

2 Answers2

0

Before trying the following steps make sure your server authentication is based on session cookies.

Solution Worked for me:

You need to add cookies from the first repose to the second request. In this case server consider your second request as authenticated one.In the response after login to the site your cookie will be having following set of values like cookie-name,cookie-value,Expiration Date, Path, Secure and HTTPOnly. From the cookie string explode cookie-name and cookie-value

Example Session Cookie after login response:

"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"

Create new cookie string for further server communication in the following pattern:

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

Adding a handy method to create cookie string from zend_http_client "Set-cookie" array.

/**
 * 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;
}

Using Zend_Http_Client making consecutive requests:

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

Here I am removing "$client->resetParameters();" because you are not setting any GET Params using "$client->setParameterGet()"(Same for POST as well)

If using "$client->setParameterGet()" or "$client->setParameterPost()" use "$client->resetParameters();" before setting second uri.

$client->resetParameters() accepts boolean values:

  • FALSE : This is the default value which reset POST and GET Params only.
  • TRUE : Reset all the params including headers,last request and last response.
Vijay
  • 365
  • 1
  • 2
  • 12
0

Actuall, Zend_Http_Client provides a very simple way to do what you've done, no need to reinvent the wheel here.

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

Read more here: http://framework.zend.com/manual/en/zend.http.cookies.html

shevron
  • 3,463
  • 2
  • 23
  • 35