public function execute() { $body = ''; /** @var Yoast_Google_HttpRequest $req */ foreach ($this->requests as $key => $req) { $body .= "--{$this->boundary}\n"; $body .= $req->toBatchString($key) . "\n"; } $body = rtrim($body); $body .= "\n--{$this->boundary}--"; global $apiConfig; $url = $apiConfig['basePath'] . '/batch'; $httpRequest = new Yoast_Google_HttpRequest($url, 'POST'); $httpRequest->setRequestHeaders(array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary)); $httpRequest->setPostBody($body); $response = Yoast_Google_Client::$io->makeRequest($httpRequest); $response = $this->parseResponse($response); return $response; }
/** * @visible for testing * Process an http request that contains an enclosed entity. * @param Yoast_Google_HttpRequest $request * @return Yoast_Google_HttpRequest Processed request with the enclosed entity. */ protected function processEntityRequest(Yoast_Google_HttpRequest $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; }