how to display each record's values?
$collectionReference = $this->fsdb->collection('orders');
$documentReference = $collectionReference->document('MSKpcuedwxNmdLn2Ydsp');
$snapshot = $documentReference->snapshot();
echo "Hello " . $snapshot['userId'];
I am receiving this error:
ErrorException
Object of class Google\Cloud\Firestore\DocumentReference could not be converted to string
print_r() function works correctly but how to access each individual record?
I have been searching for hours to get this solved but I found no reference on web
Please help me in here...
A snapshot contains both data and metadata about the document. To get the data you need to call its data() method:
$collectionReference = $this->fsdb->collection('orders');
$documentReference = $collectionReference->document('MSKpcuedwxNmdLn2Ydsp');
$snapshot = $documentReference->snapshot();
if ($snapshot->exists()) {
printf('Document data:' . PHP_EOL);
echo "Hello " . $snapshot->data()['userId'];
} else {
printf('Document %s does not exist!' . PHP_EOL, $snapshot->id());
}
Also see the Firebase documentation on getting a document from Firestore.
Related
I've written a small function (in PHP) to return all AWS instances in my account.
I've tried using the basic describe instances method but it times out (see below).
So the new function uses the getIterator, however, the page still times out. If it set the max results to 10 it works, but I think that defeats the object. I want a full list of all of my instances.
Any thoughts on what I might be doing wrong that would cause my request to time out? The time out message I get is:
Unable to load the webpage because the server sent no data. Error code: ERR_EMPTY_RESPONSE"
public function allInstances(){
$response = $this->ec2Client->getIterator('describeInstances',array(
'Filters' => array(
array(
'Name' => 'instance-state-name',
'Values' => array('running')
)
))
);
return $response;
}
Update to original post:
This is my code for iterating through the results return by the describeInstances itertator. I know I have around 400 instances running, however the result never returns anything more than about 280.
foreach($iterator as $object){
$value = $object['Reservation'];
//echo "<pre>" . print_r($object,true) . "</pre>";
$number = $count++;
echo $count . ">" . "<b>InstanceID: </b>" . $object['InstanceId'] . " <b>AMI: </b>" . $object['ImageId'] . "</br>";
}
I'm trying to figure out how to print/echo a formatted array to the modx error log. but print_r & pre tags do not work, if I use something like:
$log = "<pre>";
$log .= print_r($formdata);
$log .= "</pre>";
$this->modx->log(modX::LOG_LEVEL_ERROR, 'Form Data = ' . $log);
the result in the log is:
[2014-12-20 22:35:18] (ERROR # /index.php) Form Data = <pre>1</pre>
I have seen formatted arrays in the modx logs before, does anyone know how to do it?
in print_r() function add 2nd argument TRUE for return output value, see below sample code
$log = "<pre>";
$log .= print_r($formdata, true);
$log .= "</pre>";
$this->modx->log(modX::LOG_LEVEL_ERROR, 'Form Data = ' . $log);
You don't need the "<pre>" - "</pre>" tags, just
$modx->log(xPDO::LOG_LEVEL_ERROR, "Form Data = " . print_r($formdata,true),'','mySnippet');
will do the job, as of Revo version 2.3.3 at any rate.
I still have trouble with the new google_api_client php library. I'm trying to retrieve the user's contacts.
I'm very close to the right solution ... I mean, I just got all the results but a can't parse it.
Probably it's because I'm not strong with XML parser. After tests and tests ... I get this solution (based on the example file by Google):
...
$req = new apiHttpRequest("https://www.google.com/m8/feeds/contacts/default/full");
$val = $client->getIo()->authenticatedRequest($req);
$response = simplexml_load_string($val->getResponseBody());
foreach($response->entry as $entry)
{
$child = $entry->children("http://schemas.google.com/g/2005");
$mail_info = $child->attributes();
}
...
In the $response I can get the title field where my contact's full name is stored, and in the $mail_info a got an object where i see the address field when I get the email address.
It's SAD and UGLY solution ... what if I want the company name, address ... phone numbers ... photos. Where are all these informations.
How can I use the Google response in a great and clean solution?
Anyone can give me some help.
Bye
What helped me was requesting JSON instead of XML. Try adding ?alt=json to the end of the URL in the request you make to google.
$req = new apiHttpRequest("https://www.google.com/m8/feeds/contacts/default/full?alt=json");
$val = $client->getIo()->authenticatedRequest($req);
$string = $val->getResponseBody();
$phparray = json_decode($string);
Certainly not child's play to get what you want but working with php arrays is probably easier.
For completeness this is the google contacts php example that we both probably found that helped us:
https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/contacts/simple.php
EDIT:
Here is another link that might help. In the comments it describes a cleaner of accessing contact's data using JSON.
http://25labs.com/import-gmail-or-google-contacts-using-google-contacts-data-api-3-0-and-oauth-2-0-in-php/
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
$xmlresponse = curl_file_get_contents($url);
$temp = json_decode($xmlresponse,true);
foreach($temp['feed']['entry'] as $cnt) {
echo $cnt['title']['$t'] . " --- " . $cnt['gd$email']['0']['address'] . "</br>";
}
and
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
$xmlresponse = curl_file_get_contents($url);
$temp = json_decode($xmlresponse,true);
foreach($temp['feed']['entry'] as $cnt) {
echo $cnt['title']['$t'] . " --- " . $cnt['gd$email']['0']['address'];
if(isset($cnt['gd$phoneNumber'])) echo " --- " . $cnt['gd$phoneNumber'][0]['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$street'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$street']['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$neighborhood'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$neighborhood']['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$pobox'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$pobox']['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$postcode'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$postcode']['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$city'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$city']['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$region'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$region']['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$country'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$country']['$t'];
echo "</br>";
}
I'm currently using the following method to get coordinates from GoogleMaps.
Can I possibly write this shorter/more efficient?
EDIT 21.06.2013
As of now the old Google Geocoding API is off. This is my modified code that works with the most recent version. I've updated this post, if someone stumbles over it and finds it useful.
public function getGeographicCoordinatesFromGoogle()
{
// create address string
$value = $this->address_line_1 . ' ' .
$this->postal_code . ' ' .
$this->city . ' ' .
$this->country;
$value = preg_replace('!\s+!', '+', $value);
// create request
$request = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' .
$value . '&sensor=false';
// get value from xml request
$xml = file_get_contents($request);
$doc = new \DOMDocument('1.0', 'UTF-8');
#$doc->loadXML($xml);
// fetch result
$result = $doc->getElementsByTagName('lat')->item(0)->nodeValue . ',' .
$doc->getElementsByTagName('lng')->item(0)->nodeValue;
// check result
if (!preg_match('/(-?\d+\.\d+),(-?\d+\.\d+)/', $result) ) {
$result = null;
}
// assign value
$this->setGeographicCoordinates($result);
}
You can use json instead of xml. json is newer, lightweight and object orientated.
I'll recommend you to use http_build_query() instead of trying to build the SearchQuery with Regex. There are other chars which need to be escaped.
This is a quite long method. Maybe it would make sense to have one Class which only handles the communication with GoogleMaps (Class GeoBackend with methods getGeoDataForAddress(), which returns a GeoData-Object, which could be asked for Cordinates etc.)
code.google.com's API for PHP
I have used this function : function printVideoEntry($videoEntry) and got
Video ID: XXXXXXXXXXXXXXXXXXXXXXXXXX Watch page:
http://www.youtube.com/watch?v=YYYYYYYYYYYYYY&feature=youtube_gdata_player
Flash Player Url:
http://www.youtube.com/v/YYYYYYYYYYYYY?version=3&f=playlists&app=youtube_gdata
If I pass given video id it gives me error:
Uncaught exception 'Zend_Gdata_App_HttpException' with message
'Expected response code 200, got 400 Invalid id'
And if I pass YYYYYYYYYYYYYYYYY from Watch page and Flash Player Url [both are same] I'm getting it what I need.
Help is much appreciated, Thanks in advance.
Using this function for getting video entries
function printVideoEntry($videoEntry) {
echo "<div onclick=\"ytvbp.presentVideo('".$videoEntry->getVideoId()."')\" >";
echo 'Video: '.$videoEntry->getVideoTitle() . "<br>";
echo "</div>";
echo 'Video ID: ' . $videoEntry->getVideoId() . "<br>";
echo 'Watch page: ' . $videoEntry->getVideoWatchPageUrl() . "<br>";
echo 'Flash Player Url: ' . $videoEntry->getFlashPlayerUrl() . "<br>";
}
I'm calling print video function from
function getAndPrintPlaylistVideoFeed($playlistListEntry) {
$yt = new Zend_Gdata_YouTube();
$playlistVideoFeed = $yt->getPlaylistVideoFeed($playlistListEntry->getPlaylistVideoFeedUrl());
foreach ($playlistVideoFeed as $playlistVideoEntry) {
$getandprintplaylistvideofeed_array[] = printVideoEntry($playlistVideoEntry);
}
$videoEntry should be a VideoEntry-object, which you can get based on an ID:
$videoEntry = $yt->getVideoEntry('the0KZLEacs');
Then you could call the function and set that as a parameter:
printVideoEntry($videoEntry);
Or you could edit the first lines of printVideoEntry:
function printVideoEntry($videoId) {
$videoEntry = $yt->getVideoEntry('the0KZLEacs');
//...
}
Check out the docs to see what information you can squeeze out of a VideoEntry =)