本文整理汇总了PHP中Guzzle\Http\Message\RequestFactory类的典型用法代码示例。如果您正苦于以下问题:PHP RequestFactory类的具体用法?PHP RequestFactory怎么用?PHP RequestFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RequestFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: parseRequest
public function parseRequest($data)
{
list($headers, $bodyBuffer) = explode("\r\n\r\n", $data, 2);
$factory = new RequestFactory();
$parsed = $factory->parseMessage($headers . "\r\n\r\n");
$parsedQuery = array();
if (0 === strpos($parsed['parts']['query'], '?')) {
$query = substr($parsed['parts']['query'], 1);
parse_str($query, $parsedQuery);
}
$request = new Request($parsed['method'], $parsed['parts']['path'], $parsedQuery, $parsed['protocol_version'], $parsed['headers']);
return array($request, $bodyBuffer);
}
示例2: testIgnoresUnsentRequests
/**
* @covers Guzzle\Http\Plugin\HistoryPlugin::add
*/
public function testIgnoresUnsentRequests()
{
$h = new HistoryPlugin();
$request = RequestFactory::getInstance()->create('GET', 'http://localhost/');
$h->add($request);
$this->assertEquals(0, count($h));
}
示例3: testRevalidatesResponsesAgainstOriginServer
/**
* @dataProvider cacheRevalidationDataProvider
*/
public function testRevalidatesResponsesAgainstOriginServer($can, $request, $response, $validate = null, $result = null)
{
// Send some responses to the test server for cache validation
$server = $this->getServer();
$server->flush();
if ($validate) {
$server->enqueue($validate);
}
$request = RequestFactory::getInstance()->fromMessage("GET / HTTP/1.1\r\nHost: 127.0.0.1:" . $server->getPort() . "\r\n" . $request);
$response = Response::fromMessage($response);
$request->setClient(new Client());
$plugin = new CachePlugin(new DoctrineCacheAdapter(new ArrayCache()));
$this->assertEquals($can, $plugin->canResponseSatisfyRequest($request, $response), '-> ' . $request . "\n" . $response);
if ($result) {
$result = Response::fromMessage($result);
$result->removeHeader('Date');
$request->getResponse()->removeHeader('Date');
$request->getResponse()->removeHeader('Connection');
// Get rid of dates
$this->assertEquals((string) $result, (string) $request->getResponse());
}
if ($validate) {
$this->assertEquals(1, count($server->getReceivedRequests()));
}
}
示例4: uploadMedia
private function uploadMedia($path, $type)
{
//build Mac
$timeStamp = time();
$lstParams = array($this->pageId, $timeStamp, $this->secretKey);
$mac = ZaloSdkHelper::buildMacForAuthentication($lstParams);
$request = RequestFactory::getInstance()->create('POST', CommonInfo::$DOMAIN . CommonInfo::$SER_UPLOAD)->setClient(new Client())->addPostFiles(array(CommonInfo::$URL_UPLOAD => $path))->addPostFields(array(CommonInfo::$URL_ACT => $type, CommonInfo::$URL_PAGEID => $this->pageId, CommonInfo::$URL_TIMESTAMP => $timeStamp, CommonInfo::$URL_MAC => $mac));
$response = $request->send()->json();
$error = $response["error"];
if ($error < 0) {
$zaloSdkExcep = new ZaloSdkException();
$zaloSdkExcep->setZaloSdkExceptionErrorCode($error);
if (!empty($response["message"])) {
$zaloSdkExcep->setZaloSdkExceptionMessage($response["message"]);
}
throw $zaloSdkExcep;
} else {
if (!empty($response["result"])) {
return $response["result"];
} else {
$zaloSdkExcep = new ZaloSdkException();
$zaloSdkExcep->setZaloSdkExceptionErrorCode(-1);
throw zaloSdkExcep;
}
}
}
示例5: createRedirectRequest
protected function createRedirectRequest(RequestInterface $request, $statusCode, $location, RequestInterface $original)
{
$redirectRequest = null;
$strict = $original->getParams()->get(self::STRICT_REDIRECTS);
if ($request instanceof EntityEnclosingRequestInterface && ($statusCode == 303 || !$strict && $statusCode <= 302)) {
$redirectRequest = RequestFactory::getInstance()->cloneRequestWithMethod($request, 'GET');
} else {
$redirectRequest = clone $request;
}
$redirectRequest->setIsRedirect(true);
$redirectRequest->setResponseBody($request->getResponseBody());
$location = Url::factory($location);
if (!$location->isAbsolute()) {
$originalUrl = $redirectRequest->getUrl(true);
$originalUrl->getQuery()->clear();
$location = $originalUrl->combine((string) $location, true);
}
$redirectRequest->setUrl($location);
$redirectRequest->getEventDispatcher()->addListener('request.before_send', $func = function ($e) use(&$func, $request, $redirectRequest) {
$redirectRequest->getEventDispatcher()->removeListener('request.before_send', $func);
$e['request']->getParams()->set(RedirectPlugin::PARENT_REQUEST, $request);
});
if ($redirectRequest instanceof EntityEnclosingRequestInterface && $redirectRequest->getBody()) {
$body = $redirectRequest->getBody();
if ($body->ftell() && !$body->rewind()) {
throw new CouldNotRewindStreamException('Unable to rewind the non-seekable entity body of the request after redirecting. cURL probably ' . 'sent part of body before the redirect occurred. Try adding acustom rewind function using on the ' . 'entity body of the request using setRewindFunction().');
}
}
return $redirectRequest;
}
示例6: __construct
/**
* Client constructor
*
* @param string $baseUrl Base URL of the web service
* @param array|Collection $config Configuration settings
*/
public function __construct($baseUrl = '', $config = null)
{
$this->setConfig($config ?: new Collection());
$this->setBaseUrl($baseUrl);
$this->defaultHeaders = new Collection();
$this->setRequestFactory(RequestFactory::getInstance());
}
示例7: __construct
/**
* Constructor override
*/
public function __construct(Collection $config)
{
$this->setConfig($config);
$this->setBaseUrl($config->get(Options::BASE_URL));
$this->defaultHeaders = new Collection();
$this->setRequestFactory(RequestFactory::getInstance());
}
示例8: testProperlyValidatesWhenUsingContentEncoding
public function testProperlyValidatesWhenUsingContentEncoding()
{
$plugin = new Md5ValidatorPlugin(true);
$request = RequestFactory::getInstance()->create('GET', 'http://www.test.com/');
$request->getEventDispatcher()->addSubscriber($plugin);
// Content-MD5 is the MD5 hash of the canonical content after all
// content-encoding has been applied. Because cURL will automatically
// decompress entity bodies, we need to re-compress it to calculate.
$body = EntityBody::factory('abc');
$body->compress();
$hash = $body->getContentMd5();
$body->uncompress();
$response = new Response(200, array('Content-MD5' => $hash, 'Content-Encoding' => 'gzip'), 'abc');
$request->dispatch('request.complete', array('response' => $response));
$this->assertEquals('abc', $response->getBody(true));
// Try again with an unknown encoding
$response = new Response(200, array('Content-MD5' => $hash, 'Content-Encoding' => 'foobar'), 'abc');
$request->dispatch('request.complete', array('response' => $response));
// Try again with compress
$body->compress('bzip2.compress');
$response = new Response(200, array('Content-MD5' => $body->getContentMd5(), 'Content-Encoding' => 'compress'), 'abc');
$request->dispatch('request.complete', array('response' => $response));
// Try again with encoding and disabled content-encoding checks
$request->getEventDispatcher()->removeSubscriber($plugin);
$plugin = new Md5ValidatorPlugin(false);
$request->getEventDispatcher()->addSubscriber($plugin);
$request->dispatch('request.complete', array('response' => $response));
}
示例9: testCreatesCanonicalizedString
/**
* @dataProvider signatureDataProvider
*/
public function testCreatesCanonicalizedString($input, $result, $expires = null)
{
$signature = new S3Signature();
$request = \Guzzle\Http\Message\RequestFactory::getInstance()->create($input['verb'], 'http://' . $input['headers']['Host'] . $input['path'], $input['headers']);
$request->setClient($this->getServiceBuilder()->get('s3'));
$this->assertEquals($result, $signature->createCanonicalizedString($request), $expires);
}
示例10: testSettingBody
public function testSettingBody()
{
$request = \Guzzle\Http\Message\RequestFactory::getInstance()->create('PUT', 'http://www.test.com/');
$request->setBody(EntityBody::factory('test'));
$this->assertEquals(4, (string) $request->getHeader('Content-Length'));
$this->assertFalse($request->hasHeader('Transfer-Encoding'));
}
示例11: testErrorDescriptions
/**
* @dataProvider getErrorMapping
* @param string $httpMethod
* @param int $statusCode
* @param string $description
*/
public function testErrorDescriptions($httpMethod, $statusCode, $description)
{
$request = RequestFactory::getInstance()->fromMessage("{$httpMethod} /container/ HTTP/1.1\r\n" . "Host: www.foo.bar\r\n");
$response = new Response($statusCode);
$prevException = BadResponseException::factory($request, $response);
$httpException = HttpException::factory($prevException);
$this->assertEquals($description, $httpException->getDescription());
}
示例12: testAddsDebugLoggerWhenSetToDebug
/**
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage GET http://www.example.com/ - 503 Service Unavailable
*/
public function testAddsDebugLoggerWhenSetToDebug()
{
list($config, $plugin, $resolver) = $this->getMocks();
$config->set(Options::BACKOFF_LOGGER, 'debug');
$client = $this->getMock('Aws\\Common\\Client\\DefaultClient', array(), array(), '', false);
$resolver->resolve($config, $client);
$listeners = $plugin->getEventDispatcher()->getListeners();
$this->assertArrayHasKey('plugins.backoff.retry', $listeners);
$plugin->dispatch('plugins.backoff.retry', array('request' => RequestFactory::getInstance()->create('GET', 'http://www.example.com'), 'response' => new Response(503), 'retries' => 3, 'delay' => 2));
}
示例13: testMasksCurlExceptions
/**
* @covers Guzzle\Http\Plugin\AsyncPlugin::onRequestTimeout
*/
public function testMasksCurlExceptions()
{
$p = new AsyncPlugin();
$request = RequestFactory::getInstance()->create('PUT', 'http://www.example.com');
$e = new CurlException('Error');
$event = new Event(array('request' => $request, 'exception' => $e));
$p->onRequestTimeout($event);
$this->assertEquals(RequestInterface::STATE_COMPLETE, $request->getState());
$this->assertEquals(200, $request->getResponse()->getStatusCode());
$this->assertTrue($request->getResponse()->hasHeader('X-Guzzle-Async'));
}
示例14: getMocks
/**
* @return array
*/
protected function getMocks()
{
$that = $this;
$logger = new ClosureLogAdapter(function ($message) use($that) {
$that->message .= $message . "\n";
});
$logPlugin = new BackoffLogger($logger);
$response = new Response(503);
$request = RequestFactory::getInstance()->create('PUT', 'http://www.example.com', array('Content-Length' => 3, 'Foo' => 'Bar'));
return array($logPlugin, $request, $response);
}
示例15: parseRequestFromResponse
/**
* @param Response $response
* @param string $path
* @throws UnexpectedValueException
* @return UnifiedRequest
*/
private function parseRequestFromResponse(Response $response, $path)
{
try {
$requestInfo = Util::deserialize($response->getBody());
} catch (UnexpectedValueException $e) {
throw new UnexpectedValueException(sprintf('Cannot deserialize response from "%s": "%s"', $path, $response->getBody()), null, $e);
}
$request = RequestFactory::getInstance()->fromMessage($requestInfo['request']);
$params = $this->configureRequest($request, $requestInfo['server'], isset($requestInfo['enclosure']) ? $requestInfo['enclosure'] : []);
return new UnifiedRequest($request, $params);
}