本文整理匯總了PHP中Google_Http_Request::getRequestMethod方法的典型用法代碼示例。如果您正苦於以下問題:PHP Google_Http_Request::getRequestMethod方法的具體用法?PHP Google_Http_Request::getRequestMethod怎麽用?PHP Google_Http_Request::getRequestMethod使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Google_Http_Request
的用法示例。
在下文中一共展示了Google_Http_Request::getRequestMethod方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: executeRequest
/**
* Execute an HTTP Request
*
* @param Google_Http_Request $request the http request to be executed
* @return array containing response headers, body, and http code
* @throws Google_IO_Exception on curl or IO error
*/
public function executeRequest(Google_Http_Request $request)
{
$curl = curl_init();
if ($request->getPostBody()) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody());
}
$requestHeaders = $request->getRequestHeaders();
if ($requestHeaders && is_array($requestHeaders)) {
$curlHeaders = array();
foreach ($requestHeaders as $k => $v) {
$curlHeaders[] = "{$k}: {$v}";
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
}
curl_setopt($curl, CURLOPT_URL, $request->getUrl());
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod());
curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent());
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
// 1 is CURL_SSLVERSION_TLSv1, which is not always defined in PHP.
// UpdraftPlus patch
// The SDK leaves this on the default setting in later releases
// curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if ($request->canGzip()) {
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
}
$options = $this->client->getClassConfig('Google_IO_Curl', 'options');
if (is_array($options)) {
$this->setOptions($options);
}
foreach ($this->options as $key => $var) {
curl_setopt($curl, $key, $var);
}
if (!isset($this->options[CURLOPT_CAINFO])) {
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem');
}
$this->client->getLogger()->debug('cURL request', array('url' => $request->getUrl(), 'method' => $request->getRequestMethod(), 'headers' => $requestHeaders, 'body' => $request->getPostBody()));
$response = curl_exec($curl);
if ($response === false) {
$error = curl_error($curl);
$code = curl_errno($curl);
$map = $this->client->getClassConfig('Google_IO_Exception', 'retry_map');
$this->client->getLogger()->error('cURL ' . $error);
throw new Google_IO_Exception($error, $code, null, $map);
}
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize);
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$this->client->getLogger()->debug('cURL response', array('code' => $responseCode, 'headers' => $responseHeaders, 'body' => $responseBody));
return array($responseBody, $responseHeaders, $responseCode);
}
示例2: decodeHttpResponse
/**
* Decode an HTTP Response.
* @static
* @throws Google_Service_Exception
* @param Google_Http_Request $response The http response to be decoded.
* @return mixed|null
*/
public static function decodeHttpResponse($response)
{
$code = $response->getResponseHttpCode();
$body = $response->getResponseBody();
$decoded = null;
if (intVal($code) >= 300) {
$decoded = json_decode($body, true);
$err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl();
if (isset($decoded['error']) && isset($decoded['error']['message']) && isset($decoded['error']['code'])) {
// if we're getting a json encoded error definition, use that instead of the raw response
// body for improved readability
$err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}";
} else {
$err .= ": ({$code}) {$body}";
}
$errors = null;
// Specific check for APIs which don't return error details, such as Blogger.
if (isset($decoded['error']) && isset($decoded['error']['errors'])) {
$errors = $decoded['error']['errors'];
}
throw new Google_Service_Exception($err, $code, null, $errors);
}
// Only attempt to decode the response, if the response code wasn't (204) 'no content'
if ($code != '204') {
$decoded = json_decode($body, true);
if ($decoded === null || $decoded === "") {
throw new Google_Service_Exception("Invalid json in service response: {$body}");
}
if ($response->getExpectedClass()) {
$class = $response->getExpectedClass();
$decoded = new $class($decoded);
}
}
return $decoded;
}
示例3: executeRequest
/**
* Execute an HTTP Request
*
* @param Google_HttpRequest $request the http request to be executed
* @return Google_HttpRequest http request with the response http code,
* response headers and response body filled in
* @throws Google_IO_Exception on curl or IO error
*/
public function executeRequest(Google_Http_Request $request)
{
$default_options = stream_context_get_options(stream_context_get_default());
$requestHttpContext = array_key_exists('http', $default_options) ? $default_options['http'] : array();
if ($request->getPostBody()) {
$requestHttpContext["content"] = $request->getPostBody();
}
$requestHeaders = $request->getRequestHeaders();
if ($requestHeaders && is_array($requestHeaders)) {
$headers = "";
foreach ($requestHeaders as $k => $v) {
$headers .= "{$k}: {$v}\r\n";
}
$requestHttpContext["header"] = $headers;
}
$requestHttpContext["method"] = $request->getRequestMethod();
$requestHttpContext["user_agent"] = $request->getUserAgent();
$requestSslContext = array_key_exists('ssl', $default_options) ? $default_options['ssl'] : array();
if (!array_key_exists("cafile", $requestSslContext)) {
$requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem';
}
$options = array("http" => array_merge(self::$DEFAULT_HTTP_CONTEXT, $requestHttpContext), "ssl" => array_merge(self::$DEFAULT_SSL_CONTEXT, $requestSslContext));
$context = stream_context_create($options);
$url = $request->getUrl();
if ($request->canGzip()) {
$url = self::ZLIB . $url;
}
// We are trapping any thrown errors in this method only and
// throwing an exception.
$this->trappedErrorNumber = null;
$this->trappedErrorString = null;
// START - error trap.
set_error_handler(array($this, 'trapError'));
$fh = fopen($url, 'r', false, $context);
restore_error_handler();
// END - error trap.
if ($this->trappedErrorNumber) {
throw new Google_IO_Exception(sprintf("HTTP Error: Unable to connect: '%s'", $this->trappedErrorString), $this->trappedErrorNumber);
}
$response_data = false;
$respHttpCode = self::UNKNOWN_CODE;
if ($fh) {
if (isset($this->options[self::TIMEOUT])) {
stream_set_timeout($fh, $this->options[self::TIMEOUT]);
}
$response_data = stream_get_contents($fh);
fclose($fh);
$respHttpCode = $this->getHttpResponseCode($http_response_header);
}
if (false === $response_data) {
throw new Google_IO_Exception(sprintf("HTTP Error: Unable to connect: '%s'", $respHttpCode), $respHttpCode);
}
$responseHeaders = $this->getHttpResponseHeaders($http_response_header);
return array($response_data, $responseHeaders, $respHttpCode);
}
示例4: executeRequest
/**
* Execute an HTTP Request
*
* @param Google_Http_Request $request the http request to be executed
* @return array containing response headers, body, and http code
* @throws Google_IO_Exception on curl or IO error
*/
public function executeRequest(Google_Http_Request $request)
{
$client = $this->getGuzzleClient();
$options = array('exceptions' => true);
$method = $request->getRequestMethod();
if (strcasecmp($method, 'get') == 0) {
$req = $client->get($request->getUrl(), $request->getRequestHeaders(), $options);
} elseif (strcasecmp($method, 'post') == 0) {
$req = $client->post($request->getUrl(), $request->getRequestHeaders(), $request->getPostBody(), $options);
} elseif (strcasecmp($method, 'delete') == 0) {
$req = $client->delete($request->getUrl(), $request->getRequestHeaders(), null, $options);
} elseif (strcasecmp($method, 'put') == 0) {
$req = $client->put($request->getUrl(), $request->getRequestHeaders(), $request->getPostBody(), $options);
} else {
throw new Google_IO_Exception(__CLASS__ . '#' . __LINE__ . ' Request method not supported');
}
if ($request->canGzip()) {
$req->addHeader('Accept-Encoding', 'gzip,deflate');
}
$this->client->getLogger()->debug('guzzle request', array('url' => $request->getUrl(), 'method' => $request->getRequestMethod(), 'headers' => $req->getHeaders(), 'body' => $request->getPostBody()));
try {
$result = $client->send($req);
$rawHeaders = $result->getRawHeaders();
$responseHeaders = $this->getHttpResponseHeaders($rawHeaders);
$responseBody = $result->getBody();
$responseCode = $result->getStatusCode();
} catch (Exception $ex) {
// check if http response info can be retrieved from exception
// this gives calling service opportunity to handle failures
if ($ex instanceof Guzzle\Http\Exception\BadResponseException) {
$response = $ex->getResponse();
$responseBody = $response->getBody();
$rawHeaders = $response->getRawHeaders();
$responseHeaders = $this->getHttpResponseHeaders($rawHeaders);
$responseCode = $response->getStatusCode();
} else {
throw $ex;
}
}
$this->client->getLogger()->debug('guzzle response', array('code' => $responseCode, 'headers' => $responseHeaders, 'body' => $responseBody));
return array($responseBody, $responseHeaders, $responseCode);
}
示例5: isRequestCacheable
/**
* Check if an HTTP request can be cached by a private local cache.
*
* @static
* @param Google_Http_Request $resp
* @return bool True if the request is cacheable.
* False if the request is uncacheable.
*/
public static function isRequestCacheable(Google_Http_Request $resp)
{
$method = $resp->getRequestMethod();
if (!in_array($method, self::$CACHEABLE_HTTP_METHODS)) {
return false;
}
// Don't cache authorized requests/responses.
// [rfc2616-14.8] When a shared cache receives a request containing an
// Authorization field, it MUST NOT return the corresponding response
// as a reply to any other request...
if ($resp->getRequestHeader("authorization")) {
return false;
}
return true;
}
示例6: executeRequest
/**
* Execute an HTTP Request
*
* @param Google_HttpRequest $request the http request to be executed
*
* @return Google_HttpRequest http request with the response http code,
* response headers and response body filled in
* @throws Google_IO_Exception on curl or IO error
*/
public function executeRequest(Google_Http_Request $request)
{
$default_options = stream_context_get_options(stream_context_get_default());
$requestHttpContext = array_key_exists('http', $default_options) ? $default_options['http'] : array();
if ($request->getPostBody()) {
$requestHttpContext["content"] = $request->getPostBody();
}
$requestHeaders = $request->getRequestHeaders();
if ($requestHeaders && is_array($requestHeaders)) {
$headers = "";
foreach ($requestHeaders as $k => $v) {
$headers .= "{$k}: {$v}\r\n";
}
$requestHttpContext["header"] = $headers;
}
$requestHttpContext["method"] = $request->getRequestMethod();
$requestHttpContext["user_agent"] = $request->getUserAgent();
$requestSslContext = array_key_exists('ssl', $default_options) ? $default_options['ssl'] : array();
if (!array_key_exists("cafile", $requestSslContext)) {
$requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem';
}
$options = array("http" => array_merge(self::$DEFAULT_HTTP_CONTEXT, $requestHttpContext), "ssl" => array_merge(self::$DEFAULT_SSL_CONTEXT, $requestSslContext));
$context = stream_context_create($options);
$url = $request->getUrl();
if ($request->canGzip()) {
$url = self::ZLIB . $url;
}
// Not entirely happy about this, but supressing the warning from the
// fopen seems like the best situation here - we can't do anything
// useful with it, and failure to connect is a legitimate run
// time situation.
@($fh = fopen($url, 'r', false, $context));
$response_data = false;
$respHttpCode = self::UNKNOWN_CODE;
if ($fh) {
if (isset($this->options[self::TIMEOUT])) {
stream_set_timeout($fh, $this->options[self::TIMEOUT]);
}
$response_data = stream_get_contents($fh);
fclose($fh);
$respHttpCode = $this->getHttpResponseCode($http_response_header);
}
if (false === $response_data) {
throw new Google_IO_Exception(sprintf("HTTP Error: Unable to connect: '%s'", $respHttpCode), $respHttpCode);
}
$responseHeaders = $this->getHttpResponseHeaders($http_response_header);
return array($response_data, $responseHeaders, $respHttpCode);
}
示例7: decodeHttpResponse
/**
* Decode an HTTP Response.
* @static
* @throws Google_Service_Exception
* @param Google_Http_Request $response The http response to be decoded.
* @param Google_Client $client
* @return mixed|null
*/
public static function decodeHttpResponse($response, Google_Client $client = null)
{
$code = $response->getResponseHttpCode();
$body = $response->getResponseBody();
$decoded = null;
if (intVal($code) >= 300) {
$decoded = json_decode($body, true);
$err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl();
if (isset($decoded['error']) && isset($decoded['error']['message']) && isset($decoded['error']['code'])) {
// if we're getting a json encoded error definition, use that instead of the raw response
// body for improved readability
$err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}";
} else {
$err .= ": ({$code}) {$body}";
}
$errors = null;
// Specific check for APIs which don't return error details, such as Blogger.
if (isset($decoded['error']) && isset($decoded['error']['errors'])) {
$errors = $decoded['error']['errors'];
}
$map = null;
if ($client) {
$client->getLogger()->error($err, array('code' => $code, 'errors' => $errors));
$map = $client->getClassConfig('Google_Service_Exception', 'retry_map');
}
//throw new Google_Service_Exception($err, $code, null, $errors, $map);
echo json_encode(array('status' => FALSE, 'response-code' => $code, 'response' => $err, 'message' => $errors, "map" => $map));
die;
}
// Only attempt to decode the response, if the response code wasn't (204) 'no content'
if ($code != '204') {
$decoded = json_decode($body, true);
if ($decoded === null || $decoded === "") {
$error = "Invalid json in service response: {$body}";
if ($client) {
$client->getLogger()->error($error);
}
//throw new Google_Service_Exception($error);
echo json_encode(array('status' => FALSE, 'response-code' => $code, 'response' => $error, 'message' => $body));
die;
}
if ($response->getExpectedClass()) {
$class = $response->getExpectedClass();
$decoded = new $class($decoded);
}
}
return $decoded;
}
示例8: executeRequest
/**
* Execute an HTTP Request
*
* @param Google_HttpRequest $request the http request to be executed
* @return Google_HttpRequest http request with the response http code,
* response headers and response body filled in
* @throws Google_IO_Exception on curl or IO error
*/
public function executeRequest(Google_Http_Request $request)
{
$curl = curl_init();
if ($request->getPostBody()) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody());
}
$requestHeaders = $request->getRequestHeaders();
if ($requestHeaders && is_array($requestHeaders)) {
$curlHeaders = array();
foreach ($requestHeaders as $k => $v) {
$curlHeaders[] = "{$k}: {$v}";
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod());
curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent());
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_URL, $request->getUrl());
if ($request->canGzip()) {
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
}
foreach ($this->options as $key => $var) {
curl_setopt($curl, $key, $var);
}
if (!isset($this->options[CURLOPT_CAINFO])) {
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem');
}
$response = curl_exec($curl);
if ($response === false) {
throw new Google_IO_Exception(curl_error($curl));
}
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$responseBody = substr($response, $headerSize);
$responseHeaderString = substr($response, 0, $headerSize);
$responseHeaders = $this->getHttpResponseHeaders($responseHeaderString);
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
return array($responseBody, $responseHeaders, $responseCode);
}
示例9: executeRequest
/**
* Execute an HTTP Request
*
* @param Google_Http_Request $request the http request to be executed
* @return array containing response headers, body, and http code
* @throws Google_IO_Exception on curl or IO error
*/
public function executeRequest(Google_Http_Request $request)
{
$default_options = stream_context_get_options(stream_context_get_default());
$requestHttpContext = array_key_exists('http', $default_options) ? $default_options['http'] : array();
if ($request->getPostBody()) {
$requestHttpContext["content"] = $request->getPostBody();
}
$requestHeaders = $request->getRequestHeaders();
if ($requestHeaders && is_array($requestHeaders)) {
$headers = "";
foreach ($requestHeaders as $k => $v) {
$headers .= "{$k}: {$v}\r\n";
}
$requestHttpContext["header"] = $headers;
}
$requestHttpContext["method"] = $request->getRequestMethod();
$requestHttpContext["user_agent"] = $request->getUserAgent();
$requestSslContext = array_key_exists('ssl', $default_options) ? $default_options['ssl'] : array();
# UpdraftPlus patch
// if (!array_key_exists("cafile", $requestSslContext)) {
// $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem';
// }
$url = $request->getUrl();
if (preg_match('#^https?://([^/]+)/#', $url, $umatches)) {
$cname = $umatches[1];
} else {
$cname = false;
}
# UpdraftPlus patch
// Added
if (empty($this->options['disable_verify_peer'])) {
$requestSslContext['verify_peer'] = true;
if (version_compare(PHP_VERSION, '5.6.0', '>=')) {
if (!empty($cname)) {
$requestSslContext['peer_name'] = $cname;
}
} else {
if (!empty($cname)) {
$requestSslContext['CN_match'] = $cname;
$retry_on_fail = true;
}
}
} else {
$requestSslContext['allow_self_signed'] = true;
}
if (!empty($this->options['cafile'])) {
$requestSslContext['cafile'] = $this->options['cafile'];
}
$options = array("http" => array_merge(self::$DEFAULT_HTTP_CONTEXT, $requestHttpContext), "ssl" => array_merge($requestSslContext));
$context = stream_context_create($options);
# UpdraftPlus patch
// $url = $request->getUrl();
if ($request->canGzip()) {
$url = self::ZLIB . $url;
}
$this->client->getLogger()->debug('Stream request', array('url' => $url, 'method' => $request->getRequestMethod(), 'headers' => $requestHeaders, 'body' => $request->getPostBody()));
// We are trapping any thrown errors in this method only and
// throwing an exception.
$this->trappedErrorNumber = null;
$this->trappedErrorString = null;
// START - error trap.
set_error_handler(array($this, 'trapError'));
$fh = fopen($url, 'r', false, $context);
# UpdraftPLus patch
if (!$fh && isset($retry_on_fail) && !empty($cname) && 'www.googleapis.com' == $cname) {
// Reset
$this->trappedErrorNumber = null;
$this->trappedErrorString = null;
global $updraftplus;
$updraftplus->log("Using Stream, and fopen failed; retrying different CN match to try to overcome");
// www.googleapis.com does not match the cert now being presented - *.storage.googleapis.com; presumably, PHP's stream handler isn't handling alternative names properly. Rather than turn off all verification, let's retry with a new name to match.
$options['ssl']['CN_match'] = 'www.storage.googleapis.com';
$context = stream_context_create($options);
$fh = fopen($url, 'r', false, $context);
}
restore_error_handler();
// END - error trap.
if ($this->trappedErrorNumber) {
$error = sprintf("HTTP Error: Unable to connect: '%s'", $this->trappedErrorString);
$this->client->getLogger()->error('Stream ' . $error);
throw new Google_IO_Exception($error, $this->trappedErrorNumber);
}
$response_data = false;
$respHttpCode = self::UNKNOWN_CODE;
if ($fh) {
if (isset($this->options[self::TIMEOUT])) {
stream_set_timeout($fh, $this->options[self::TIMEOUT]);
}
$response_data = stream_get_contents($fh);
fclose($fh);
$respHttpCode = $this->getHttpResponseCode($http_response_header);
}
if (false === $response_data) {
//.........這裏部分代碼省略.........
示例10: processEntityRequest
/**
* @visible for testing
* Process an http request that contains an enclosed entity.
* @param Google_Http_Request $request
* @return Google_Http_Request Processed request with the enclosed entity.
*/
public function processEntityRequest(Google_Http_Request $request)
{
$postBody = $request->getPostBody();
$contentType = $request->getRequestHeader("content-type");
// Set the default content-type as application/x-www-form-urlencoded.
if (false == $contentType && $request->getRequestMethod() != 'POST') {
$contentType = self::FORM_URLENCODED;
$request->setRequestHeaders(array('content-type' => $contentType));
}
// Force the payload to match the content-type asserted in the header.
if ($contentType == self::FORM_URLENCODED && is_array($postBody)) {
$postBody = http_build_query($postBody, '', '&');
$request->setPostBody($postBody);
}
// Make sure the content-length header is set.
if (!$postBody || is_string($postBody)) {
$postsLength = strlen($postBody);
$request->setRequestHeaders(array('content-length' => $postsLength));
}
return $request;
}
示例11: executeRequest
/**
* Execute an API request.
*
* This is a copy/paste from the parent class that uses Moodle's implementation
* of curl. Portions have been removed or altered.
*
* @param Google_Http_Request $request the http request to be executed
* @return Google_Http_Request http request with the response http code, response
* headers and response body filled in
* @throws Google_IO_Exception on curl or IO error
*/
public function executeRequest(Google_Http_Request $request)
{
$curl = new curl();
if ($request->getPostBody()) {
$curl->setopt(array('CURLOPT_POSTFIELDS' => $request->getPostBody()));
}
$requestHeaders = $request->getRequestHeaders();
if ($requestHeaders && is_array($requestHeaders)) {
$curlHeaders = array();
foreach ($requestHeaders as $k => $v) {
$curlHeaders[] = "{$k}: {$v}";
}
$curl->setopt(array('CURLOPT_HTTPHEADER' => $curlHeaders));
}
$curl->setopt(array('CURLOPT_URL' => $request->getUrl()));
$curl->setopt(array('CURLOPT_CUSTOMREQUEST' => $request->getRequestMethod()));
$curl->setopt(array('CURLOPT_USERAGENT' => $request->getUserAgent()));
$curl->setopt(array('CURLOPT_FOLLOWLOCATION' => false));
$curl->setopt(array('CURLOPT_SSL_VERIFYPEER' => true));
$curl->setopt(array('CURLOPT_RETURNTRANSFER' => true));
$curl->setopt(array('CURLOPT_HEADER' => true));
if ($request->canGzip()) {
$curl->setopt(array('CURLOPT_ENCODING' => 'gzip,deflate'));
}
$curl->setopt($this->options);
$respdata = $this->do_request($curl, $request);
$infos = $curl->get_info();
$respheadersize = $infos['header_size'];
$resphttpcode = (int) $infos['http_code'];
$curlerrornum = $curl->get_errno();
$curlerror = $curl->error;
if ($respdata != CURLE_OK) {
throw new Google_IO_Exception($curlerror);
}
list($responseHeaders, $responseBody) = $this->parseHttpResponse($respdata, $respheadersize);
return array($responseBody, $responseHeaders, $resphttpcode);
}