php-google-spreadsheet-client icon indicating copy to clipboard operation
php-google-spreadsheet-client copied to clipboard

String could not be parsed as XML

Open zak905 opened this issue 10 years ago • 9 comments

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:

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 " Authenticate "; echo "
"; 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
"; echo "$spreadsheetName
"; } } ``` } 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

zak905 avatar Feb 02 '15 20:02 zak905

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

asimlqt avatar Feb 05 '15 21:02 asimlqt

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.

zak905 avatar Feb 08 '15 15:02 zak905

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

kanafghan avatar Feb 21 '15 15:02 kanafghan

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.

SimonEast avatar Mar 17 '15 03:03 SimonEast

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.

mTorres avatar May 07 '15 12:05 mTorres

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

sagark1510 avatar Nov 17 '15 12:11 sagark1510

@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?

ronakprogrammer avatar Mar 02 '16 12:03 ronakprogrammer

@ronakprogrammer

Yes that is odd.

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

asimlqt avatar Apr 23 '16 15:04 asimlqt

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

Jiys avatar Dec 05 '17 18:12 Jiys