asimlqt / php-google-spreadsheet-client

A PHP library for accessing and manipulating Google Spreadsheets
Other
543 stars 154 forks source link

String could not be parsed as XML #56

Open zak905 opened 9 years ago

zak905 commented 9 years ago

Hello,

I am new to php, but I have used Google Spreadsheets API in Java and I understand how it works. I have an issue with this simple example that tries to get the list of Spreadsheets. and print their names. My Code:

<?php

require 'vendor/autoload.php'; //Google OAuth Client require_once realpath(dirname(FILE) . '/../google-api-php-client/vendor/autoload.php');

use Google\Spreadsheet\DefaultServiceRequest; use Google\Spreadsheet\ServiceRequestFactory;

Class SpreadsheetImporter { function SpreadsheetImporter(){ $client_id = 'my_id'; $client_secret = 'my_client_secret'; // $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; $redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'; $client = new Google_Client(); $client->setClientId($client_id); $client->setClientSecret($client_secret); $client->setRedirectUri($redirect_uri); $client->addScope("https://spreadsheets.google.com/feeds");

        if (isset($_REQUEST['code']) ) {
            $code = $_REQUEST['code'];
              $response = $client->authenticate($code);

             $_SESSION['access_token'] = $client->getAccessToken();
           }

            if (isset($_SESSION['access_token'])) {
                 $code = $_SESSION['access_token'];
              $client->setAccessToken($code);
            } else{
              $authUrl = $client->createAuthUrl();
      echo "<a  href=$authUrl target = \"_blank\");\"> Authenticate </a>";
      echo "<form action=". $_SERVER['PHP_SELF'] ." method=\"POST\"> <input type=\"text\" name=\"code\"> <input type=\"submit\" value=\"authenticate\"></form>";

                try{
                  $accessToken= $client->getAccessToken();
                  $_SESSION['access_token'] = $accessToken;
                 }catch(Exception $e){
                     echo 'Message1: ' .$e->getMessage();
                 }
            }
            $code = $_SESSION['access_token'];

            $obj = json_decode($code, true);
            $access_token_code = $obj['access_token'];

            $this->serviceRequest = new DefaultServiceRequest($access_token_code);

            ServiceRequestFactory::setInstance($this->serviceRequest);

}

function getSpreadsheetList(){
     $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
     $spreadsheetFeed = $spreadsheetService->getSpreadsheets();

    foreach($spreadsheetFeed->xml->entry as $spreadsheetName){
           echo "here <br>";
           echo "$spreadsheetName <br>";

     }

}

}

try{ $spreadsheetimporterService = new SpreadsheetImporter(); $spreadsheetimporterService->getSpreadsheetList();

} catch(Exception $e){ echo 'Message2: ' .$e->getMessage();

}

However, I get the following exception: String could not be parsed as XML.

Anybody who has a hint or suggestions for solving this issue would be much appreciated. Thanks

Regards, Zakaria

asimlqt commented 9 years ago

Hi,

I'm not sure your way of generating the token is correct. The method you seem to be using is for installed applications and php applications are generally classed as "Web Applications". This is the only method i've used and tested.

Can you please clone the following project and try to generate an access token using the method described in the README. if you still have trouble then let me know.

https://github.com/asimlqt/php-google-oauth

zak905 commented 9 years ago

Thanks for your feedback. Your Oauth generation works fine.

I discovered the root of the issue, which has nothing to do with OAuth but with SSL.

I added the following code to execute() function in DefaultServiceRequest.php:

$ret=curl_exec($ch);

if(ret === false)
{
    echo "Error Number:".curl_errno($ch)."<br>";
    echo "Error String:".curl_error($ch);
}

and I got the following Error message: SSL certificate problem, verify that the CA cert is OK.

And the turnaround is by setting this option to false: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false) ; during initialization.

I am not sure if it is valid for Ubuntu as well, but in my case, (on windows), It solved the issue.

kanafghan commented 9 years ago

I did the same as @zak905 described above and it worked fine for me. I'm on Windows 8.1 using the Chrome browser.

SimonEast commented 9 years ago

Yes, if you're developing on Windows machines, you usually need to disable CURLOPT_SSL_VERIFYPEER. I did this by editing initRequest() inside /vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/DefaultServiceRequest.php.

mTorres commented 9 years ago

You shouln't disable SSL verification as you are opening yourself to MIM attacks. Instead just download the CA bundle from the cURL guys and update your php.ini:

[CURL]
curl.cainfo = "path/to/cacert.pem"

BTW, I've also made a PR to check the returned value from curl_exec because I've found myself with the same problem.

sagark1510 commented 8 years ago

@zak905 Got the same issue and you are right CURLOPT_SSL_VERIFYPEER = False is the solution.

ronakprogrammer commented 8 years ago

@asimlqt I tried with setting "CURLOPT_SSL_VERIFYPEER = false" but still for some case it throws error.sometimes work and sometimes suddenly throws error there is not any issue with access token also because I am regenerating the token when making calls to spreadsheet api.Its weird to see such kind of result that sometimes work and sometimes don't work and throws error like this "String could not be parsed as XML"

Can you tell me whats wrong there?

asimlqt commented 8 years ago

@ronakprogrammer

Yes that is odd.

Are you using windows? Also can you provide the full code you are using?

Jiys commented 6 years ago

@asimlqt I had the exact same problem as @ronakprogrammer mentioned above, did you figure it out? Please Help!