google classroom announcements create + php api (materials) - php

I am using google Classroom google API and Google APIs Client Library for PHP.
I can add announcement, but I cannot add materials.
I would like to add files to Google Drive, but I have errors even with "link"
My code so far:
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setApplicationName("test classroom");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/classroom.courses',
"https://www.googleapis.com/auth/classroom.rosters",
"https://www.googleapis.com/auth/classroom.announcements",
"https://www.googleapis.com/auth/classroom.coursework.students",
"https://www.googleapis.com/auth/classroom.coursework.me",
]);
// $service implements the client interface, has to be set before auth call
$service = new Google_Service_Classroom($client);
$text="some text";
$link="http://someurl";
$glink = new Google_Service_Classroom_Link($link);
$glink->setUrl($link);
$params = [
"text" => $text,
"materials" => [
"link" => $glink,
],
];
$params_obj = new Google_Service_Classroom_Announcement($params);
$service->courses_announcements->create($course_classid, $params_obj);
//tried also with:
$params = [
"text" => $text,
"materials" => [
"link" => ((new Google_Service_Classroom_Material())->setLink($glink)) ,
],
];
the error:
"message": "materials: Link materials must have a URL.",

So off the top, it seems odd here that you are creating params associative arrays at all. The PHP client has methods for adding all parameters, all the way down to the leaf level. So you shouldn't see any arrays in your code. Continue to use the methods instead and it will help clean things up. If you're still having problems, I may have some time to dig on this particular item.

Your limk must be array with three entry
$params = [
"text" => 'Please, do your homeworks until Monday',
"materials"=>['link'=>['url'=>'https://www.examaker.com',
'title'=>'HW',
'thumbnailUrl'=>'https://examaker.com/apps/imgs/logo_40.png']
]
];

Related

Setting spreadsheet title with Guzzle in PHP

I am connected to my Google sheets, when I run a post request for https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create and set no properties, it creates a new spreadsheet.
I would love to add a title so I did this
$options = [
'form_params' => [
'properties' => [
'title' => 'A new title'
]
]
];
$client->post('v4/spreadsheets', $options);
I am using Guzzle to make this call. As a response I'm getting HTTP status code 400 (Bad Request) when adding title to form params.
How can I solve this?
You really should be using Google's SDK for this since they already have support for one in PHP.
According to their docs, sheet properties are set in a Google_Service_Sheets_SpreadsheetProperties instance which takes the title property.
An example from their docs:
<?php
$spreadsheet = new Google_Service_Sheets_Spreadsheet([
'properties' => [
'title' => $title
]
]);
$spreadsheet = $service->spreadsheets->create($spreadsheet, [
'fields' => 'spreadsheetId'
]);
printf("Spreadsheet ID: %s\n", $spreadsheet->spreadsheetId);

Using Guzzle to send multiple posts to one URL - different parameters

I am trying to send multiple posts to Twilio using Guzzle. My apologizes, I'm very new to guzzle. I have seen some examples where I can set an array of URIs and run the posts in parallel. But I haven't been able to figure out how to use the same URI with different parameters for each request.
The ONLY difference between each call would the the "TO" field. The body, messageSID, and auth would stay the same for each parallel call. I wanted to get this test working then eventually just be able to build the array of anywhere from 1 to 100 TO phone numbers.
Here is my code to send one request:
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$client = new Client(["base_uri" => "https://api.twilio.com/2010-04-01/Accounts/"]);
$options = array(
'form_params' => [
"Body" => "hello world",
"To" => "+12015551234",
"MessagingServiceSid" => "MGxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
],
'auth' => [
"accountsidxxxxxxxxxxx",
"tokenxxxxxxxxxxxxxxx"
]
);
$response = $client->post("ACxxxxxxxxxxxxxxxxxxxxx/Messages.json", $options);
echo $response->getBody();

Sorting sheets in Google Drive Spreadsheet using PHP API

I have a SpreadSheet on Google Drive and I'm updating the content using PHP API (https://developers.google.com/sheets/api/guides/values).
I store here reports from my projects. Each project has its sheet.
When the project has no sheet (new project), I create a new sheet for this project called by the name of a new project.
The sheets is created as a last one.
This is my current code to create a new Sheet:
$body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(
['requests' => ['addSheet' => ['properties' => ['title' => $project_name]]]]
);
$result = $service->spreadsheets->batchUpdate($spreadsheetId, $body);
I would like to have sheets sorted alphabetically. Excluding the first sheet, where are some global statistics.
Is there any way to change the order of the sheets using Google SpreadSheet API?
This would be the workflow:
Each time after creating a new sheet use the method spreadsheets.get and retrieve the sheets of the spreadsheet
Retrieve the ids and titles of the sheets, pass the ids into an array and sort the position of the ids as a function of title, as performed here
Perform an UpdateSheetPropertiesRequest, specifying the SheetProperty "index" - which will move the sheet to a position corresponding to the position of the sheetId in the alpahabetically sorted array.
I just found the solution ;-)
$requests = [
new Google_Service_Sheets_Request([
'updateSheetProperties' => [
'fields' => 'index',
'properties' => [
'sheetId' => $sheet_id,
'index' => $i,
],
],
])
];
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => $requests
]);
$result = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);

Elasticsearch Unknown key for a VALUE_STRING in [scroll]

I am trying to use the PHP API, and same example as given in the code
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_search_operations.html#_scrolling
$client = ClientBuilder::create()->build();
$params = [
"scroll" => "30s", // how long between scroll requests. should be small!
"size" => 50, // how many results *per shard* you want back
"index" => "my_index",
"body" => [
"query" => [
"match_all" => new \stdClass()
]
]
];
// Execute the search
// The response will contain the first batch of documents
// and a scroll_id
$response = $client->search($params);
But getting error like this Unknown key for a VALUE_STRING in [scroll].
Currently using Elasticsearch version 6.2.2
Any ideas?
The problem is that you put scroll parameter in json body, but it should be instead in the URL. e.g
index-name/_search?scroll=30s
Don't forget to remove it from $params as well
You may have accidentaly put scroll attribute inside body.

Amazon AWS SDK PHP 2 - Filter tags by instance?

I have been able to retrieve a full list of tags from all my EC2 instances using the PHP SDK but I'm struggling to filter the results down to a particular instance...
// Collect instance information
$sInstanceId = file_get_contents('http://169.254.169.254/latest/meta-data/instance-id'); // 'i-52da5b1f'
$sAvailabilityZone = file_get_contents('http://169.254.169.254/latest/meta-data/placement/availability-zone'); // 'eu-west-1b'
$sRegion = preg_replace('/^(.*)([0-9]{1})([a-zA-Z]{1})/', '$1$2', $sAvailabilityZone);
use Aws\Common\Aws;
use Aws\Ec2\Command\DescribeTags;
use Aws\Common\Enum\Region;
// Set up the global AWS factory
$oAWS = Aws::factory(array(
'key' => CONST_AWS_ACCESS_KEY,
'secret' => CONST_AWS_SECRET_KEY,
'region' => $sRegion
));
// Query EC2 for tags
$oEC2Client = $oAWS->get('ec2');
$oModel = $oEC2Client->describeTags()->toArray();
I've tried changing the call to describeTags to...
$oModel = $oEC2Client->describeTags(array(
"Filters" => array(
array("Name" => "resource-id", "Value" => $sInstanceId)
)
))->toArray();
But that seems to make no difference.
Could someone shed some light on this for me please?
The API docs for Ec2Client.describeTags show that Value should actually be Values and should be an array. Try the following:
$oModel = $oEC2Client->describeTags(array(
"Filters" => array(
array("Name" => "resource-id", "Values" => array($sInstanceId))
)
))->toArray();

Categories