I am using Google APIs Client Library for PHP to create a spreadsheet. I am creating a spreadsheet with 3 sheets in it. Now I need to insert a row on each sheet before attaching them to the spreadsheet. Is there a way to populate the sheets with some data? All I want is to create the sheet in one request with all the data I need. Please see the below code for a better understanding:
<?php
class GoogleSheetFunctions {
public function create_new() {
$client = $this->get_client();
if ( is_null( $client ) ) {
return false;
}
$service = new Sheets( $client );
$spreadsheet = $this->set_spreadsheet_title();
$spreadsheet = $this->set_spreadsheet_with_sheets( $spreadsheet );
// making the request to create a new spreadsheet in google
try {
$response = $service->spreadsheets->create( $spreadsheet );
$spreadsheet_id = $response->spreadsheetId;
} catch ( Exception $ex ) {
return false;
}
if ( ! $permission ) {
return false;
}
return true;
}
protected function set_spreadsheet_with_sheets( Spreadsheet $spreadsheet ) {
$sheet_names = [
'sheet-no-1',
'sheet-no-2',
'sheet-no-3',
];
$sheets = [];
// creating sheets as per sheet names
foreach ( $sheet_names as $sheet ) {
$sheet_properties = new SheetProperties();
$sheet_properties->setTitle( $sheet );
$temp_sheet = new Sheet();
$temp_sheet->setProperties( $sheet_properties );
// generate a first row with custom data for each sheet
$sheets[] = $temp_sheet;
}
// set the created sheets to the main spreadsheet
$spreadsheet->setSheets( $sheets );
return $spreadsheet;
}
protected function set_spreadsheet_title() {
$month = current_datetime()->format( 'F-Y' );
$spreadsheet_name = $month;
$spreadsheet_props = new SpreadsheetProperties();
$spreadsheet_props->setTitle( $spreadsheet_name );
$spreadsheet = new Spreadsheet();
$spreadsheet->setProperties( $spreadsheet_props );
return $spreadsheet;
}
protected function get_client( $scopes = [] ) {
$credential = 'location/of/credential.json';
$scopes = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/spreadsheets',
];
return new Client( [
'credentials' => $credential,
'scopes' => $scopes,
] );
}
}
Feel free to notify me of any queries. Sorry for my poor English skill.
Related
I am using Google APIs Client Library for PHP. I can create and access the DeveloperMetadata of the spreadsheet. But cannot update the DeveloperMetadata. Please see the below code for a better understanding.
<?php
class GoogleSheetHelper {
function update_dev_meta() {
$dev_meta = new DeveloperMetadata();
$dev_meta->setVisibility( 'DOCUMENT' );
$dev_meta->setMetadataKey( 'new_meta_key' );
$dev_meta->setMetadataValue( 'new_meta_value_updated' );
$update_req = new UpdateDeveloperMetadataRequest();
$update_req->setDeveloperMetadata( $dev_meta );
$update_req->setFields( '*' );
$request = new Request();
$request->setUpdateDeveloperMetadata( $update_req );
$batch_update = new BatchUpdateSpreadsheetRequest();
$batch_update->setRequests( [ $request ] );
$client = $this->get_client();
$service = new Sheets( $client );
try {
return $service->spreadsheets->batchUpdate( $spreadsheet_id, $batch_update );
} catch ( Exception $ex ) {
var_dump( $ex->getErrors() );
return false;
}
}
private function get_client( $scopes = [] ) {
$credential = 'location/of/credential.json';
if ( empty( $credential ) ) {
return null;
}
$default_scopes = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/spreadsheets',
];
return new Client(
[
'credentials' => $credential,
'scopes' => $default_scopes,
]
);
}
}
Feel free to notify me of any queries. Sorry for my poor English skill.
Checking your code and the documentation it seems that you are missing to specify a Data Filter and a DeveloperMetadata Object including a field mask that declares fields to be updated. Please check the documentation on how to update the developer metadata to corroborate you are doing it properly.
Here is the example shown that updates the developer metadata, it's not that far from what you currently have:
{
"requests": [
{
"updateDeveloperMetadata": {
"dataFilters": [
{
"developerMetadataLookup": {
"metadataId": metadataId
}
}
],
"developerMetadata": {
"location": {
"sheetId": sheetId
},
"metadataKey": "SalesUpdated"
},
"fields": "location,metadataKey"
}
}
]
}
I'm using google sheet api with this logic for each request:
create a sheet,
add custom query there (custom formula),
fecth query results,
delete the created sheet.
The creation code is a follow with Laravel:
class GoogleSheetsApiClientManager {
private $client;//authorized API client.
private $service = null;
public function __construct($apiOptions) {
$this->client = new Google_Client();
$this->client->setApplicationName(array_get($apiOptions, 'application_name', 'Google Sheets API PHP'));
$this->client->setScopes(array_get($apiOptions, 'scope', Google_Service_Sheets::SPREADSHEETS));
$this->client->setAuthConfig(array_get($apiOptions, 'credential_path'));
$this->client->setAccessType('offline');
$this->service = new Google_Service_Sheets($this->client);
}
public function addNewSheet($spreadsheetId, $hidden = false, $sheetName = false){
try {
$properties = [
'hidden' => $hidden
];
if($sheetName){
$properties['title'] = $sheetName;
}
$body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => [
'addSheet' => [
'properties' => $properties
]
]
]);
$result = $this->service->spreadsheets->batchUpdate($spreadsheetId, $body);
return $result;
}
catch (\Exception $exception) {
Log::error('Add new sheet using Google Sheet API : '.$exception->getMessage());
return false;
}
}
}
And the call is like:
$apiOptions = [
'application_name' => 'My app name',
'credential_path' => __DIR__.'/service_account-secret.json'
];
$googleSheetManager = new GoogleSheetsApiClientManager($apiOptions);
$spreadsheetId = '--theSpreasheetId--';
$mySheet = $googleSheetManager->addNewSheet($spreadsheetId);
The issue : Each time I create new sheet, its name is based on last created one. eg: last sheet name is sheet5, even sheet5 has been deleted I got new sheet with name sheet6.
What I would like to have is the minimal possible name, meaning if sheet5 & sheet4 are not there (but sheet3 is present), I would like a new name sheet4, not an endless incremented name
I have 'sendsms' function which i used it in one of my controllers and worked fine. now what i need to know how i can make class reference of this code to use it in other controllers, instead of copy/paste whole code in all controllers.
In other Q/A they mentioned about only creating reference but i wanted to do it properly like using constructor or etc, not just doing things work, i want to do it like real-world project.
Here's the code in controller :
public function store(Request $request)
{
$this->validate($request,[
'title' => 'required|string|min:6',
'gametype' => 'required|string|min:3',
'description' => 'required|string|min:1|max:180',
'price' => 'required|numeric|min:4',
'buyyer_id' => 'required|numeric|min:1'
// 'seller_id' => 'required|numeric|min:1'
]);
// return RequestModel::create([
// 'title' => $request['title'],
// 'description' => $request['description'],
// 'gametype' => $request['gametype'],
// 'price' => $request['price'],
// 'buyyer_id' => $request['buyyer_id'],
// 'seller_id' => Auth::user()->id,
// ]);
//
$requestModel = new RequestModel;
// store
$requestModel->title = $request['title'];
$requestModel->description = $request['description'];
$requestModel->gametype = $request['gametype'];
$requestModel->price = $request['price'];
$requestModel->buyyer_id = $request['buyyer_id'];
$requestModel->seller_id = Auth::user()->id;
$requestModel->save();
return $this->sendSms($request['title'], $request['gametype']);
}
// I want to use this code in another class to use it in all controllers without copy/paste it.
function sendSms($reqid, $recgametype) {
//Send sms to getway
//implement later.
$otp_prefix = ':';
$response_type = 'json';
$textMSGATLAS = iconv("UTF-8", 'UTF-8//TRANSLIT',"req : ( " .$reqid. " ) for ( " .$recgametype. " ) submitted ");
ini_set("soap.wsdl_cache_enabled", "0");
try {
$client = new SoapClient("http://xxxx");
$user = "user";
$pass = "pass";
$fromNum = "+xxx";
$toNum = "+xxxx";
$messageContent = $textMSGATLAS;
$op = "send";
$client->SendSMS($fromNum,$toNum,$messageContent,$user,$pass,$op);
} catch (SoapFault $ex) {
echo $ex->faultstring;
}
}
I'm right now learning and I'm beginner at this so help to understand how to make it work properly. Thanks.
You can create a separate SMS class like :
<?php
namespace App;
class SMS {
private $reqid;
private $recgametype;
public function __construct($reqid, $recgametype)
{
$this->reqid = $reqid;
$this->recgametype = $recgametype;
}
public function send()
{
$otp_prefix = ':';
$response_type = 'json';
$textMSGATLAS = iconv("UTF-8", 'UTF-8//TRANSLIT',"req : ( " .$this->reqid. " ) for ( " .$this->recgametype. " ) submitted ");
ini_set("soap.wsdl_cache_enabled", "0");
try {
$client = new SoapClient("http://xxxx");
$user = "user";
$pass = "pass";
$fromNum = "+xxx";
$toNum = "+xxxx";
$messageContent = $textMSGATLAS;
$op = "send";
return $client->SendSMS($fromNum,$toNum,$messageContent,$user,$pass,$op);
} catch (SoapFault $ex) {
throw new \Exception('SMS sending failed')
}
}
}
And then inside controller or wherever you would need :
public function sendSms($reqid, $recgametype) {
$sms = new \App\SMS($reqid, $recgametype);
$sms->send();
}
You can also create custom exception like SMSSendingFailedException and throw it instead of standard \Exception inside send() function.
That will help you to send appropriate response in controller like :
public function sendSms($reqid, $recgametype) {
try{
$sms = new \App\SMS($reqid, $recgametype);
$sms->send();
return response()->json('message' => 'SMS sent successfully', 200);
}
catch(SMSSendingFailedException $e){
return response()->json('message' => 'SMS sending failed', 500);
}
}
Then to go one step further, you can use concept of laravel facade if you need it all over the project with a quick class alias.
I see the documentation https://www.twilio.com/docs/video/api/rooms-resource#rooms-list-resource
But i cannot find how to set the mediaRegion.
Can you tell me how please?
This is what i try but it not works :
use Symfony\Component\HttpFoundation\Request;
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VideoGrant;
use Twilio\Rest\Client;
class VideoconferencingController extends Controller
{
public function createAction(Request $request, $roomName)
{
$user = $this->getUser();
// An identifier for your app - can be anything you'd like
$identity = $user->getFullName();
// Create access token, which we will serialize and send to the client
$token = new AccessToken(
$twilioAccountSid,
$twilioApiKey,
$twilioApiSecret,
3600,
$identity
);
// Create Video grant
$videoGrant = new VideoGrant();
$videoGrant->setRoom($roomName);
// Add grant to token
$token->addGrant($videoGrant);
$twilio = new Client($twilioApiKey, $twilioApiSecret, $twilioAccountSid);
$room = $twilio
->video
->v1
// ->rooms($roomName)
->rooms('RM2900c0f08a237f6e978fc413cb997403')
->mediaRegion('ie1')
->update('completed')
;
error_log(print_r($room,1));
// render token to string
return [
'token' => $token->toJWT(),
'roomName' => $roomName,
];
}
Best regards,
Bruno
I found what i need to do.
Create the room with mediaRegion :
use Symfony\Component\HttpFoundation\Request;
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VideoGrant;
use Twilio\Rest\Client;
class VideoconferencingController extends Controller
{
public function createAction(Request $request, $twilioRoomSid, $staffId, $roomName)
{
$twilioRoomSid = ('undefined' == $twilioRoomSid) ? null : $twilioRoomSid;
$user = $this->getUser();
$twilioAccountSid = $this->getParameter('twilio_account_sid');
$twilioApiKey = $this->getParameter('twilio_api_key');
$twilioApiSecret = $this->getParameter('twilio_api_secret');
$now = new \DateTime();
// Get or create room
$twilio = new Client($twilioApiKey, $twilioApiSecret, $twilioAccountSid);
if ($twilioRoomSid) {
$room = $twilio
->video
->v1
->rooms($twilioRoomSid)
->fetch()
;
}
$createRoom = (!$twilioRoomSid || 'completed' == $room->status) ? true : false;
if ($createRoom) {
$room = $twilio
->video
->v1
->rooms
->create([
'mediaRegion' => 'ie1',
'uniqueName' => $roomName
]
)
;
$twilioRoomSid = $room->sid;
$staff = $this->findOr404('App:Staff', $staffId);
$staff->setTwilioRoomSid($twilioRoomSid);
$this->flush();
}
// Authorize room
$identity = $user->getFullName();
// Create access token, which we will serialize and send to the client
$token = new AccessToken(
$twilioAccountSid,
$twilioApiKey,
$twilioApiSecret,
3600,
$identity
);
// Create Video grant
$videoGrant = new VideoGrant();
$videoGrant->setRoom($twilioRoomSid);
// Add grant to token
$token->addGrant($videoGrant);
// render token to string
return [
'token' => $token->toJWT(),
'roomName' => $roomName,
];
}
}
I am trying to get a soap response in php. It keeps coming as an object onto my web browser but not as xml. WSDL shows as XML but not the response received. Below is my server side code. The soap server is Zend Soap
ini_set("soap.wsdl_cache_enabled", 0);
if (isset($_GET['wsdl'])){
$wsdl = 'http://localhost/webservice/soap';
$autoDiscover = new AutoDiscover();
$autoDiscover->setOperationBodyStyle(
array('use' => 'literal',
'namespace' => 'http://localhost/webservice/soap')
);
$autoDiscover->setBindingStyle(
array('style' => 'rpc',
'transport' => 'http://schemas.xmlsoap.org/soap/http')
);
$autoDiscover->setComplexTypeStrategy(new ArrayOfTypeComplex());
// $service is the class that does the handling of functions
$autoDiscover->setClass($service);
$autoDiscover->setUri($wsdl);
$response->getHeaders()->addHeaderLine('Content-Type', 'text/xml');
$response->setContent($autoDiscover->toXml());
} else {
$server = new Server('http://localhost/webservice/soap?wsdl'
);
// $service is the class that does the handling of functions
$server->setObject($service);
$response->setContent($server->handle());
}
return $response;
}
Service class
class service
{
/**
*
* #param string $Email
* #return int $Credit
*/
public function checkCredits($Email)
{
$validator = new email();
if (!$validator->isValid($Email))
{
return new \SoapFault('5', 'Please Provide an Email');
}
$rowset = $this->tableGateway->select(array('EMAIL'=>$Email))
$row = $rowset->current();
$credits = $row->CREDITS;
return $credits;
}
}
Request is :
try{
$sClient = new SoapClient('http://localhost/webservice/soap?wsdl');
$params = "email";
$response = $sClient->checkCredits($params);
var_dump($response);
} catch(SoapFault $e){
var_dump($e);
}
This is an example of how I handle my functions with SoapClient:
$client = new SoapClient('http://url/Service.svc?wsdl');
$var = array('arg' => 10,
'VA' => 48);
$varresponse = $client->Function($var);
print_r( $varresponse->FunctionResult);
Hope this will help you out.
Your soapserver should look a bit like this:
<?php
if(!extension_loaded("soap")){
dl("php_soap.dll");
}
ini_set("soap.wsdl_cache_enabled","0");
$server = new SoapServer("hello.wsdl");
function doHello($yourName){
return "Hello, ".$yourName;
}
$server->AddFunction("doHello");
$server->handle();
?>
How did you set up yours? Do you return anything?
Now, your client should look like this:
<?php
try{
$sClient = new SoapClient('http://localhost/test/wsdl/hello.xml');
$params = "Name";
$response = $sClient->doHello($params);
var_dump($response);
} catch(SoapFault $e){
var_dump($e);
}
?>