I am trying to update a google spreadsheet using PHP. Currently the code reliably connects and prints values, but when I try to update values, I get:
Fatal error: Uncaught exception 'Google_Exception' with message '(update) missing required param: 'spreadsheetId''
$service = new Google_Service_Sheets($client);
$spreadsheetId = '[MyID]';
$range = 'Sheet1!A2:E';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (count($values) == 0) {
print "No data found.\n";
} else {
foreach ($values as $row) {
// Print columns A and E, which correspond to indices 0 and 4.
printf("%s, %s<br>", $row[0], $row[4]);
}
}
$range = 'Sheet1!A2:E2';
$values = [1,2,3,4,5];
$body = new Google_Service_Sheets_ValueRange(['values'=>$values]);
$service->spreadsheets_values->update($spreadsheetId,'Sheet1!A2:E',$body,'raw');
The get() call works perfectly, using the same spreadsheet ID. The update call says that it is missing the spreadsheet ID parameter, but prints the correct spreadsheet ID in the call stack.
Is there an issue with the way I am passing the ID in the update call?
Issue was not actually with spreadsheet ID, but with the way $values and the value input option was passed in. $values should be a two dimensional array, and value input option should be an array not a string. posted the corrected parts of code below for posterity.
$range = 'Sheet1!A2:E2';
$values = [[1,2,3,4,5]];
$inputoption = ['valueInputOption' => "RAW"];
$body = new Google_Service_Sheets_ValueRange(['values'=>$values]);
$service->spreadsheets_values->update($spreadsheetId,$range,$body,$inputoption);
It looks like you are not passing any Oauth2 login credentials, which if I'm not mistaken, is required to add or update information through a Google API (though reading the information does not require this).
https://developers.google.com/sheets/api/quickstart/php
OR, you are not passing the correct information in $value when trying to update (it may be looking for a specific spreadsheet ID, not a range.
Related
I'm using the ZohoCRM PHP SDK to attempt to pull all Account records from the CRM and manipulate them locally (do some reports). The basic code looks like this, which works fine:
$account_module = ZCRMModule::getInstance('Accounts');
$response = $account_module->getRecords();
$records = $response->getData();
foreach ($records as $record) {
// do stuff
}
The problem is that the $records object only has 200 records (out of about 3000 total). I can't find any docs in the (minimally / poorly documented) SDK documentation showing how to paginate or get bigger result sets, and the Zoho code samples in the dev site don't seem to be using the same SDK for some reason.
Does anyone know how I can paginate through these records?
The getRecords() method seems to accept 2 parameters. This is used within some of their examples. You should be able to use those params to set/control pagination.
$param_map = ["page" => "20", "per_page" => "200"];
$response = $account_module->getRecords($param_map);
#dakdad was right that you can pass in the page and per page values into the param_map. You also should use the $response->getInfo()->getMoreRecords() to determine if you need to paginate. Something like this seems to work:
$account_module = ZCRMModule::getInstance('Accounts');
$page = 1;
$has_more = true;
while ($has_more) {
$param_map = ["page" => $page, "per_page" => "200"];
$response = $account_module->getRecords($param_map);
$has_more = $response->getInfo()->getMoreRecords();
$records = $response->getData();
foreach ($records as $record) {
// do stuff
}
$page++;
}
I am looking for a little assistance on listing available phone numbers for purchase using Twilios API and PHP for their 5.X API Verison. Below is the error I get and the PHP im using. Im sure im just overlooking something:
PHP Notice: Trying to get property of non-object in /twilio-php-app/findnumbers.php on line 16
PHP Warning: Invalid argument supplied for foreach() in /twilio-php-app/findnumbers.php on line 16
<?php
// Get the PHP helper library from https://twilio.com/docs/libraries/php
require_once 'vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "Axxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$token = "removed";
$client = new Client($sid, $token);
$numbers = $client->availablePhoneNumbers('US')->local->read(
array("areaCode" => "513")
);
foreach($numbers->availablephonenumbers as $number) {
echo $number->phone_number;
}
If I echo $numbers I find it is an array. Here is the raw output where I just want to get the "phone_number": "xxxxxx" output; minus the "phone_number": part.
output of array Screenshot
Adding to this, if I run the PHP as the following; I get single number outputs
$numbers = $client->availablePhoneNumbers('US')->local->read(
array("areaCode" => "513")
);
echo $numbers[1]->phoneNumber;
Changing the value of [1] to [2] grabs the next phone number. How can I loop this?
Might not be done 100% correct but I found a solution that increases the count of the array based on count and stacks the numbers nicely.
Sharing this in case anyone else ever comes across this and needs help; this does exactly what its intended to.... Search the twilio databse for available numbers to purchase, based on criteria
<?php
// Get the PHP helper library from https://twilio.com/docs/libraries/php
require_once 'vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "your_SID";
$token = "Your_Token";
$client = new Client($sid, $token);
$numbers = $client->availablePhoneNumbers('US')->local->read(
array("areaCode" => "513")
);
for ($i = 0; $i < count($numbers); ++$i) {
print $numbers[$i]->phoneNumber . "\n";
}
Just an observation, but you are already using the availablePhoneNumbers method on $client when you say $numbers = $client->availablePhoneNumbers...
Perhaps, in the foreach, you just need to reference $numbers and not $numbers->availablephonenumbers?
I'm trying to update a template document via PHP API using this: https://github.com/docusign/docusign-php-client/blob/master/src/Api/TemplatesApi.php#L4946
I get one of two errors depending on if I set the apply_document_fields option.
Without it set, I get UNSPECIFIED ERROR Value cannot be null.\r\nParameter name: fileBytes. However, if I view the request body before sending, document_base_64 is set as expected.
With apply_document_fields set 'true' (actual boolean value is not supported), I get FORMAT_CONVERSION_ERROR The data could not be converted.
Either way, it seems like the document data is not getting sent correctly, but I can't figure out how I'm supposed to be sending it. Here's my code:
public static function updateTemplateWithDocument(string $documentId, string $templateId, $documentBody = null)
{
$api = My_Service_Docusign::getInstance();
$templatesApi = new DocuSign\eSign\Api\TemplatesApi($api->getAuth());
$document = new \DocuSign\eSign\Model\Document();
$document->setDocumentBase64(base64_encode($documentBody));
// Got an error reusing $documentId, so I'm incrementing it now
$document->setDocumentId((string) (((int)$documentId) + 1));
$def = new DocuSign\eSign\Model\EnvelopeDefinition();
$def->setDocuments(array($document));
$opts = new \DocuSign\eSign\Api\TemplatesApi\UpdateDocumentOptions();
// Different behavior with this set vs not
$opts->setApplyDocumentFields('true');
$res = $tmpApi->updateDocument($api->getAccountId(), $documentId, $templateId, $def, $opts);
return $res;
}
Unfortunately, DocuSign support doesn't support their API :-(
I figured out I need to use TemplatesApi::updateDocuments (plural) instead, which also allows me to reuse the documentId.
I am making a curl request to Amazon MWS orders API to get ListOrders then I loop through the xml response using foreach loop and prints the response in html table.
But as per Amazon MWS Orders API it only returns 100 results in one request and to get more results I need to make another curl request by using other parameter NextToken that I got from the last response of previous request which then will return next 100 orders and so on until there is no more NextToken available.
So my question is how can I iterate to the new response of NextToken request again and again and print the response in html table until there is no more NextToken available?
Report response is also avilable in XML format
For those who is still struggling and want an easy way to get orders data. Please use Amazon MWS Reports API and request a report using -
ReportType:_GET_XML_ALL_ORDERS_DATA_BY_ORDER_DATE_
For more info see
https://docs.developer.amazonservices.com/en_UK/reports/Reports_ReportType.html
This is what I use to convert the report's csv response to an array for saving into database.
$report_listing = explode("\n", $report_data["report_data"]);
$orders_list = '';
$i = 0;
$headers = '';
// Building an Associative array of CSV report
foreach($report_listing as $listing)
{
if($i == 0)
{
$headers = explode("\t", trim($listing));
}
else
{
$csv_data = explode("\t", $listing);
if(!isset($csv_data[1]))
continue;
foreach($headers as $key => $index)
{
$orders_list[$i - 1][$index] = $csv_data[$key];
}
}
$i++;
}
$new_order = array();
// Combining order items into one order array
foreach($orders_list as $orders)
{
$new_order[$orders['amazon-order-id']][] = $orders;
}
I have been trying to get a single value on google spreadsheet for my school project .Since I am still new in PHP script ,I follow the google guideline which is this link google guidelines .But I cannot find any documentation about getting value on the sheet based on my input .
I plan to use another program to input a single string value and will check whether the value exist on the spreadsheet or not . If yes it will be shown .
I am still quite new in programming with PHP so a code example/simple explanation will be nice . Thanks
You should try the quickstart for PHP. It is correct to read about the document to be familiar with the terms, request and response forms and all the basic stuff about the API. In the quickstart, you will be learning the code implementation of the API starting with the basic function like, listing (getValues()).
Here is a snippet from the quickstart :
// Prints the names and majors of students in a sample spreadsheet:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
$spreadsheetId = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms';
$range = 'Class Data!A2:E';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (count($values) == 0) {
print "No data found.\n";
} else {
print "Name, Major:\n";
foreach ($values as $row) {
// Print columns A and E, which correspond to indices 0 and 4.
printf("%s, %s\n", $row[0], $row[4]);
}
}
Hope this helps.