I am having an issue with updating Google Sheet
data. For updating data I am using this Library and it works properly if you call direct file and if you run on local environment
But the problem is when I run from server to update data it gives
500 internal error
and if I refresh the page it works properly.
Basically it's not working first time .
According to my error_log file the error was
Warning: htmlspecialchars() expects parameter 1 to be string
<?php
require 'vendor/autoload.php';
use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;
//error_reporting(E_ALL);
//ini_set('display_errors', 'On');
class APIClass {
function __construct($token){
$serviceRequest = new DefaultServiceRequest($token);
ServiceRequestFactory::setInstance($serviceRequest);
}
function updateNow($sku,$quantity){
$this->updateNow($sku,$quantity);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed();
$spreadsheet = $spreadsheetFeed->getByTitle('product');
$worksheetFeed = $spreadsheet->getWorksheetFeed();
$worksheet = $worksheetFeed->getByTitle('Sheet2');
$listFeed = $worksheet->getListFeed();
$Entries = $listFeed->getEntries();
$i = 0;
$update = false;
foreach ($listFeed->getEntries() as $entry) {
$values[$i] = $entry->getValues();
$i++;
}
for($k=0;$k<=$i;$k++){
for($j=1;$j<=16;$j++){
if($values[$k]['sku'] == $sku){
$listEntry = $Entries[$k];
$values['quantity'] = $quantity;
$listEntry->update($values);
$update = true;
}
}
}
}
}
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:
%s
", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
//$client->setAccessToken($accessToken);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
$client = getClient();
$obj = new APIClass($client->getAccessToken()['access_token']);
$obj->updateNow("Temp2","100");
?>
Little Help will be appreciated..Thank you
Finally found that i was passing multidimensional and after passing it to foreach it becomes single array but what i need is string value so it get solved by passing single dimensional array also put your code into try catch block which prevent me from getting 'BadRequestException' from google sheet api
Thank you guys.