public function execute()
    {
        $body = '';
        $classes = array();
        $batchHttpTemplate = <<<EOF
--%s
Content-Type: application/http
Content-Transfer-Encoding: binary
MIME-Version: 1.0
Content-ID: %s

%s%s
%s


EOF;
        /** @var Google_Http_Request $req */
        foreach ($this->requests as $key => $request) {
            $firstLine = sprintf('%s %s HTTP/%s', $request->getMethod(), $request->getResource(), $request->getProtocolVersion());
            $content = (string) $request->getBody();
            $body .= sprintf($batchHttpTemplate, $this->boundary, $key, $firstLine, Request::getHeadersAsString($request), $content ? "\n" . $content : '');
            $classes['response-' . $key] = $request->getHeader('X-Php-Expected-Class');
        }
        $body .= "--{$this->boundary}--";
        $body = trim($body);
        $url = Google_Client::API_BASE_PATH . '/' . self::BATCH_PATH;
        $headers = array('Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary), 'Content-Length' => strlen($body));
        $request = $this->client->getHttpClient()->createRequest('POST', $url, ['headers' => $headers, 'body' => Stream::factory($body)]);
        $response = $this->client->getHttpClient()->send($request);
        return $this->parseResponse($response, $classes);
    }
 private function fetchResumeUri()
 {
     $result = null;
     $body = $this->request->getBody();
     if ($body) {
         $headers = array('content-type' => 'application/json; charset=UTF-8', 'content-length' => $body->getSize(), 'x-upload-content-type' => $this->mimeType, 'x-upload-content-length' => $this->size, 'expect' => '');
         foreach ($headers as $key => $value) {
             $this->request->setHeader($key, $value);
         }
     }
     $response = $this->client->getHttpClient()->send($this->request);
     $location = $response->getHeader('location');
     $code = $response->getStatusCode();
     if (200 == $code && true == $location) {
         return $location;
     }
     $message = $code;
     $body = $response->json();
     if (isset($body['error']['errors'])) {
         $message .= ': ';
         foreach ($body['error']['errors'] as $error) {
             $message .= "{$error[domain]}, {$error[message]};";
         }
         $message = rtrim($message, ';');
     }
     $error = "Failed to start the resumable upload (HTTP {$message})";
     $this->client->getLogger()->error($error);
     throw new Google_Exception($error);
 }
 public function execute()
 {
     $body = '';
     $classes = array();
     /** @var Google_Http_Request $req */
     foreach ($this->requests as $key => $request) {
         $request->addHeaders(['Content-Type' => 'application/http', 'Content-Transfer-Encoding' => 'binary', 'MIME-Version' => '1.0', 'Content-ID' => $key]);
         $body .= "--{$this->boundary}";
         $body .= Request::getHeadersAsString($request) . "\n\n";
         $body .= sprintf('%s %s HTTP/%s', $request->getMethod(), $request->getResource(), $request->getProtocolVersion());
         $body .= "\n\n";
         $classes['response-' . $key] = $request->getHeader('X-Php-Expected-Class');
     }
     $body .= "--{$this->boundary}--";
     $body = trim($body);
     $url = Google_Client::API_BASE_PATH . '/' . self::BATCH_PATH;
     $headers = array('Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary), 'Content-Length' => strlen($body));
     $request = $this->client->getHttpClient()->createRequest('POST', $url, ['headers' => $headers, 'body' => Stream::factory($body)]);
     $response = $this->client->getHttpClient()->send($request);
     return $this->parseResponse($response, $classes);
 }
 /**
  * TODO: This function needs simplifying.
  * @param $name
  * @param $arguments
  * @param $expected_class - optional, the expected class name
  * @return Google_Http_Request|expected_class
  * @throws Google_Exception
  */
 public function call($name, $arguments, $expected_class = null)
 {
     if (!isset($this->methods[$name])) {
         $this->client->getLogger()->error('Service method unknown', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name));
         throw new Google_Exception("Unknown function: " . "{$this->serviceName}->{$this->resourceName}->{$name}()");
     }
     $method = $this->methods[$name];
     $parameters = $arguments[0];
     // postBody is a special case since it's not defined in the discovery
     // document as parameter, but we abuse the param entry for storing it.
     $postBody = null;
     if (isset($parameters['postBody'])) {
         if ($parameters['postBody'] instanceof Google_Model) {
             // In the cases the post body is an existing object, we want
             // to use the smart method to create a simple object for
             // for JSONification.
             $parameters['postBody'] = $parameters['postBody']->toSimpleObject();
         } else {
             if (is_object($parameters['postBody'])) {
                 // If the post body is another kind of object, we will try and
                 // wrangle it into a sensible format.
                 $parameters['postBody'] = $this->convertToArrayAndStripNulls($parameters['postBody']);
             }
         }
         $postBody = (array) $parameters['postBody'];
         unset($parameters['postBody']);
     }
     // TODO: optParams here probably should have been
     // handled already - this may well be redundant code.
     if (isset($parameters['optParams'])) {
         $optParams = $parameters['optParams'];
         unset($parameters['optParams']);
         $parameters = array_merge($parameters, $optParams);
     }
     if (!isset($method['parameters'])) {
         $method['parameters'] = array();
     }
     $method['parameters'] = array_merge($this->stackParameters, $method['parameters']);
     foreach ($parameters as $key => $val) {
         if ($key != 'postBody' && !isset($method['parameters'][$key])) {
             $this->client->getLogger()->error('Service parameter unknown', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $key));
             throw new Google_Exception("({$name}) unknown parameter: '{$key}'");
         }
     }
     foreach ($method['parameters'] as $paramName => $paramSpec) {
         if (isset($paramSpec['required']) && $paramSpec['required'] && !isset($parameters[$paramName])) {
             $this->client->getLogger()->error('Service parameter missing', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $paramName));
             throw new Google_Exception("({$name}) missing required param: '{$paramName}'");
         }
         if (isset($parameters[$paramName])) {
             $value = $parameters[$paramName];
             $parameters[$paramName] = $paramSpec;
             $parameters[$paramName]['value'] = $value;
             unset($parameters[$paramName]['required']);
         } else {
             // Ensure we don't pass nulls.
             unset($parameters[$paramName]);
         }
     }
     $this->client->getLogger()->info('Service Call', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'arguments' => $parameters));
     $url = $this->createRequestUri($method['path'], $parameters);
     $http = $this->client->getHttpClient();
     $this->client->authorize($http);
     // Guzzle 5 cannot locate App Engine certs by default,
     // so we tell Guzzle where to look
     if ($this->client->isAppEngine()) {
         $http->setDefaultOption('verify', '/etc/ca-certificates.crt');
     }
     $request = $http->createRequest($method['httpMethod'], $url, ['json' => $postBody]);
     if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') {
         $expected_class = null;
     }
     if ($this->client->shouldDefer()) {
         // @TODO find a better way to do this
         $request->setHeader('X-Php-Expected-Class', $expected_class);
         return $request;
     }
     // support uploads
     if (isset($parameters['data'])) {
         $mimeType = isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream';
         $data = $parameters['data']['value'];
         $upload = new Google_Http_MediaFileUpload($this->client, $request, $mimeType, $data);
     }
     if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') {
         $expected_class = null;
     }
     return $this->client->execute($request, $expected_class);
 }
 public function testAppEngineVerifyConfig()
 {
     $this->onlyGuzzle5();
     $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
     $client = new Google_Client();
     $this->assertEquals('/etc/ca-certificates.crt', $client->getHttpClient()->getDefaultOption('verify'));
     unset($_SERVER['SERVER_SOFTWARE']);
 }
 /**
  * @requires extension Memcached
  */
 public function testAppEngineAutoConfig()
 {
     $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
     $client = new Google_Client();
     $this->assertInstanceOf('Google_Cache_Memcache', $client->getCache());
     // check Stream Handler is used
     $http = $client->getHttpClient();
     $class = new ReflectionClass(get_class($http));
     $property = $class->getProperty('fsm');
     $property->setAccessible(true);
     $fsm = $property->getValue($http);
     $class = new ReflectionClass(get_class($fsm));
     $property = $class->getProperty('handler');
     $property->setAccessible(true);
     $handler = $property->getValue($fsm);
     $this->assertInstanceOf('GuzzleHttp\\Ring\\Client\\StreamHandler', $handler);
     unset($_SERVER['SERVER_SOFTWARE']);
 }
Example #7
0
<?php

require_once 'vendor/autoload.php';
require_once 'config/google.auth.php';
require_once 'misc/helpers.php';
use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;
session_start();
$client = new Google_Client();
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUrl);
$client->setScopes(array('https://spreadsheets.google.com/feeds'));
//Prevent error with authentication
$client->getHttpClient()->setDefaultOption('verify', __DIR__ . DIRECTORY_SEPARATOR . 'cacert.pem');
print '<a href="' . $client->createAuthUrl() . '">Authenticate to Google Account</a>';
if (isset($_GET['code'])) {
    try {
        $client->authenticate($_GET['code']);
        debug($client->getAccessToken());
        $accessToken = $client->getAccessToken();
        $accessToken = $accessToken["access_token"];
        $serviceRequest = new DefaultServiceRequest($accessToken);
        ServiceRequestFactory::setInstance($serviceRequest);
        $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
        $spreadsheetFeed = $spreadsheetService->getSpreadsheets();
        $spreadsheet = $spreadsheetFeed->getByTitle('Knihovna');
        $worksheetFeed = $spreadsheet->getWorksheets();
        $worksheet = $worksheetFeed->getByTitle('List');
        $rowCount = $worksheet->getRowCount();
        $colCount = $worksheet->getColCount();
 public function execute()
 {
     $responses = Pool::batch($this->client->getHttpClient(), $this->requests);
     return $this->parseResponse($responses);
 }