I'm trying to make a custom management system for Telegram bots so that I can be able to send and read updates easily.. But my main problem is that I don't know how to retrieve data from an array. let me explain this in depth so you can understand what I'm talking about.
Basically when I try the getme command, I get this:
{"ok":true,"result":{"id":275700102,"first_name":"photoshop_post_production","username":"post_pro_bot"}}
So I converted this into an array using Php and it goes like this:
$_SESSION['website'] = "https://api.telegram.org/bot";
$_SESSION['url'] = "https://api.telegram.org/bot".$_SESSION['token'];
$_SESSION['me'] = file_get_contents($_SESSION['url']."/getme");
$_SESSION['meArray'] = json_decode($_SESSION['me'], TRUE);
So as you can see I have an array called meArray and if I print_r it ,this will be shown:
Array ( [ok] => 1 [result] => Array ( [id] => 275700102 [first_name] => photoshop_post_production [username] => post_pro_bot ) )
So basically I don't know how to take out the username or other information that I want from this array. I have tried several things but no success till now.
So, should be:
$_SESSION['meArray']['result']['username']
Try this.
Related
I'm trying to use this php library along with Sheetsu to pull single bits of data from a Google spreadsheet for output on a web page. My php skills are minimal and spotty, I'm afraid, and so I'm missing a final crucial component.
When I set up a test file and run my query, the code dumps everything into a $collection object. If I output print_r($collection); I get this:
Sheetsu\Collection Object (
[models:Sheetsu\Collection:private] => Array (
[0] => Sheetsu\Model Object (
[id] => 2.05.1
[title] => The Mead of Poetry
[answer] => Kvasir was created from the spit of the Aesir and Vanir. He was very wise.
)
)
)
My data's there, everything's working as expected, but I've never seen a structure like that before.
How can I just echo, say, just the [answer] value? I'm not sure what syntax to use to drill into that.
Thanks!
try
$collection->get(0)
or
$collection->getFirst()
or
$collection->getAll()[0]
etc.
models is a private property, you can't access it directly
so to access answer it would be something like
$collection->get(0)->answer
Try echo $collection[0]->answer;
I have a variable called $graph that carries OpenGraph data scraped from a URL. When I print_r the variable, this is what it looks like:
Array (
[OpenGraph_values] => Array (
[url] => http://www.theguardian.com/sport/blog/2017/jun/05/lebron-james-cleveland-cavaliers-nba-finals
[description] => The Cleveland star’s greatness created an arms race in basketball, and it has reached its apotheosis in the Golden State Warriors
[image] => https://i.guim.co.uk/img/media/3746052fc5b7f0a1ffb9ee2d5cd79585c31149a8/562_880_4049_2429/master/4049.jpg?w=1200&h=630&q=55&auto=format&usm=12&fit=crop&crop=faces%2Centropy&bm=normal&ba=bottom%2Cleft&blend64=aHR0cHM6Ly91cGxvYWRzLmd1aW0uY28udWsvMjAxNi8wNS8yNS9vdmVybGF5LWxvZ28tMTIwMC05MF9vcHQucG5n&s=71512608e33d4fda4fcf6ca52ccbd2bf
[type] => article
[title] => LeBron James created the modern NBA superteam. Now it will destroy him
[site_name] => the Guardian
)
[OpenGraph_position] => 0
)
I cannot for the life of me figure out how to access the data in there. I've tried $graph['image'] and $graph['OpenGraph_values']['image'], but no luck. Both come out null. There has to be something really simple that I'm missing, but I'm pretty new to PHP so I don't even know how to search for my problem.
EDIT: Some additional information:
I'm creating the variable by doing this:
$graph = (array)OpenGraph::fetch( $permalink );
where $permalink is a string with a URL.
Which uses this tool to fetch the OpenGraph data: https://github.com/scottmac/opengraph
Let me know if anything else in particular might be useful. This is running in a WordPress site, if that's relevant at all.
Summary:
Google_Service_Calendar seems to be "force-paginating" results of $service->events->listEvents();
Background:
google calendar API v3,
using php client library
We are developing a mechanism to sync our internal calendar with a user's google calendar.
Please note I will refer below to $x, which represents google's default limit on the number of events, similar to $options['maxResults']; The default value is 250, but it should not matter: we have tested the below with and without explicitly defined request parameters such as 'maxResults', 'timeMin', and 'timeMax' - the problem occurs in all cases.
Another relevant test we did: export this calendar to foobar.ics, created a new gmail user form scratch, import foobar.ics to newuser#gmail.com. DOES NOT REPLICATE THIS ISSUE. We have reviewed/reset various options in the subject calendar (sharing, etc) but cannot find any setting that has any effect.
The Problem:
Normally, when we call this:
$calendar='primary';
$optParams=array();
$events = $this->service->events->listEvents($calendar, $optParams);
$events comes back as a Google_Service_Calendar_Events object, containing $n "items". IF there are more than $x items, the results could be paginated, but the vanilla response (for a 'normal', result set with ( count($items) < $x ) ) is a single object, and $events->nextPageToken should be empty.
One account we are working with (of course, the boss's personal account) does not behave this way. The result of:
$events = $this->service->events->listEvents('primary', []);
is a Google_Service_Calendar_Events object like this:
Google_Service_Calendar_Events Object
(
[accessRole] => owner
[defaultRemindersType:protected] => Google_Service_Calendar_EventReminder
[defaultRemindersDataType:protected] => array
[description] =>
[etag] => "-kakMffdIzB99fTAlD9HooLp8eo/WiDS9OZS7i25CVZYoK2ZLLwG7bM"
[itemsType:protected] => Google_Service_Calendar_Event
[itemsDataType:protected] => array
[kind] => calendar#events
[nextPageToken] => CigKGmw5dGh1Mms4aWliMDNhanRvcWFzY3Y1ZGkwGAEggICA6-L3lrgUGg0IABIAGLig_Zfi278C
[nextSyncToken] =>
[summary] => example#mydomain.com
[timeZone] => America/New_York
[updated] => 2014-07-23T15:38:50.195Z
[collection_key:protected] => items
[modelData:protected] => Array
(
[defaultReminders] => Array
(
[0] => Array
(
[method] => popup
[minutes] => 30
)
)
[items] => Array
(
)
)
[processed:protected] => Array
(
)
)
Notice that $event['items'] is empty, and nextPageToken is not null. If we then do a paginated request like this:
while (true) {
$pageToken = $events->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$events = $this->service->events->listEvents($calendar, $optParams);
if(count($events) > 0){
h2("Google Service returned total of ".count($events)." events.");
}
} else {
break;
}
}
The next result set gives us the events. In other words, the google service seems to be paginating the initial results, despite the fact that we are certain the result is less than $x.
To be clear, if we have 5 events on our calendar, we expect 1 result with 5 items. Instead, we get 1 result with 0 items, but the first result of the 'nextPageToken' logic gives us the desired 5 items.
Solution Ideas?:
A. handle paginated results, and/or "Incremental Syncronization'. These are on our list of features to implement, but we consider these to be more 'optimization' than 'necessity'. In other words, I understand that handling/sending nextSyncToken and nextPageToken are OPTIONAL- thus the issue we are having should not depend on our client code doing this.
B. use a different, non-primary calendar for this user. we think this particular primary calendar may corrupt or somehow cached on google's side: to be fair, we did at one point accidentally insert a bunch of junk events on this calendar to the point that google put us in read-only mode as described here: https://support.google.com/a/answer/2905486?hl=en but we understand that was a temporary result of clunky testing.... In other words, we know we HAD screwed this calendar up badly, but this morning we deleted ALL events, added a single test event, and got the same result as above FOR THIS CALENDAR. Cannot replicate for any other user.... including a brand new gmail user.
C. delete the 'primary' calendar, create a new one. Unfortunately, we understand it is not possible to delete the primary CALENDAR, only to delete the CALENDAR EVENTS.
D. make the boss open a brand new google account
Any other suggestions? We are proceeding with A, but even that is a band-aid to the problem, and does not answer WHY is this happening? How can we avoid it in the future? (Please don't say "D")
Thanks in advance for any advice or input!
There is a maximum page size, if you don't specify one yourself there is an implicit one (https://developers.google.com/google-apps/calendar/v3/pagination). Given this it's necessary to implement pagination for your app to work correctly.
As you noticed, a page does not always contain the maximum number of results so pagination is important even if the number of events does not exceed the page size. Just keep following the page tokens and it will eventually give you all the results (there will be a page with no nextPageToken)
TL;DR A :)
I need to be able to store the $_POST data from a form (I'm doing that with the SESSION variable) and I need to add data to the $_POST array without overwriting the previous data.
Here's my original thread about the SESSION variables: Store Array Variables Through Page Refresh with PHP
Here's an example of what I need to do:
There's a form on a page that you can put the width and height of a wall to calculate out how much paint/labor would be needed to paint the walls. Once the first wall dimensions are entered, the user can click the submit button labeled "Add Wall" to add another wall to their order.
After the first wall input, the POST data would return something like this:
Array ( [wallW] => Array ( [0] => 9 ) [wallL] => Array ( [0] => 10 ) )
After the second wall is entered it would look something like this:
Array ( [wallW] => Array ( [0] => 9 [1] => 20 ) [wallL] => Array ( [0] => 10 [1] => 15 ) )
This would continue on until the user has entered all of their walls.
I've tried doing array_merge, array_append, etc, but none of those seem to be working; it keeps overwriting what's tin the POST data, even if I bring the POST data into another array.
I know the easy thing would be to throw some jQuery in there, but this is for a PHP class (and we're not allowed to use SQL yet) so I'd like to try to figure this out with just PHP.
Any help would be awesome.
Thanks,
Chelsea
I don't know why this has been downvoted, i find it rude as you are obviously new to php. But you just need to re-post your post vars in subsequent requests. So you would need to serialize you existing post objects as perhaps hidden fields. What you are missing is that php / http is stateless. So nothing is preserved between requests, another easy solution may be to move these POST variables to SESSION variables.
I see you've added the POST data to the session, but there is no reason to add it back to post, just keep appending to session and then use it from there. adding things to _POST wont force them to be posted next time if thats you are trying todo, see the part about serializing them to hidden fields.
Good luck, welcome to programming for the web.
The way you are constructing array seems little odd to me. Instead I would do something like.
if(!empty($_POST))
{
$wall = array('w' => $_POST['w'], 'h' => $_POST['h']);
$_SESSION['walls'][] = $wall;
}
Please give this a try.
I'm getting a redirect loop when using Login Toboggan. It doesn't happen all of the time and I think I've narrowed it down to something with the session, specifically the active-tabs[last-active-href] value.
Since it's intermittent, I was able to print out a session of a working copy and a non-working copy. Here are both:
WORKS -- Array ( [active-tabs] => Array ( [last-active-href] => index ) ) toboggan/denied
DOESN'T WORK -- Array ( [active-tabs] => Array ( [last-active-href] => user/register [user] => user/register ) [wantsEvents] => [wantsResources] => [wantsSupport] => ) toboggan/denied
I've also noticed that if I comment out the following line the redirection loop stops (although no page loads):
$return = menu_execute_active_handler('user/register');
Any ideas? I'm at my wits end.
Resolved here: http://drupal.org/node/802330#comment-2987308
Was an issue combining Login Toboggan and Smart Tabs.
#sirhc thanks for pointing me to the Drupal Issue page.