本文整理汇总了PHP中Guzzle\Http\Message\RequestInterface::getBody方法的典型用法代码示例。如果您正苦于以下问题:PHP RequestInterface::getBody方法的具体用法?PHP RequestInterface::getBody怎么用?PHP RequestInterface::getBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Message\RequestInterface
的用法示例。
在下文中一共展示了RequestInterface::getBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: signRequest
public function signRequest(RequestInterface $request, CredentialsInterface $credentials)
{
if ($request instanceof EntityEnclosingRequestInterface && $request->getBody()) {
$request->setHeader('X-Amz-Content-Sha256', EntityBody::getHash($request->getBody(), 'sha256'));
} else {
$request->setHeader('X-Amz-Content-Sha256', hash('sha256', ''));
}
parent::signRequest($request, $credentials);
}
示例2: readRequestBody
/**
* Read data from the request body and send it to curl
*
* @param resource $ch Curl handle
* @param resource $fd File descriptor
* @param int $length Amount of data to read
*
* @return string
*/
public function readRequestBody($ch, $fd, $length)
{
$read = '';
if ($this->request->getBody()) {
$read = $this->request->getBody()->read($length);
if ($this->emitIo) {
$this->request->dispatch('curl.callback.read', array('request' => $this->request, 'read' => $read));
}
}
return !$read ? '' : $read;
}
示例3: signRequest
/**
* Always add a x-amz-content-sha-256 for data integrity.
*/
public function signRequest(RequestInterface $request, CredentialsInterface $credentials)
{
if ($request instanceof EntityEnclosingRequestInterface && $request->getBody() && !$request->hasHeader('x-amz-content-sha256')) {
$request->setHeader('X-Amz-Content-Sha256', $this->getPresignedPayload($request));
}
parent::signRequest($request, $credentials);
}
示例4: signRequest
/**
* @param RequestInterface|EntityEnclosingRequestInterface $request
* @param Credentials $credentials
*/
public function signRequest($request, $credentials)
{
$request->setHeader('X-HMB-Signature-Method', self::DEFAULT_METHOD);
$request->setHeader('X-HMB-Signature-Version', self::DEFAULT_SIGN_VERSION);
$request->setHeader('X-HMB-TimeStamp', time());
$contentMd5 = $request instanceof EntityEnclosingRequestInterface ? md5($request->getBody()) : '';
if ($contentMd5) {
$request->setHeader('Content-MD5', $contentMd5);
}
$sign = array();
$sign[] = strtoupper($request->getMethod());
$sign[] = $request->getHost();
if ($request->getHeader('Content-MD5')) {
$sign[] = $request->getHeader('Content-MD5');
}
if ($request->getHeader('Content-Type')) {
$sign[] = $request->getHeader('Content-Type');
}
$sign[] = $request->getHeader('X-HMB-Signature-Method');
$sign[] = $request->getHeader('X-HMB-Signature-Version');
$sign[] = $request->getHeader('X-HMB-TimeStamp');
if ($request->getHeader('X-HMB-User-Session-Token')) {
$sign[] = $request->getHeader('X-HMB-User-Session-Token');
}
$sign[] = $request->getQuery(true) ? $request->getPath() . '?' . $request->getQuery(true) : $request->getPath();
$signature = base64_encode(hash_hmac(strtolower($request->getHeader('X-HMB-Signature-Method')), implode("\n", $sign), $credentials->getSecret()));
$request->setHeader('Authorization', sprintf('%s %s:%s', self::AUTHORIZATION_SCHME, $credentials->getKey(), $signature));
}
示例5: handshake
/**
* @param Guzzle\Http\Message\RequestInterface
* @return Guzzle\Http\Message\Response
*/
public function handshake(RequestInterface $request)
{
$body = $this->sign($request->getHeader('Sec-WebSocket-Key1', true), $request->getHeader('Sec-WebSocket-Key2', true), (string) $request->getBody());
$headers = array('Upgrade' => 'WebSocket', 'Connection' => 'Upgrade', 'Sec-WebSocket-Origin' => $request->getHeader('Origin', true), 'Sec-WebSocket-Location' => 'ws://' . $request->getHeader('Host', true) . $request->getPath());
$response = new Response(101, $headers, $body);
$response->setStatus(101, 'WebSocket Protocol Handshake');
return $response;
}
示例6: getDelay
/**
* {@inheritdoc}
*/
protected function getDelay($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
{
if ($response && $response->getStatusCode() == 400 && strpos($response->getBody(), self::ERR)) {
// Check if the request is sending a local file, and if so, clear the stat cache and recalculate the size.
if ($request instanceof EntityEnclosingRequestInterface) {
if ($request->getBody()->getWrapper() == 'plainfile') {
$filename = $request->getBody()->getUri();
// Clear the cache so that we send accurate file sizes
clearstatcache(true, $filename);
$length = filesize($filename);
$request->getBody()->setSize($length);
$request->setHeader('Content-Length', $length);
}
}
return true;
}
}
示例7: readRequestBody
/**
* Read data from the request body and send it to curl
*
* @param resource $ch Curl handle
* @param resource $fd File descriptor
* @param int $length Amount of data to read
*
* @return string
*/
public function readRequestBody($ch, $fd, $length)
{
if (!($body = $this->request->getBody())) {
return '';
}
$read = (string) $body->read($length);
if ($this->emitIo) {
$this->request->dispatch('curl.callback.read', array('request' => $this->request, 'read' => $read));
}
return $read;
}
示例8: handshake
/**
* @param \Guzzle\Http\Message\RequestInterface $request
* @return \Guzzle\Http\Message\Response
* @throws \UnderflowException If there hasn't been enough data received
*/
public function handshake(RequestInterface $request)
{
$body = substr($request->getBody(), 0, 8);
if (8 !== strlen($body)) {
throw new \UnderflowException("Not enough data received to issue challenge response");
}
$challenge = $this->sign((string) $request->getHeader('Sec-WebSocket-Key1'), (string) $request->getHeader('Sec-WebSocket-Key2'), $body);
$headers = array('Upgrade' => 'WebSocket', 'Connection' => 'Upgrade', 'Sec-WebSocket-Origin' => (string) $request->getHeader('Origin'), 'Sec-WebSocket-Location' => 'ws://' . (string) $request->getHeader('Host') . $request->getPath());
$response = new Response(101, $headers, $challenge);
$response->setStatus(101, 'WebSocket Protocol Handshake');
return $response;
}
示例9: __construct
public function __construct(ConnectionInterface $conn, RequestInterface $req)
{
$body = (string) $req->getBody();
if (!empty($body)) {
$query = QueryString::fromString($body);
$fields = $query->getAll();
foreach ($fields as $field => $value) {
$req->setPostField($field, $value);
}
}
$this->conn = $conn;
$this->session = $conn->Session;
$this->req = $req;
}
示例10: signRequest
/**
* Sign the Pusher request
*
* @link http://pusher.com/docs/rest_api#authentication
* @param RequestInterface $request
* @param Credentials $credentials
*/
public function signRequest(RequestInterface $request, Credentials $credentials)
{
$queryParameters = array('auth_key' => $credentials->getKey(), 'auth_timestamp' => time(), 'auth_version' => self::AUTH_VERSION);
if ($request instanceof EntityEnclosingRequestInterface) {
$body = $request->getBody();
$queryParameters['body_md5'] = $body->getContentLength() ? $body->getContentMd5() : '';
}
// The signature algorithm asks that keys are all lowercased
$queryParameters = array_change_key_case($request->getQuery()->toArray()) + $queryParameters;
$queryParameters = array_filter($queryParameters);
ksort($queryParameters);
$method = strtoupper($request->getMethod());
$requestPath = $request->getPath();
$query = urldecode(http_build_query($queryParameters));
$signature = $this->signString(implode("\n", array($method, $requestPath, $query)), $credentials);
$queryParameters['auth_signature'] = $signature;
$request->getQuery()->replace($queryParameters);
}
示例11: __toString
/**
* Returns an HTML-formatted string representation of the exception.
*
* @return string
* @internal
*/
public function __toString()
{
$msg = $this->getMessage();
if (is_object($this->requestObj) && $this->requestObj instanceof \Guzzle\Http\Message\Request) {
$request = array('url' => $this->requestObj->getUrl(), 'host' => $this->requestObj->getHost(), 'headers' => $this->requestObj->getRawHeaders(), 'query' => (string) $this->requestObj->getQuery());
if ($this->requestObj instanceof \Guzzle\Http\Message\EntityEnclosingRequestInterface) {
$request_body = $this->requestObj->getBody();
$request['content-type'] = $request_body->getContentType();
$request['content-length'] = $request_body->getContentLength();
$request['body'] = $request_body->__toString();
}
$msg .= "\n\nRequest: <pre>" . htmlspecialchars(print_r($request, true)) . '</pre>';
}
if (is_object($this->responseObj) && $this->responseObj instanceof \Guzzle\Http\Message\Response) {
$response = array('status' => $this->responseObj->getStatusCode(), 'headers' => $this->responseObj->getRawHeaders(), 'body' => $this->responseBody);
$msg .= "\n\nResponse: <pre>" . htmlspecialchars(print_r($response, true)) . '</pre>';
}
return $msg;
}
示例12: cloneRequestWithMethod
public function cloneRequestWithMethod(RequestInterface $request, $method)
{
if ($request->getClient()) {
$cloned = $request->getClient()->createRequest($method, $request->getUrl(), $request->getHeaders());
} else {
$cloned = $this->create($method, $request->getUrl(), $request->getHeaders());
}
$cloned->getCurlOptions()->replace($request->getCurlOptions()->toArray());
$cloned->setEventDispatcher(clone $request->getEventDispatcher());
if (!($cloned instanceof EntityEnclosingRequestInterface)) {
$cloned->removeHeader('Content-Length');
} elseif ($request instanceof EntityEnclosingRequestInterface) {
$cloned->setBody($request->getBody());
}
$cloned->getParams()->replace($request->getParams()->toArray());
$cloned->dispatch('request.clone', array('request' => $cloned));
return $cloned;
}
示例13: collectRequest
/**
* Collect & sanitize data about a Guzzle request
*
* @param Guzzle\Http\Message\RequestInterface $request
*
* @return array
*/
private function collectRequest(GuzzleRequestInterface $request)
{
$body = null;
if ($request instanceof EntityEnclosingRequestInterface) {
$body = (string) $request->getBody();
}
return array('headers' => $request->getHeaders(), 'method' => $request->getMethod(), 'scheme' => $request->getScheme(), 'host' => $request->getHost(), 'port' => $request->getPort(), 'path' => $request->getPath(), 'query' => $request->getQuery(), 'body' => $body);
}
示例14: factory
/**
* Factory method to create a new curl handle based on an HTTP request.
*
* There are some helpful options you can set to enable specific behavior:
* - debug: Set to true to enable cURL debug functionality to track the actual headers sent over the wire.
* - progress: Set to true to enable progress function callbacks.
*
* @param RequestInterface $request Request
*
* @return CurlHandle
* @throws RuntimeException
*/
public static function factory(RequestInterface $request)
{
$requestCurlOptions = $request->getCurlOptions();
$mediator = new RequestMediator($request, $requestCurlOptions->get('emit_io'));
$tempContentLength = null;
$method = $request->getMethod();
$bodyAsString = $requestCurlOptions->get(self::BODY_AS_STRING);
// Array of default cURL options.
$curlOptions = array(CURLOPT_URL => $request->getUrl(), CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_RETURNTRANSFER => false, CURLOPT_HEADER => false, CURLOPT_PORT => $request->getPort(), CURLOPT_HTTPHEADER => array(), CURLOPT_HEADERFUNCTION => array($mediator, 'receiveResponseHeader'), CURLOPT_HTTP_VERSION => $request->getProtocolVersion() === '1.0' ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1, CURLOPT_SSL_VERIFYPEER => 1, CURLOPT_SSL_VERIFYHOST => 2);
if (defined('CURLOPT_PROTOCOLS')) {
// Allow only HTTP and HTTPS protocols
$curlOptions[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
}
// Add CURLOPT_ENCODING if Accept-Encoding header is provided
if ($acceptEncodingHeader = $request->getHeader('Accept-Encoding')) {
$curlOptions[CURLOPT_ENCODING] = (string) $acceptEncodingHeader;
// Let cURL set the Accept-Encoding header, prevents duplicate values
$request->removeHeader('Accept-Encoding');
}
// Enable the progress function if the 'progress' param was set
if ($requestCurlOptions->get('progress')) {
$curlOptions[CURLOPT_PROGRESSFUNCTION] = array($mediator, 'progress');
$curlOptions[CURLOPT_NOPROGRESS] = false;
}
// Enable curl debug information if the 'debug' param was set
if ($requestCurlOptions->get('debug')) {
$curlOptions[CURLOPT_STDERR] = fopen('php://temp', 'r+');
// @codeCoverageIgnoreStart
if (false === $curlOptions[CURLOPT_STDERR]) {
throw new RuntimeException('Unable to create a stream for CURLOPT_STDERR');
}
// @codeCoverageIgnoreEnd
$curlOptions[CURLOPT_VERBOSE] = true;
}
// HEAD requests need no response body, everything else might
if ($method != 'HEAD') {
$curlOptions[CURLOPT_WRITEFUNCTION] = array($mediator, 'writeResponseBody');
}
// Specify settings according to the HTTP method
switch ($method) {
case 'GET':
$curlOptions[CURLOPT_HTTPGET] = true;
break;
case 'HEAD':
$curlOptions[CURLOPT_NOBODY] = true;
break;
case 'POST':
$curlOptions[CURLOPT_POST] = true;
// Special handling for POST specific fields and files
if (count($request->getPostFiles())) {
$fields = $request->getPostFields()->useUrlEncoding(false)->urlEncode();
foreach ($request->getPostFiles() as $key => $data) {
$prefixKeys = count($data) > 1;
foreach ($data as $index => $file) {
// Allow multiple files in the same key
$fieldKey = $prefixKeys ? "{$key}[{$index}]" : $key;
$fields[$fieldKey] = $file->getCurlString();
}
}
$curlOptions[CURLOPT_POSTFIELDS] = $fields;
$request->removeHeader('Content-Length');
} elseif (count($request->getPostFields())) {
$curlOptions[CURLOPT_POSTFIELDS] = (string) $request->getPostFields()->useUrlEncoding(true);
$request->removeHeader('Content-Length');
} elseif (!$request->getBody()) {
// Need to remove CURLOPT_POST to prevent chunked encoding for an empty POST
unset($curlOptions[CURLOPT_POST]);
$curlOptions[CURLOPT_CUSTOMREQUEST] = 'POST';
}
break;
case 'PUT':
case 'PATCH':
case 'DELETE':
default:
$curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
if (!$bodyAsString) {
$curlOptions[CURLOPT_UPLOAD] = true;
// Let cURL handle setting the Content-Length header
if ($tempContentLength = $request->getHeader('Content-Length')) {
$tempContentLength = (int) (string) $tempContentLength;
$curlOptions[CURLOPT_INFILESIZE] = $tempContentLength;
}
} elseif (!$request->hasHeader('Content-Type')) {
// Remove the curl generated Content-Type header if none was set manually
$curlOptions[CURLOPT_HTTPHEADER][] = 'Content-Type:';
}
}
// Special handling for requests sending raw data
//.........这里部分代码省略.........
示例15: perform
/**
* Perform a http request and return the response
*
* @param \Guzzle\Http\Message\RequestInterface $request the request to preform
* @param bool $async whether or not to perform an async request
*
* @return \Guzzle\Http\Message\Response|mixed the response from elastic search
* @throws \Exception
*/
public function perform(\Guzzle\Http\Message\RequestInterface $request, $async = false)
{
try {
$profileKey = null;
if ($this->enableProfiling) {
$profileKey = __METHOD__ . '(' . $request->getUrl() . ')';
if ($request instanceof \Guzzle\Http\Message\EntityEnclosingRequest) {
$profileKey .= " " . $request->getBody();
}
Yii::beginProfile($profileKey);
}
$response = $async ? $request->send() : json_decode($request->send()->getBody(true), true);
Yii::trace("Sent request to '{$request->getUrl()}'", 'application.elastic.connection');
if ($this->enableProfiling) {
Yii::endProfile($profileKey);
}
return $response;
} catch (\Guzzle\Http\Exception\BadResponseException $e) {
$body = $e->getResponse()->getBody(true);
if (($msg = json_decode($body)) !== null && isset($msg->error)) {
throw new \CException($msg->error);
} else {
throw new \CException($e);
}
} catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
throw new \CException($e->getResponse()->getBody(true));
}
}