本文整理汇总了PHP中Guzzle\Http\Client::head方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::head方法的具体用法?PHP Client::head怎么用?PHP Client::head使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Client
的用法示例。
在下文中一共展示了Client::head方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: head
/**
* Performs an HTTP HEAD on a URI. $this->responseText and $this->responseObj
* empty after this call, but you can read the content-length from
* $this->responseLength.
*
* @param string $uri
* @param string $accept_mime_type
* @param array $custom_headers
* @param array $options
*/
public function head($uri = null, $accept_mime_type = 'application/json; charset=utf-8', array $custom_headers = array(), array $options = array())
{
$headers = array('accept' => $accept_mime_type);
foreach ($custom_headers as $key => $value) {
$headers[strtolower($key)] = $value;
}
$request = $this->client->head($uri, $headers, $options);
$this->exec($request);
}
示例2: fetch
/**
* @return mixed
* @throws SummonException
* @throws \Guzzle\Http\Exception\ClientErrorResponseException
*/
public function fetch()
{
try {
$response = $this->client->head()->send();
} catch (ClientErrorResponseException $e) {
// HEAD method is not allowed, try GET instead
$response = $this->client->get()->send();
}
// update the url to the actual endpoint (after redirects)
$this->url = $response->getInfo('url');
list($this->mimetype) = explode(';', $response->getContentType());
$type = self::getType($this->mimetype);
$handler = sprintf('parse%s', ucfirst($type));
if (!method_exists($this, $handler)) {
throw new SummonException("Unable to handle type [{$type}] in self");
}
return $this->{$handler}($response);
}
示例3: resolveUrl
public function resolveUrl($url)
{
$client = new GuzzleClient($url);
$history = new HistoryPlugin();
$client->addSubscriber($history);
$response = $client->head($url)->send();
if (!$response->isSuccessful()) {
throw new \Exception(sprintf("Url %s is not a valid URL or website is down.", $url));
}
return $response->getEffectiveUrl();
}
示例4: createComposerLockBadge
/**
* @param string $repository
* @param string $format
*
* @return \PUGX\Badge\Model\Badge
*/
public function createComposerLockBadge($repository, $format = 'svg')
{
$repo = str_replace('.git', '', $this->packageRepository->fetchByRepository($repository)->getOriginalObject()->getRepository());
$request = $this->client->head($repo . '/blob/master/composer.lock', array(), array('timeout' => 2, 'connect_timeout' => 1, 'exceptions' => false));
$response = $this->client->send($request);
$status = 500;
if ($request) {
$status = $response->getStatusCode();
}
$this->text = self::LOCK_ERROR;
$color = self::COLOR_ERROR;
$subject = self::SUBJECT_ERROR;
if (200 === $status) {
$this->text = self::LOCK_COMMITTED;
$color = self::COLOR_COMMITTED;
$subject = self::SUBJECT;
} elseif (404 === $status) {
$this->text = self::LOCK_UNCOMMITTED;
$color = self::COLOR_UNCOMMITTED;
$subject = self::SUBJECT;
}
return $this->createBadgeFromRepository($repository, $subject, $color, $format);
}
示例5: validate
public function validate($value, Constraint $constraint)
{
if (empty($value)) {
return;
}
$previousViolationsCount = $this->context->getViolations()->count();
parent::validate($value, $constraint);
if ($previousViolationsCount < $this->context->getViolations()->count()) {
return;
}
$client = new Client();
try {
$request = $client->head($value);
$response = $request->send();
if (!$response->isSuccessful()) {
$this->context->addViolation($constraint->clientError, array('%errorCode%' => $response->getStatusCode()));
}
} catch (CurlException $e) {
$this->context->addViolation($constraint->websiteDoesntExist, array('%url' => $value));
} catch (ClientErrorResponseException $e) {
$errorCode = $e->getResponse()->getStatusCode();
if ($errorCode == 403) {
$this->context->addViolation($constraint->accessDenied);
} elseif ($errorCode == 404) {
$this->context->addViolation($constraint->resNotFound);
} elseif ($errorCode == 405) {
$allow = $e->getResponse()->getHeaders()['allow'];
if (!preg_match('#GET#', $allow)) {
$this->context->addViolation($constraint->methodNotAllowed);
}
} else {
$this->context->addViolation($constraint->clientError, array('%errorCode%' => $errorCode));
}
} catch (ServerErrorResponseException $e) {
$this->context->addViolation($constraint->serverError, array('%errorCode%' => $e->getResponse()->getStatusCode()));
}
}
示例6: testProperlyBlocksBasedOnRequestsInScope
/**
* @covers Guzzle\Http\Curl\CurlMulti
*/
public function testProperlyBlocksBasedOnRequestsInScope()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest1", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest2", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest3", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest4", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest5", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest6", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"));
$client = new Client($this->getServer()->getUrl());
$requests = array($client->get(), $client->get(), $client->get());
// Sends 2 new requests in the middle of a CurlMulti loop while other requests
// are completing. This causes the scope of the multi handle to go up.
$callback = function (Event $event) use($client) {
$client->getConfig()->set('called', $client->getConfig('called') + 1);
$request = $client->get();
if ($client->getConfig('called') <= 2) {
$request->getEventDispatcher()->addListener('request.complete', function (Event $event) use($client) {
$client->head()->send();
});
}
$request->send();
};
$requests[0]->getEventDispatcher()->addListener('request.complete', $callback);
$requests[1]->getEventDispatcher()->addListener('request.complete', $callback);
$requests[2]->getEventDispatcher()->addListener('request.complete', $callback);
$client->send($requests);
$this->assertEquals(8, count($this->getServer()->getReceivedRequests(false)));
}
示例7: head
/**
* @param string $uri
* @param array $headers
* @param array $options
* @return \Guzzle\Http\Message\RequestInterface
*/
public function head($uri, $headers = null, $options = [])
{
$this->request = $this->client->head($uri, $headers, $options);
$this->response = $this->request->send();
return $this->response;
}
示例8: testHeadCanUseOptions
public function testHeadCanUseOptions()
{
$client = new Client();
$head = $client->head('http://www.foo.com', array(), array('query' => array('foo' => 'bar')));
$this->assertEquals('bar', $head->getQuery()->get('foo'));
}
示例9: testUriArrayAllowsCustomTemplateVariables
/**
* @covers Guzzle\Http\Client::createRequest
* @covers Guzzle\Http\Client::get
* @covers Guzzle\Http\Client::put
* @covers Guzzle\Http\Client::post
* @covers Guzzle\Http\Client::head
* @covers Guzzle\Http\Client::options
*/
public function testUriArrayAllowsCustomTemplateVariables()
{
$client = new Client();
$vars = array('var' => 'hi');
$this->assertEquals('/hi', (string) $client->createRequest('GET', array('/{var}', $vars))->getUrl());
$this->assertEquals('/hi', (string) $client->get(array('/{var}', $vars))->getUrl());
$this->assertEquals('/hi', (string) $client->put(array('/{var}', $vars))->getUrl());
$this->assertEquals('/hi', (string) $client->post(array('/{var}', $vars))->getUrl());
$this->assertEquals('/hi', (string) $client->head(array('/{var}', $vars))->getUrl());
$this->assertEquals('/hi', (string) $client->options(array('/{var}', $vars))->getUrl());
}
示例10: getRemoteFileSize
/**
* @param string $location
* @return int
*/
private function getRemoteFileSize($location)
{
$parts = parse_url($location);
$http = new HttpClient($parts['scheme'] . '://' . $parts['host']);
$response = $http->head($parts['path'])->send();
return (int) current($response->getHeader('Content-Length')->toArray());
}