本文整理汇总了PHP中Zend\Http\Headers::addHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP Headers::addHeader方法的具体用法?PHP Headers::addHeader怎么用?PHP Headers::addHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Http\Headers
的用法示例。
在下文中一共展示了Headers::addHeader方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: disableHttpCache
/**
* Prepare cache-busting headers for GET requests
*
* Invoked from the onFinish() method for GET requests to disable client-side HTTP caching.
*
* @param Headers $headers
*/
protected function disableHttpCache(Headers $headers)
{
$headers->addHeader(new GenericHeader('Expires', '0'));
$headers->addHeader(new GenericMultiHeader('Cache-Control', 'no-store, no-cache, must-revalidate'));
$headers->addHeader(new GenericMultiHeader('Cache-Control', 'post-check=0, pre-check=0'));
$headers->addHeaderLine('Pragma', 'no-cache');
}
示例2: 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());
}
示例3: testFromResponseInCookie
public function testFromResponseInCookie()
{
$response = new Response();
$headers = new Headers();
$header = new SetCookie("foo", "bar");
$header->setDomain("www.zend.com");
$header->setPath("/");
$headers->addHeader($header);
$response->setHeaders($headers);
$response = Cookies::fromResponse($response, "http://www.zend.com");
$this->assertSame($header, $response->getCookie('http://www.zend.com', 'foo'));
}
示例4: 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);
}
示例5: testRetrievingASingleValueForParameters
public function testRetrievingASingleValueForParameters()
{
$request = new Request();
$p = new \Zend\Stdlib\Parameters(array('foo' => 'bar'));
$request->setQuery($p);
$request->setPost($p);
$request->setFiles($p);
$this->assertSame('bar', $request->getQuery('foo'));
$this->assertSame('bar', $request->getPost('foo'));
$this->assertSame('bar', $request->getFiles('foo'));
$headers = new Headers();
$h = new GenericHeader('foo', 'bar');
$headers->addHeader($h);
$request->setHeaders($headers);
$this->assertSame($headers, $request->getHeaders());
$this->assertSame($h, $request->getHeaders()->get('foo'));
$this->assertSame($h, $request->getHeader('foo'));
}
示例6: testLogin
public function testLogin()
{
$user_model = $this->getApplicationServiceLocator()->get('Core\\Model\\User');
$authentication = $this->getApplicationServiceLocator()->get('API\\Service\\AuthenticationService');
//1 - create user record
$uid = md5(time() . rand());
$id = $user_model->insert(array('username' => $uid, 'password' => md5($uid), 'email' => $uid . '@yahoo.com', 'status' => 'active', 'default_role' => 'root'));
//test if user was created
$this->assertTrue((bool) $id, 'User not created for authentication tests.');
//2 - send login request - incorrect
$response = $this->api('login', array('username' => $uid));
$this->assertArrayHasKey('error', $response);
$this->assertArrayHasKey('password', (array) $response['error']);
$response = (array) $response['error'];
$response = (array) $response['password'];
$this->assertArrayHasKey('isEmpty', (array) $response);
//2 - send login request - correct
$response = $this->api('login', array('username' => $uid, 'password' => $uid));
//test if login was successful
$this->assertArrayHasKey('error', $response, 'API response do not returned "error" key.');
$this->assertFalse($response['error'], '"error" key should be false.');
$this->assertArrayHasKey('response', $response, 'API response do not returned "response" key.');
$token = (array) $response['response'];
$this->assertArrayHasKey('$token', $token, '"token" not returned as a key.');
$token = $token['$token'];
$this->assertEquals(32, strlen($token), 'Not a proper MD5 token.');
//3 - get storage and test it with login request data
$storage = $authentication->getStorage()->read();
$this->assertEquals($id, $storage->id);
$this->assertEquals($uid, (string) $storage->username);
$this->assertEquals($token, (string) $storage->token);
$this->assertEquals('active', (string) $storage->status);
//32 - test session mehod
$headers = new Headers();
$headers->addHeader(Authorization::fromString('Authorization: Token ' . $token));
$this->getRequest()->setHeaders($headers);
$response = $this->api('session');
$this->assertArrayHasKey('error', $response);
$this->assertFalse($response['error']);
$this->assertArrayHasKey('response', $response);
$this->assertArrayHasKey('$user', (array) $response['response']);
$response = (array) $response['response'];
$this->assertEquals($id, $response['$user']->id);
$this->assertEquals($uid, (string) $response['$user']->username);
//4 - logout - without authentication
$this->reset();
$response = $this->api('logout');
$this->assertArrayHasKey('error', $response);
$this->assertEquals($response['error'], 'authentication-required');
$this->assertArrayHasKey('response', $response);
$this->assertNull($response['response']);
//5 - logout - with authentication
$this->reset();
$headers = new Headers();
//print_r($token);
$headers->addHeader(Authorization::fromString('Authorization: Token ' . $token));
$this->getRequest()->setHeaders($headers);
$response = $this->api('logout');
$this->assertArrayHasKey('error', $response);
$this->assertFalse($response['error']);
$this->assertArrayHasKey('response', $response);
$this->assertTrue($response['response']);
//6 - repeat previous request
$response = $this->api('logout');
$this->assertArrayHasKey('error', $response);
$this->assertEquals($response['error'], 'authentication-required');
$this->assertArrayHasKey('response', $response);
$this->assertNull($response['response']);
//delete the user created for testing
$this->assertTrue((bool) $user_model->delete(array('id' => $id)), 'Testing user was not deleted.');
}
示例7: disableHttpCache
/**
* Prepare cache-busting headers for GET requests
*
* Invoked from the onFinish() method for GET requests to disable client-side HTTP caching.
*
* @param \Zend\Http\Headers $headers
*/
protected function disableHttpCache($headers)
{
$headers->addHeader(new GenericHeader('Expires', '0'));
// $headers->addHeaderLine('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
$headers->addHeader(new GenericMultiHeader('Cache-Control', 'no-store, no-cache, must-revalidate'));
$headers->addHeader(new GenericMultiHeader('Cache-Control', 'post-check=0, pre-check=0'));
$headers->addHeaderLine('Pragma', 'no-cache');
}
示例8: setVary
/**
* @param Headers $headers
* @return self
*/
public function setVary(Headers $headers)
{
if (!empty($this->cacheConfig['vary']['value']) && (!empty($this->cacheConfig['vary']['override']) || !$headers->has('vary'))) {
$vary = new Header\Vary($this->cacheConfig['vary']['value']);
$headers->addHeader($vary);
}
return $this;
}
示例9: testHeadersAggregatesHeaderThroughAddHeader
public function testHeadersAggregatesHeaderThroughAddHeader()
{
$headers = new Headers();
$headers->addHeader(new Header\GenericHeader('Fake', 'bar'));
$this->assertEquals(1, $headers->count());
$this->assertInstanceOf('Zend\\Http\\Header\\GenericHeader', $headers->get('Fake'));
}
示例10: testCastingToStringReturnsAllMultiHeaderValues
public function testCastingToStringReturnsAllMultiHeaderValues()
{
$headers = new Headers();
$cookie1 = new Header\SetCookie('foo', 'bar');
$cookie2 = new Header\SetCookie('bar', 'baz');
$headers->addHeader($cookie1);
$headers->addHeader($cookie2);
$string = $headers->toString();
$expected = array('Set-Cookie: ' . $cookie1->getFieldValue(), 'Set-Cookie: ' . $cookie2->getFieldValue());
$expected = implode("\r\n", $expected) . "\r\n";
$this->assertEquals($expected, $string);
}