Load the connection adapter
public getAdapter ( ) : Zend_Http_Client_Adapter_Interface | ||
return | Zend_Http_Client_Adapter_Interface | $adapter |
public function processException(Exception $exception) { if ($exception instanceof Zend_Exception) { $this->_httpClient->getAdapter()->close(); } parent::processException($exception); }
public function testStreamRequest() { if (!$this->client->getAdapter() instanceof Zend_Http_Client_Adapter_Stream) { $this->markTestSkipped('Current adapter does not support streaming'); return; } $data = fopen(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg', "r"); $res = $this->client->setRawData($data, 'image/jpeg')->request('PUT'); $expected = $this->_getTestFileContents('staticFile.jpg'); $this->assertEquals($expected, $res->getBody(), 'Response body does not contain the expected data'); }
/** * Test that we can handle trailing space in location header * * @group ZF-11283 * @link http://framework.zend.com/issues/browse/ZF-11283 */ public function testRedirectWithTrailingSpaceInLocationHeaderZF11283() { $this->_client->setUri('http://example.com/'); $this->_client->setAdapter('Zend_Http_Client_Adapter_Test'); $adapter = $this->_client->getAdapter(); /* @var $adapter Zend_Http_Client_Adapter_Test */ $response = "HTTP/1.1 302 Redirect\r\n" . "Content-Type: text/html; charset=UTF-8\r\n" . "Location: /test\r\n" . "Server: Microsoft-IIS/7.0\r\n" . "Date: Tue, 19 Apr 2011 11:23:48 GMT\r\n\r\n" . "RESPONSE"; $adapter->setResponse($response); $res = $this->_client->request('GET'); $lastUri = $this->_client->getUri(); $this->assertEquals("/test", $lastUri->getPath()); }
/** * Performs a HTTP request using the specified method * * @param string $method The HTTP method for the request - 'GET', 'POST', * 'PUT', 'DELETE' * @param string $url The URL to which this request is being performed * @param array $headers An associative array of HTTP headers * for this request * @param string $body The body of the HTTP request * @param string $contentType The value for the content type * of the request body * @param int $remainingRedirects Number of redirects to follow if request * s results in one * @return Zend_Http_Response The response object */ public function performHttpRequest($method, $url, $headers = null, $body = null, $contentType = null, $remainingRedirects = null) { require_once 'Zend/Http/Client/Exception.php'; if ($remainingRedirects === null) { $remainingRedirects = self::getMaxRedirects(); } if ($headers === null) { $headers = array(); } // Append a Gdata version header if protocol v2 or higher is in use. // (Protocol v1 does not use this header.) $major = $this->getMajorProtocolVersion(); $minor = $this->getMinorProtocolVersion(); if ($major >= 2) { $headers['GData-Version'] = $major + ($minor === null ? '.' + $minor : ''); } // check the overridden method if (($method == 'POST' || $method == 'PUT') && $body === null && $headers['x-http-method-override'] != 'DELETE') { require_once 'Zend/Gdata/App/InvalidArgumentException.php'; throw new Zend_Gdata_App_InvalidArgumentException('You must specify the data to post as either a ' . 'string or a child of Zend_Gdata_App_Entry'); } if ($url === null) { require_once 'Zend/Gdata/App/InvalidArgumentException.php'; throw new Zend_Gdata_App_InvalidArgumentException('You must specify an URI to which to post.'); } $headers['Content-Type'] = $contentType; if (Zend_Gdata_App::getGzipEnabled()) { // some services require the word 'gzip' to be in the user-agent // header in addition to the accept-encoding header if (strpos($this->_httpClient->getHeader('User-Agent'), 'gzip') === false) { $headers['User-Agent'] = $this->_httpClient->getHeader('User-Agent') . ' (gzip)'; } $headers['Accept-encoding'] = 'gzip, deflate'; } else { $headers['Accept-encoding'] = 'identity'; } // Make sure the HTTP client object is 'clean' before making a request // In addition to standard headers to reset via resetParameters(), // also reset the Slug and If-Match headers $this->_httpClient->resetParameters(); $this->_httpClient->setHeaders(array('Slug', 'If-Match')); // Set the params for the new request to be performed $this->_httpClient->setHeaders($headers); require_once 'Zend/Uri/Http.php'; $uri = Zend_Uri_Http::fromString($url); preg_match("/^(.*?)(\\?.*)?\$/", $url, $matches); $this->_httpClient->setUri($matches[1]); $queryArray = $uri->getQueryAsArray(); foreach ($queryArray as $name => $value) { $this->_httpClient->setParameterGet($name, $value); } $this->_httpClient->setConfig(array('maxredirects' => 0)); // Set the proper adapter if we are handling a streaming upload $usingMimeStream = false; $oldHttpAdapter = null; if ($body instanceof Zend_Gdata_MediaMimeStream) { $usingMimeStream = true; $this->_httpClient->setRawDataStream($body, $contentType); $oldHttpAdapter = $this->_httpClient->getAdapter(); if ($oldHttpAdapter instanceof Zend_Http_Client_Adapter_Proxy) { require_once 'Zend/Gdata/HttpAdapterStreamingProxy.php'; $newAdapter = new Zend_Gdata_HttpAdapterStreamingProxy(); } else { require_once 'Zend/Gdata/HttpAdapterStreamingSocket.php'; $newAdapter = new Zend_Gdata_HttpAdapterStreamingSocket(); } $this->_httpClient->setAdapter($newAdapter); } else { $this->_httpClient->setRawData($body, $contentType); } try { $response = $this->_httpClient->request($method); // reset adapter if ($usingMimeStream) { $this->_httpClient->setAdapter($oldHttpAdapter); } } catch (Zend_Http_Client_Exception $e) { // reset adapter if ($usingMimeStream) { $this->_httpClient->setAdapter($oldHttpAdapter); } require_once 'Zend/Gdata/App/HttpException.php'; throw new Zend_Gdata_App_HttpException($e->getMessage(), $e); } if ($response->isRedirect() && $response->getStatus() != '304') { if ($remainingRedirects > 0) { $newUrl = $response->getHeader('Location'); $response = $this->performHttpRequest($method, $newUrl, $headers, $body, $contentType, $remainingRedirects); } else { require_once 'Zend/Gdata/App/HttpException.php'; throw new Zend_Gdata_App_HttpException('Number of redirects exceeds maximum', null, $response); } } if (!$response->isSuccessful()) { require_once 'Zend/Gdata/App/HttpException.php'; $exceptionMessage = 'Expected response code 200, got ' . $response->getStatus(); if (self::getVerboseExceptionMessages()) { $exceptionMessage .= "\n" . $response->getBody(); } $exception = new Zend_Gdata_App_HttpException($exceptionMessage); $exception->setResponse($response); throw $exception; } return $response; }
/** * Authenticate with the remote document production server * Sets the property httpclient to an instance of Zend_Http_Client * for further interaction with the document production server. * * @return bool */ private function _remoteServerAuthentication() { $config = Zend_Registry::get('params'); $host = ''; $authk = ''; $requesttimeout = ''; // Capture DMS service parameters if (isset($config->dms) && isset($config->dms->requestHost)) { $host = $config->dms->requestHost; } if (isset($config->dms) && isset($config->dms->authToken)) { $authk = $config->dms->authToken; } if (isset($config->dms) && isset($config->dms->requestTimeout)) { $requesttimeout = $config->dms->requestTimeout; } if (isset($config->dms) && isset($config->dms->customercode)) { $customercode = $config->dms->customercode; } // Validate parameters are ok if (!isset($host) || $host == '') { error_log(__FILE__ . ':' . __LINE__ . ':DMS service host not set'); throw new Exception('DMS service host not set'); } if (!isset($authk) || $authk == '') { error_log(__FILE__ . ':' . __LINE__ . ':DMS service auth token not set'); throw new Exception('DMS service auth token not set'); } if (!isset($requesttimeout) || $requesttimeout == '') { // Default to 15 seconds $requesttimeout = 15; } $client = new Zend_Http_Client($host . '?action=UserAuth&language=gb&cc=' . $customercode, array('maxredirects' => 0, 'timeout' => $requesttimeout, 'keepalive' => true)); // Set the adapter $client->setAdapter(new Zend_Http_Client_Adapter_Curl()); // Disable SSL certificate verification, fails in testing $client->getAdapter()->setCurlOption(CURLOPT_SSL_VERIFYHOST, 0); $client->getAdapter()->setCurlOption(CURLOPT_SSL_VERIFYPEER, false); // Login to DMS service $client->setParameterPost(array('authmethod' => '1', 'AuthK' => $authk, 'login' => '', 'password' => '')); try { $response = $client->request('POST'); } catch (Zend_Http_Client_Exception $ex) { error_log(__FILE__ . ':' . __LINE__ . ':' . $ex->getMessage()); throw new Exception('Failure calling DMS server'); } $this->_debugRequest($client); if (is_string($response)) { $response = Zend_Http_Response::fromString($response); } else { if (!$response instanceof Zend_Http_Response) { // Some other response returned, don't know how to process. // The request is queued, so return a fault. error_log(__FILE__ . ':' . __LINE__ . ':' . $ex->getMessage()); throw new Exception('DMS server returned unknown response'); } } try { $response = $response->getBody(); $loginresponse = Zend_Json::decode($response); } catch (Exception $ex) { // Problem requesting service, report the problem back but keep the queue record error_log(__FILE__ . ':' . __LINE__ . ':' . $ex->getMessage()); throw new Exception('DMS server returned invalid response'); } $client->resetParameters(); // Check login response if ($loginresponse == null) { // Problem authenticating with DMS server error_log(__FILE__ . ':' . __LINE__ . ':Failed to auth DMS server for request'); throw new Exception('Failed to auth DMS server for request'); } // Make request for creation $client->setUri($host); // Remove get parameters from original login uri $this->httpclient = $client; $this->sessionid = $loginresponse['SessionId']; $this->customercode = $customercode; return true; }
/** * @group ZF-11598 */ public function testGetAdapterWithoutSet() { $client = new Zend_Http_Client(null, $this->config); $adapter = $client->getAdapter(); $this->assertTrue(!empty($adapter)); }
<?php header('Content-Type: text/plain'); set_include_path(realpath('../src/ZendFramework/library')); require_once 'Zend/Http/Client.php'; require_once 'Zend/XmlRpc/Client.php'; require_once 'Zend/XmlRpc/Request.php'; require_once 'Zend/XmlRpc/Generator/XmlWriter.php'; $generator = new Zend_XmlRpc_Generator_XmlWriter(); //require_once('Zend/XmlRpc/Generator/DomDocument.php'); //$generator = new Zend_XmlRpc_Generator_DomDocument(); Zend_XmlRpc_Value::setGenerator($generator); $httpClient = new Zend_Http_Client(); $httpClient->setAdapter('Zend_Http_Client_Adapter_Test'); $httpClient->getAdapter()->addResponse(new Zend_Http_Response(200, array(), new Zend_XmlRpc_Response('OK'))); $client = new Zend_XmlRpc_Client('http://localhost/', $httpClient); for ($i = 8; $i > 0; $i--) { $client->call("test-{$i}", array('one', 'two')); echo "memory_usage " . memory_get_usage() . " bytes \n"; }