本文整理汇总了PHP中Guzzle\Http\EntityBody类的典型用法代码示例。如果您正苦于以下问题:PHP EntityBody类的具体用法?PHP EntityBody怎么用?PHP EntityBody使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EntityBody类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: transfer
/**
* {@inheritdoc}
*/
protected function transfer()
{
while (!$this->stopped && !$this->source->isConsumed()) {
if ($this->source->getContentLength() && $this->source->isSeekable()) {
// If the stream is seekable and the Content-Length known, then stream from the data source
$body = new ReadLimitEntityBody($this->source, $this->partSize, $this->source->ftell());
} else {
// We need to read the data source into a temporary buffer before streaming
$body = EntityBody::factory();
while ($body->getContentLength() < $this->partSize && $body->write($this->source->read(max(1, min(10 * Size::KB, $this->partSize - $body->getContentLength()))))) {
}
}
// @codeCoverageIgnoreStart
if ($body->getContentLength() == 0) {
break;
}
// @codeCoverageIgnoreEnd
$params = $this->state->getUploadId()->toParams();
$command = $this->client->getCommand('UploadPart', array_replace($params, array('PartNumber' => count($this->state) + 1, 'Body' => $body, 'ContentMD5' => (bool) $this->options['part_md5'], Ua::OPTION => Ua::MULTIPART_UPLOAD)));
// Notify observers that the part is about to be uploaded
$eventData = $this->getEventData();
$eventData['command'] = $command;
$this->dispatch(self::BEFORE_PART_UPLOAD, $eventData);
// Allow listeners to stop the transfer if needed
if ($this->stopped) {
break;
}
$response = $command->getResponse();
$this->state->addPart(UploadPart::fromArray(array('PartNumber' => count($this->state) + 1, 'ETag' => $response->getHeader('ETag', true), 'Size' => $body->getContentLength(), 'LastModified' => gmdate(DateFormat::RFC2822))));
// Notify observers that the part was uploaded
$this->dispatch(self::AFTER_PART_UPLOAD, $eventData);
}
}
示例2: setBody
/**
* Set the body of the request
*
* @param string|resource|EntityBody $body Body to use in the entity body
* of the request
* @param string $contentType (optional) Content-Type to set. Leave null
* to use an existing Content-Type or to guess the Content-Type
* @param bool $tryChunkedTransfer (optional) Set to TRUE to try to use
* Tranfer-Encoding chunked
*
* @return EntityEnclosingRequest
* @throws RequestException if the protocol is < 1.1 and Content-Length can
* not be determined
*/
public function setBody($body, $contentType = null, $tryChunkedTransfer = false)
{
$this->body = EntityBody::factory($body);
$this->removeHeader('Content-Length');
$this->setHeader('Expect', '100-Continue');
if ($contentType) {
$this->setHeader('Content-Type', (string) $contentType);
}
if ($tryChunkedTransfer) {
$this->setHeader('Transfer-Encoding', 'chunked');
} else {
$this->removeHeader('Transfer-Encoding');
// Set the Content-Length header if it can be determined
$size = $this->body->getContentLength();
if ($size !== null && $size !== false) {
$this->setHeader('Content-Length', $size);
} else {
if ('1.1' == $this->protocolVersion) {
$this->setHeader('Transfer-Encoding', 'chunked');
} else {
throw new RequestException('Cannot determine entity body ' . 'size and cannot use chunked Transfer-Encoding when ' . 'using HTTP/' . $this->protocolVersion);
}
}
}
return $this;
}
示例3: create
/**
* {@inheritdoc}
*/
public function create($method, $url, $headers = null, $body = null)
{
if ($method != 'POST' && $method != 'PUT' && $method != 'PATCH') {
$c = $this->requestClass;
$request = new $c($method, $url, $headers);
if ($body) {
$request->setResponseBody(EntityBody::factory($body));
}
return $request;
}
$c = $this->entityEnclosingRequestClass;
$request = new $c($method, $url, $headers);
if ($body) {
$isChunked = (string) $request->getHeader('Transfer-Encoding') == 'chunked';
if ($method == 'POST' && (is_array($body) || $body instanceof Collection)) {
$request->addPostFields($body);
if ($isChunked) {
$request->setHeader('Transfer-Encoding', 'chunked');
}
} elseif (is_resource($body) || $body instanceof EntityBody) {
$request->setBody($body, (string) $request->getHeader('Content-Type'), $isChunked);
} else {
$request->setBody((string) $body, (string) $request->getHeader('Content-Type'), $isChunked);
}
}
return $request;
}
示例4: testCreatesPutRequests
/**
* @covers Guzzle\Http\Message\RequestFactory::create
*/
public function testCreatesPutRequests()
{
// Test using a string
$request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, 'Data');
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
$this->assertEquals('PUT', $request->getMethod());
$this->assertEquals('http', $request->getScheme());
$this->assertEquals('http://www.google.com/path?q=1&v=2', $request->getUrl());
$this->assertEquals('www.google.com', $request->getHost());
$this->assertEquals('/path', $request->getPath());
$this->assertEquals('/path?q=1&v=2', $request->getResource());
$this->assertInstanceOf('Guzzle\\Http\\EntityBody', $request->getBody());
$this->assertEquals('Data', (string) $request->getBody());
unset($request);
// Test using an EntityBody
$request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, EntityBody::factory('Data'));
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
$this->assertEquals('Data', (string) $request->getBody());
// Test using a resource
$resource = fopen('php://temp', 'w+');
fwrite($resource, 'Data');
$request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, $resource);
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
$this->assertEquals('Data', (string) $request->getBody());
// Test using an object that can be cast as a string
$request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, Url::factory('http://www.example.com/'));
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
$this->assertEquals('http://www.example.com/', (string) $request->getBody());
}
示例5: receiveResponseHeader
/**
* Receive a response header from curl
*
* @param resource $curl Curl handle
* @param string $header Received header
*
* @return int
*/
public function receiveResponseHeader($curl, $header)
{
static $normalize = array("\r", "\n");
$length = strlen($header);
$header = str_replace($normalize, '', $header);
if (strpos($header, 'HTTP/') === 0) {
$startLine = explode(' ', $header, 3);
$code = $startLine[1];
$status = isset($startLine[2]) ? $startLine[2] : '';
// Only download the body of the response to the specified response
// body when a successful response is received.
if ($code >= 200 && $code < 300) {
$body = $this->request->getResponseBody();
} else {
$body = EntityBody::factory();
}
$response = new Response($code, null, $body);
$response->setStatus($code, $status);
$this->request->startResponse($response);
$this->request->dispatch('request.receive.status_line', array('request' => $this, 'line' => $header, 'status_code' => $code, 'reason_phrase' => $status));
} elseif ($pos = strpos($header, ':')) {
$this->request->getResponse()->addHeader(trim(substr($header, 0, $pos)), trim(substr($header, $pos + 1)));
}
return $length;
}
示例6: create
/**
* {@inheritdoc}
*/
public function create($method, $url, $headers = null, $body = '', array $options = array())
{
$c = $this->entityEnclosingRequestClass;
$request = new $c($method, $url, $headers);
$request->setBody(EntityBody::factory($body));
return $request;
}
示例7: setBody
public function setBody($body, $contentType = null)
{
$this->body = EntityBody::factory($body);
if ($contentType === null && !$this->hasHeader('Content-Type')) {
$contentType = $this->body->getContentType();
}
if ($contentType) {
$this->setHeader('Content-Type', $contentType);
}
if (!$this->body->isSeekable() && $this->expectCutoff !== false) {
$this->setHeader('Expect', '100-Continue');
}
$size = $this->body->getContentLength();
if ($size !== null && $size !== false) {
$this->setHeader('Content-Length', $size);
if ($size > $this->expectCutoff) {
$this->setHeader('Expect', '100-Continue');
}
} elseif (!$this->hasHeader('Content-Length')) {
if ('1.1' == $this->protocolVersion) {
$this->setHeader('Transfer-Encoding', 'chunked');
} else {
throw new RequestException('Cannot determine Content-Length and cannot use chunked Transfer-Encoding when using HTTP/1.0');
}
}
return $this;
}
示例8: buildCoreRequest
protected function buildCoreRequest(HttpRequest $request)
{
$headers = $request->getHeaders();
$contentLength = 0;
if (!$request->isParameterInUrl()) {
$body = $request->getParameterString();
$contentLength = strlen($body);
} else {
$body = $request->getContent();
if ($body !== null) {
AssertUtils::assertSet(HttpHeaders::CONTENT_LENGTH, $headers);
$contentLength = (int) $headers[HttpHeaders::CONTENT_LENGTH];
}
}
$entity = null;
$headers[HttpHeaders::CONTENT_LENGTH] = (string) $contentLength;
if ($body !== null) {
$entity = new ReadLimitEntityBody(EntityBody::factory($body), $contentLength, $request->getOffset() !== false ? $request->getOffset() : 0);
}
$coreRequest = $this->client->createRequest($request->getMethod(), $request->getFullUrl(), $headers, $entity);
if ($request->getResponseBody() != null) {
$coreRequest->setResponseBody($request->getResponseBody());
}
return $coreRequest;
}
示例9: 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));
}
示例10: createTransferAction
protected function createTransferAction(\SplFileInfo $file)
{
// Open the file for reading
$filename = $file->getPathName();
if (!($resource = fopen($filename, 'r'))) {
// @codeCoverageIgnoreStart
throw new RuntimeException("Could not open {$filename} for reading");
// @codeCoverageIgnoreEnd
}
$key = $this->options['source_converter']->convert($filename);
$body = EntityBody::factory($resource);
// Determine how the ACL should be applied
if ($acl = $this->options['acl']) {
$aclType = is_string($this->options['acl']) ? 'ACL' : 'ACP';
} else {
$acl = 'private';
$aclType = 'ACL';
}
// Use a multi-part upload if the file is larger than the cutoff size and is a regular file
if ($body->getWrapper() == 'plainfile' && $file->getSize() >= $this->options['multipart_upload_size']) {
$builder = UploadBuilder::newInstance()->setBucket($this->options['bucket'])->setKey($key)->setMinPartSize($this->options['multipart_upload_size'])->setOption($aclType, $acl)->setClient($this->options['client'])->setSource($body)->setConcurrency($this->options['concurrency']);
$this->dispatch(self::BEFORE_MULTIPART_BUILD, array('builder' => $builder, 'file' => $file));
return $builder->build();
}
return $this->options['client']->getCommand('PutObject', array('Bucket' => $this->options['bucket'], 'Key' => $key, 'Body' => $body, $aclType => $acl));
}
示例11: save
public function save($vaultName, $file, $description = null)
{
if (!$this->vaultExist($vaultName)) {
$this->createVault($vaultName);
}
if (!is_file($file)) {
throw new \Exception('File not exist');
}
$options = array('vaultName' => $vaultName, 'body' => EntityBody::factory(fopen($file, 'r')));
/*if (filesize($file) > 1024000) {
$multiupload = $this->glacier->initiateMultipartUpload(array(
'vaultName' => $vaultName,
'archiveDescription' => $description,
'partSize' => '4194304'
));
$options['uploadId'] = $multiupload->get('uploadId');
$options['range'] = filesize($file);
$this->glacier->uploadMultipartPart($options);
$result = $this->glacier->completeMultipartUpload(array(
'vaultName' => $vaultName,
'uploadId' => $multiupload->get('uploadId'),
));
} else {*/
if ($description) {
$options['archiveDescription'] = $description;
}
$result = $this->glacier->uploadArchive($options);
//}
$archiveId = $result->get('archiveId');
return $archiveId ? true : false;
}
示例12: putObject
public function putObject($bucket, $key, $filePath, $options)
{
$options['Bucket'] = $bucket;
$options['Key'] = $key;
$options['Body'] = EntityBody::factory(fopen($filePath, 'r+'));
return $this->s3->putObject($options);
}
示例13: 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'));
}
示例14: setBody
/**
* {@inheritdoc}
*/
public function setBody($body, $contentType = null, $tryChunkedTransfer = false)
{
$this->body = EntityBody::factory($body);
$this->removeHeader('Content-Length');
if ($contentType) {
$this->setHeader('Content-Type', (string) $contentType);
}
// Always add the Expect 100-Continue header if the body cannot be rewound. This helps with redirects.
if (!$this->body->isSeekable() && $this->expectCutoff !== false) {
$this->setHeader('Expect', '100-Continue');
}
if ($tryChunkedTransfer) {
$this->setHeader('Transfer-Encoding', 'chunked');
} else {
$this->removeHeader('Transfer-Encoding');
// Set the Content-Length header if it can be determined
$size = $this->body->getContentLength();
if ($size !== null && $size !== false) {
$this->setHeader('Content-Length', $size);
if ($size > $this->expectCutoff) {
$this->setHeader('Expect', '100-Continue');
}
} elseif ('1.1' == $this->protocolVersion) {
$this->setHeader('Transfer-Encoding', 'chunked');
} else {
throw new RequestException('Cannot determine Content-Length and cannot use chunked Transfer-Encoding when using HTTP/1.0');
}
}
return $this;
}
示例15: setBody
public function setBody($body, $contentType = null)
{
$this->body = EntityBody::factory($body);
// Auto detect the Content-Type from the path of the request if possible
if ($contentType === null && !$this->hasHeader('Content-Type')) {
$contentType = $this->body->getContentType() ?: Mimetypes::getInstance()->fromFilename($this->getPath());
}
if ($contentType) {
$this->setHeader('Content-Type', $contentType);
}
// Always add the Expect 100-Continue header if the body cannot be rewound. This helps with redirects.
if (!$this->body->isSeekable() && $this->expectCutoff !== false) {
$this->setHeader('Expect', '100-Continue');
}
// Set the Content-Length header if it can be determined
$size = $this->body->getContentLength();
if ($size !== null && $size !== false) {
$this->setHeader('Content-Length', $size);
if ($size > $this->expectCutoff) {
$this->setHeader('Expect', '100-Continue');
}
} elseif (!$this->hasHeader('Content-Length')) {
if ('1.1' == $this->protocolVersion) {
$this->setHeader('Transfer-Encoding', 'chunked');
} else {
throw new RequestException('Cannot determine Content-Length and cannot use chunked Transfer-Encoding when using HTTP/1.0');
}
}
return $this;
}