I am making requests from Product 1 to Product 2. 3 requests will be sent, the first is authentication via POST where a session will be set on the receiving server, two will be the GET requests to the REST action of choice and three will be the close call to unset the session by POST.
Below is a quick mock up of what is desired:
I can set the session from request one but when the second GET request is sent I think the Session no longer exists. How do I persist that session until the third request to unset it is sent?
Sender:
public function sendRestRequest($action, $params= array(), $method = 'GET')
{
try {
// Authenticate request
$this->sendAuthenticateRequest();
$this->client->setUri('https://product1/controller/'.$action);
// Set post parameter to the authentication token
$this->setRequestParams($params, false, 'GET');
// Make request
$restRequest = $this->client->request($method);
// Get response
$restResponse = $restRequest->getBody();
$this->debugging = 0;
if ($this->debugging == 1) {
echo '<b>Rest Request:</b>';
echo '<pre>';
echo $restRequest;
echo '<pre>';
echo '<b>Rest Response:</b>';
echo '<pre>';
echo $restResponse;
echo '<pre>';
}
// Check the request was successful
if ($restRequest->getStatus() != 200) {
throw new Zend_Exception('The request returned a '.$restRequest->getStatus().' status code');
}
// Clear parameters so another request can be sent
$this->client->resetParameters();
// Close request
$this->closeRequest();
return $restResponse;
} catch (Zend_Exception $e) {
$this->setError(500, $e->getMessage());
$this->generateXml();
}
}
Receiver:
public function authenticationIn($passPhrase)
{
try {
if (!isset($passPhrase)) {
throw new Zend_Rest_Exception('No authentication key is detected.');
}
// Construct pass key from pass phrase and the shared secret
$generatedKey = $this->generateApiKey($passPhrase);
// Get KEY setting value from global_settings
$storedKey = $this->getQuidApiKey();
if ($generatedKey != $storedKey) {
// Bad PASS_PHRASE key send a failed authenticate response
header('HTTP/1.1 500 Application Error');
$authObject = new \stdClass();
$authObject->success = false;
echo json_encode($authObject);
exit;
} else {
// This session needs to persist until the 3rd request to unset it
$_SESSION['auth'] = true;
return true;
}
} catch (Zend_Service_Exception $e) {
$this->setError(500, $e->getMessage());
$this->generateXml();
}
}
From the client's perspective it's just the cookie(s) that need to be preserved between requests. Take a look at the cookie jar functionality: http://framework.zend.com/manual/1.12/en/zend.http.client.advanced.html#zend.http.client.cookies (particularly example #3).