本文整理汇总了PHP中Zend\Http\Request::setHeaders方法的典型用法代码示例。如果您正苦于以下问题:PHP Request::setHeaders方法的具体用法?PHP Request::setHeaders怎么用?PHP Request::setHeaders使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Http\Request
的用法示例。
在下文中一共展示了Request::setHeaders方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: sendRequest
/**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request)
{
$request = $this->sanitizeRequest($request);
$headers = new Headers();
foreach ($request->getHeaders() as $key => $value) {
$headers->addHeader(new GenericHeader($key, $request->getHeaderLine($key)));
}
$zendRequest = new Request();
$zendRequest->setMethod($request->getMethod());
$zendRequest->setUri((string) $request->getUri());
$zendRequest->setHeaders($headers);
$zendRequest->setContent($request->getBody()->getContents());
$options = ['httpversion' => $request->getProtocolVersion()];
if (extension_loaded('curl')) {
$options['curloptions'] = [CURLOPT_HTTP_VERSION => $this->getProtocolVersion($request->getProtocolVersion())];
}
$this->client->setOptions($options);
if ($this->client->getAdapter() instanceof ZendClient\Adapter\Curl && $request->getMethod()) {
$request = $request->withHeader('Content-Length', '0');
}
try {
$zendResponse = $this->client->send($zendRequest);
} catch (RuntimeException $exception) {
throw new NetworkException($exception->getMessage(), $request, $exception);
}
return $this->responseFactory->createResponse($zendResponse->getStatusCode(), $zendResponse->getReasonPhrase(), $zendResponse->getHeaders()->toArray(), $zendResponse->getContent(), $zendResponse->getVersion());
}
示例2: pharAction
public function pharAction()
{
$client = $this->serviceLocator->get('zendServerClient');
$client = new Client();
if (defined('PHAR')) {
// the file from which the application was started is the phar file to replace
$file = $_SERVER['SCRIPT_FILENAME'];
} else {
$file = dirname($_SERVER['SCRIPT_FILENAME']) . '/zs-client.phar';
}
$request = new Request();
$request->setMethod(Request::METHOD_GET);
$request->setHeaders(Headers::fromString('If-Modified-Since: ' . gmdate('D, d M Y H:i:s T', filemtime($file))));
$request->setUri('https://github.com/zendtech/ZendServerSDK/raw/master/bin/zs-client.phar');
//$client->getAdapter()->setOptions(array('sslcapath' => __DIR__.'/../../../certs/'));
$client->setAdapter(new Curl());
$response = $client->send($request);
if ($response->getStatusCode() == 304) {
return 'Already up-to-date.';
} else {
ErrorHandler::start();
rename($file, $file . '.' . date('YmdHi') . '.backup');
$handler = fopen($file, 'w');
fwrite($handler, $response->getBody());
fclose($handler);
ErrorHandler::stop(true);
return 'The phar file was updated successfully.';
}
}
示例3: testRequestCanSetHeaders
public function testRequestCanSetHeaders()
{
$request = new Request();
$headers = new \Zend\Http\Headers();
$ret = $request->setHeaders($headers);
$this->assertInstanceOf('Zend\\Http\\Request', $ret);
$this->assertSame($headers, $request->headers());
}
示例4: testGet
public function testGet()
{
$expectedDate = '2015-01-01';
$headers = new Headers();
$headers->addHeaderLine(sprintf('Cookie: %s=%s', TimetableCookieListener::COOKIE_NAME, $expectedDate));
$request = new Request();
$request->setHeaders($headers);
$response = new Response();
$listener = new TimetableCookieListener($request, $response);
$event = new TimetableManagerEvent();
$listener->getTime($event);
$this->assertTrue($event->hasPointInTime());
$this->assertEquals($expectedDate, $event->getPointInTime()->format('Y-m-d'));
}
示例5: request
/**
* @param Request $request
* @return Response
*/
public function request(Request $request)
{
$headers = new Headers();
$headers->addHeaders($request->getHeaders());
$zendRequest = new ZendRequest();
$zendRequest->setVersion($request->getProtocolVersion());
$zendRequest->setMethod($request->getMethod());
$zendRequest->setUri((string) $request->getUrl());
$zendRequest->setHeaders($headers);
$zendRequest->setContent($request->getContent());
/** @var ZendResponse $zendResponse */
$zendResponse = $this->client->send($zendRequest);
return new Response((string) $zendResponse->getVersion(), $zendResponse->getStatusCode(), $zendResponse->getReasonPhrase(), $zendResponse->getHeaders()->toArray(), $zendResponse->getContent());
}
示例6: save
/**
* @param string $collectionPath
* @param \SimpleXMLElement $element
* @return \SimpleXMLElement | null
*/
public function save($collectionPath, \SimpleXMLElement $element)
{
$uri = new Http($collectionPath);
$request = new Request();
$request->setMethod('PUT');
$headers = new Headers();
$headers->addHeader(new GenericHeader('Content-Type', 'application/xml'));
$request->setHeaders($headers);
$request->setContent($element->asXML());
$response = $this->transport->put($uri, $request);
$body = $response->getBody();
if ($response->isClientError() || empty($body)) {
return null;
}
return new \SimpleXMLElement($body);
}
示例7: testUnsupportedScheme
public function testUnsupportedScheme()
{
$response = new Response();
$headers = new Headers();
$request = new Request();
$headers->addHeaderLine('Authorization', 'NotSupportedScheme <followed by a space character');
$request->setHeaders($headers);
$a = new Adapter\Http($this->_digestConfig);
$a->setDigestResolver($this->_digestResolver)->setRequest($request)->setResponse($response);
$result = $a->authenticate();
$this->assertEquals($result->getCode(), Authentication\Result::FAILURE_UNCATEGORIZED);
}
示例8: _doAuth
/**
* Acts like a client sending the given Authenticate header value.
*
* @param string $clientHeader Authenticate header value
* @param string $scheme Which authentication scheme to use
* @return array Containing the result, the response headers, and the status
*/
public function _doAuth($clientHeader, $scheme)
{
// Set up stub request and response objects
$response = new Response();
$response->setStatusCode(200);
$headers = new Headers();
$headers->addHeaderLine('Proxy-Authorization', $clientHeader);
$headers->addHeaderLine('User-Agent', 'PHPUnit');
$request = new Request();
$request->setUri('http://localhost/');
$request->setMethod('GET');
$request->setHeaders($headers);
// Select an Authentication scheme
switch ($scheme) {
case 'basic':
$use = $this->_basicConfig;
break;
case 'digest':
$use = $this->_digestConfig;
break;
case 'both':
default:
$use = $this->_bothConfig;
}
// Create the HTTP Auth adapter
$a = new \Zend\Authentication\Adapter\Http($use);
$a->setBasicResolver($this->_basicResolver);
$a->setDigestResolver($this->_digestResolver);
// Send the authentication request
$a->setRequest($request);
$a->setResponse($response);
$result = $a->authenticate();
$return = array('result' => $result, 'status' => $response->getStatusCode(), 'headers' => $response->getHeaders());
return $return;
}
示例9: call
/**
* Call an api method
*
* @param $method
* @param $params
* @return mixed
* @throws \Exception
*/
public function call($method, $params)
{
$params['apikey'] = $this->getApiKey();
$params = json_encode($params);
$client = $this->getClient();
$uri = $this->getBaseUri() . $method . '.json';
$headers = new Headers();
$headers->addHeaderLine('Accept-Encoding', 'identity');
$headers->addHeaderLine('Content-Type', 'application/json');
$headers->addHeaderLine('Accept', '*/*');
$request = new Request();
$request->setHeaders($headers);
$request->setUri($uri);
$request->setMethod('POST');
$request->setContent($params);
$response = $client->dispatch($request);
if ($response->isSuccess()) {
return json_decode($response->getContent(), true);
} else {
/*@TODO throw a more useful exception*/
throw new \Exception('Request Failed');
}
}