AWS SDK PHP iterator - php

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>";
}

Related

firestore display values of data in php

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.

Handling empty lists in PHP SOAP

Initially if there was one item in the list it would return an object rather than an array of one object. I fixed that using:
https://eirikhoem.wordpress.com/2008/03/13/array-problems-with-soap-and-php-updated/
$x = new SoapClient($wsdl, array('features' => SOAP_SINGLE_ELEMENT_ARRAYS));
But I'm having problems when there is no items in the list.
The best I've come up with so far is:
$occulist = $result->GetWebOccurrencesResult->OccuList;
if (!empty((array)($occulist))) {
foreach($occulist->TOccu as $occurrence) {
echo $occurrence->Prog_Name . ' running from ' . $occurrence->StartDate . ' to ' . $occurrence->EndDate . '<br/>';
}
}
Originally it was
foreach($result->GetWebOccurrencesResult->OccuList->TOccu as $occurrence) {
I don't understand exactly what you're trying to achieve here, so if you can clarify in the comments, that'd be great.
What I'm taking from your question is that you want to handle when the array is empty, but want a clean method to do so? The loop you have there would do what you require, but here is another alternative:
do {
foreach($occulist->TOccu as $occurrence) {
echo $occurrence->Prog_Name . ' running from ' . $occurrence->StartDate . ' to ' . $occurrence->EndDate . '<br/>';
}
} while(empty((array)($occulist)) !== FALSE);
So you're looping through the foreach while the $occulist array isn't empty.
You could even do:
while((array)$occulist !== FALSE) {
foreach(....) {
...
}
}
On my server with Windows and PHP 5.4.23 this:
if (!empty((array)($occulist))) {
gives the following error:
Parse error: syntax error, unexpected '(array)' (array) (T_ARRAY_CAST)
I fixed it using:
if (get_object_vars($occulist)) {
I think that is a more elegant alternative to the original...

PHP: Call method with array?

Not quite sure how to word the title better.
What i need to do, is call a method with call_user_func. The method i'm calling accept several different variables (var1, var2, var3 etc), not an array. The problem is, when calling the function i need to pass the array i have as seperate variables to the method. Let me show you.
Here's my method:
class Test {
public function testing_cronjob($p1, $p2, $p3, $p4, $p5) {
echo 'p1 = ' . $p1 . '<br />';
echo 'p1 = ' . $p2 . '<br />';
echo 'p1 = ' . $p3 . '<br />';
echo 'p1 = ' . $p4 . '<br />';
echo 'p1 = ' . $p5 . '<br />';
}
}
And here's how i currently (try) to call it:
$obj = new Test();
call_user_func(array($obj, 'testing_cronjob'), $params);
The method runs, but it only gets one variable, which is not what i want.
$params look like this:
$params = array(
0 => 'first one',
1 => 'second one',
2 => 'third param!',
3 => 'oh and im fourth!!',
4 => 'im last, fml! :('
)
The way i got myself into this situation, is because i'm creating a cronjob model. It accepts params along with class and method name. It's all stored in a database, and when it runs, i need to be able to send the $params to the method as intended.
I can't rewrite how methods accept variables, because that would be way too much work. I'm going to be calling several (10+) methods from the cronjob, all of which are pretty well used in the application. I'm also not sure how i would re-write it, but if you have any suggestions about that, let me know.
Also, i'm using codeigniter if that matters. If i forgot to mention anything or you want more code, just drop a comment.
Is this possible? Are there any workarounds? Any suggestions are welcome!
You have to use call_user_func_array instead:
$obj = new Test();
call_user_func_array(array($obj, 'testing_cronjob'), $params);

Is there an easier/shorter way to get geographic coordinates from GoogleMaps?

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.)

New line does not display when getting jira issue using php soap

I am trying to get issue details from JIRA 3.13 using PHP SOAP. I was able to login and get the issues; however, on one of my field, I could not get the new line formatting. So, all I got is the text for that particular field without new line character (everything just append into a single line of text). As of now, I am guessing php also did some re-formatting of the string from SOAP. The reason I am saying this is because I did some testing with SOAP UI and was able to get the text out with the formatting. Can anyone help me out with a way to displaying the text with the formatting? Thanks in advance.
This is my php code:
try {
$soap = new SoapClient("<<JIRA URL>>");
$auth = $soap->login($formUsername, $formPassword);
if ($auth)
{
$result0 = $soap->getIssue($auth,'<<JIRA ISSUE ID>>');
$result = (array) $result0;
foreach ($result as $key => $a)
{
$z = $z . '<br/>' . $key . ' = ' . $a;
}
echo $z;
}
}
catch(Exception $e){
$string = urlencode($e->getMessage());
header("Location: login.php?message=".$string);
die();
}
I just realize that I do not need to convert it into array.
Simply do the following:
foreach ($result0 as $key => $a)
{
$z = $z . '<br/>' . $key . ' = ' . $a;
}
This, however, still does not solve my problem with the new line.
Isn't it just because you don't change the linefeeds into <br/> before outputting it?
Should be easy to find out if that's the case just by looking at the source in the browser.
You need nl2br() to convert newline characters (\n et. al) into HTML <br> tags:
foreach ($result0 as $key => $a)
{
$z = $z . '<br/>' . $key . ' = ' . nl2br($a);
}
Could be that the text is stored with Unix line endings and you're displaying on a Windows machine? Which field is having the problem?

Categories