/** * 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); }
private function getResumeUri() { $result = null; $body = $this->request->getPostBody(); if ($body) { $headers = array('content-type' => 'application/json; charset=UTF-8', 'content-length' => Google_Utils::getStrLen($body), 'x-upload-content-type' => $this->mimeType, 'x-upload-content-length' => $this->size, 'expect' => ''); $this->request->setRequestHeaders($headers); } $response = $this->client->getIo()->makeRequest($this->request); var_dump($response); $location = $response->getResponseHeader('location'); $code = $response->getResponseHttpCode(); if (200 == $code && true == $location) { return $location; } throw new Google_Exception("Failed to start the resumable upload"); }
/** * @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) { $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; }