php-google-spreadsheet-client
php-google-spreadsheet-client copied to clipboard
String could not be parsed as XML
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
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
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.
I did the same as @zak905 described above and it worked fine for me. I'm on Windows 8.1 using the Chrome browser.
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
.
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.
@zak905 Got the same issue and you are right CURLOPT_SSL_VERIFYPEER = False
is the solution.
@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
Yes that is odd.
Are you using windows? Also can you provide the full code you are using?
@asimlqt I had the exact same problem as @ronakprogrammer mentioned above, did you figure it out? Please Help!