How do I deal with these PHP objects? - php

I am using the Google map API V3 making a request with lat/lng, with the expectation of getting the full address data returned. However, my PHP program would fail to get, for example, the Zip Code returned sometimes. I did a var_dump of what's being returned. I see an extra object element, which may be the cause I'm not sure. It's entirely possible I don't know everything about how Objects in PHP work too. Here is the var_dump output for the area of interest. The first example does indeed return the Zip Code to my PHP program making the request while the second one fails to do so and gives an error of "Trying to get property of non-object...". Thanks in advance for taking a look on this and your useful comments.
This is the var_dump of $xml that is returned from Google Map API V3, which DOES NOT WORK and fails to return the zip code properly:
object(SimpleXMLElement)#1 (1) {
["Response"]=>
object(SimpleXMLElement)#2 (3) {
["name"]=>
string(24) "40.74005999,-73.99718229"
["Status"]=>
object(SimpleXMLElement)#3 (2) {
["code"]=>
string(3) "200"
["request"]=>
string(7) "geocode"
}
["Placemark"]=>
object(SimpleXMLElement)#4 (5) {
["#attributes"]=>
array(1) {
["id"]=>
string(2) "p1"
}
["address"]=>
string(38) "136 W 17th St, New York, NY 10011, USA"
["AddressDetails"]=>
object(SimpleXMLElement)#5 (2) {
["#attributes"]=>
array(1) {
["Accuracy"]=>
string(1) "8"
}
["Country"]=>
object(SimpleXMLElement)#8 (3) {
["CountryNameCode"]=>
string(2) "US"
["CountryName"]=>
string(3) "USA"
["AdministrativeArea"]=>
object(SimpleXMLElement)#9 (2) {
["AdministrativeAreaName"]=>
string(2) "NY"
["SubAdministrativeArea"]=>
object(SimpleXMLElement)#10 (2) {
["SubAdministrativeAreaName"]=>
string(8) "New York"
["Locality"]=>
object(SimpleXMLElement)#11 (2) {
["LocalityName"]=>
string(8) "New York"
["DependentLocality"]=>
object(SimpleXMLElement)#12 (3) {
["DependentLocalityName"]=>
string(9) "Manhattan"
["Thoroughfare"]=>
object(SimpleXMLElement)#13 (1) {
["ThoroughfareName"]=>
string(13) "136 W 17th St"
}
["PostalCode"]=>
object(SimpleXMLElement)#14 (1) {
["PostalCodeNumber"]=>
string(5) "10011"
}
}
}
}
}
}
}
["ExtendedData"]=>
object(SimpleXMLElement)#6 (1) {
["LatLonBox"]=>
object(SimpleXMLElement)#8 (1) {
["#attributes"]=>
array(4) {
["north"]=>
string(10) "40.7414089"
["south"]=>
string(10) "40.7387109"
["east"]=>
string(11) "-73.9958332"
["west"]=>
string(11) "-73.9985312"
}
}
}
["Point"]=>
object(SimpleXMLElement)#7 (1) {
["coordinates"]=>
string(24) "-73.9971822,40.7400599,0"
}
}
}
}
The above has these errors:
PHP Notice: Trying to get property of non-object in try.php on line 38
PHP Notice: Trying to get property of non-object in try.php on line 41
Here is the PHP code for those lines:
$status = $xml->Response->Status->code;
// Country
$country = $xml->Response->Placemark->AddressDetails->Country;
$country_code = $country->CountryNameCode;
$country_name = $country->CountryName;
// Address
$address_line = $xml->Response->Placemark->address;
// Street address
$Locality = $country->AdministrativeArea->Locality;
// var_dump($Locality);
$street = $country->AdministrativeArea->Locality->Thoroughfare->ThoroughfareName;
$city = $country->AdministrativeArea->Locality->LocalityName;
$state = $country->AdministrativeArea->AdministrativeAreaName;
$zip_code = $country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber;
Line 41 is the last line of code with the $zip_code, and line 38 is the line of code which begins with $street.
You will notice the above contains a ["SubAdministrativeArea"]=> which the working example below does not. Does this matter?
object(SimpleXMLElement)#1 (1) {
["Response"]=>
object(SimpleXMLElement)#2 (3) {
["name"]=>
string(24) "40.74445606,-73.97495072"
["Status"]=>
object(SimpleXMLElement)#3 (2) {
["code"]=>
string(3) "200"
["request"]=>
string(7) "geocode"
}
["Placemark"]=>
object(SimpleXMLElement)#4 (5) {
["#attributes"]=>
array(1) {
["id"]=>
string(2) "p1"
}
["address"]=>
string(38) "317 E 34th St, New York, NY 10016, USA"
["AddressDetails"]=>
object(SimpleXMLElement)#5 (2) {
["#attributes"]=>
array(1) {
["Accuracy"]=>
string(1) "8"
}
["Country"]=>
object(SimpleXMLElement)#8 (3) {
["CountryNameCode"]=>
string(2) "US"
["CountryName"]=>
string(3) "USA"
["AdministrativeArea"]=>
object(SimpleXMLElement)#9 (2) {
["AdministrativeAreaName"]=>
string(2) "NY"
["Locality"]=>
object(SimpleXMLElement)#10 (3) {
["LocalityName"]=>
string(8) "New York"
["Thoroughfare"]=>
object(SimpleXMLElement)#11 (1) {
["ThoroughfareName"]=>
string(13) "317 E 34th St"
}
["PostalCode"]=>
object(SimpleXMLElement)#12 (1) {
["PostalCodeNumber"]=>
string(5) "10016"
}
}
}
}
}
["ExtendedData"]=>
object(SimpleXMLElement)#6 (1) {
["LatLonBox"]=>
object(SimpleXMLElement)#8 (1) {
["#attributes"]=>
array(4) {
["north"]=>
string(10) "40.7458050"
["south"]=>
string(10) "40.7431070"
["east"]=>
string(11) "-73.9736017"
["west"]=>
string(11) "-73.9762997"
}
}
}
["Point"]=>
object(SimpleXMLElement)#7 (1) {
["coordinates"]=>
string(24) "-73.9749507,40.7444560,0"
}
}
}
}
And here are the lines of code used to output which the $zip_code is blank for the first example, while with the second one it does work as expected.
echo "===================================\n";
echo "Status code is: " . $status . "\n";
echo "address line is:" . $address_line . "\n";
echo "=+=+=" . "\n";
echo "street is: " . $street . "\n";
echo "city is: " . $city . "\n";
echo "state is: " . $state . "\n";
echo "zip code is: " . $zip_code . "\n";
echo "country code is: " . $country_code . "\n";
echo "country name is: " . $country_name. "\n";
The different locations used in this example are both in New York City. I don't know why one is working while the other is not, expect I see it's returning the ["SubAdministrativeArea"]. Shouldn't the Google map API V3 return the same format of information for the same area? Is this even a factor in the problem I'm having? Am I not handling the objects and elements correctly? If so, please enlighten me because I am stuck at this point.
Or should I be checking for two different situations which might be returned (or more?) by the Google map API V3? Thanks!

You will notice the above contains a ["SubAdministrativeArea"]=> which the working example below does not. Does this matter?
Yes it matters.
Shouldn't the Google map API V3 return the same format of information for the same area?
Some elements of the answer will always be present at the same place in the result tree but some others depend on the request. I can't help you more on this as I never understood why there was sometimes more sublevels even if requests were very similar...
Is this even a factor in the problem I'm having?
Yes, because you can't rely on a fixed result structure.
Should I be checking for two different situations which might be returned (or more?) by the Google map API V3?
Another solution can be to use an other library than SimpleXML to handle the result, like DOM, which has functions to find children through a tree, like DOMElement::getElementsByTagName. Getting values needs a bit more coding than with SimpleXML but this way you don't need to check if there is a SubAdministrativeArea level or not.
I hope it helps.

Related

PHP Parse returned Object

As I am trying to read the mt940 file from ING with help of https://github.com/fruitl00p/php-mt940 I tried his test file. it returns like this.
array(1) { [0]=> object(Kingsquare\Banking\Statement)#4 (9) {
["bank":"Kingsquare\Banking\Statement":private]=> string(3) "ING"
["account":"Kingsquare\Banking\Statement":private]=> string(9) "111111111"
["transactions":"Kingsquare\Banking\Statement":private]=> array(3) {
[0]=> object(Kingsquare\Banking\Transaction)#5 (9) {
["account":"Kingsquare\Banking\Transaction":private]=> string(9) "111111111"
["accountName":"Kingsquare\Banking\Transaction":private]=> string(34) "V. DE JONG KERKSTRAAT 1154 1234 BW"
["price":"Kingsquare\Banking\Transaction":private]=> float(0.56)
["debitcredit":"Kingsquare\Banking\Transaction":private]=> string(1) "C"
["cancellation":"Kingsquare\Banking\Transaction":private]=> bool(false)
["description":"Kingsquare\Banking\Transaction":private]=> string(128) "0111111111 V. DE JONG KERKSTRAAT 1154 1234 BWENSCHEDE BET.KENM. 1004510036716378 3305330802AFLOSSINGSTERMIJN 188616 / 1E TERMIJN"
["valueTimestamp":"Kingsquare\Banking\Transaction":private]=> int(1279737000)
["entryTimestamp":"Kingsquare\Banking\Transaction":private]=> int(1279737000)
["transactionCode":"Kingsquare\Banking\Transaction":private]=> string(3) "078" }
[1]=> object(Kingsquare\Banking\Transaction)#6 (9) {
["account":"Kingsquare\Banking\Transaction":private]=> string(9) "111111111"
["accountName":"Kingsquare\Banking\Transaction":private]=> string(25) "CUSTOMER NL SPOEDBETALING"
["price":"Kingsquare\Banking\Transaction":private]=> float(10.45)
["debitcredit":"Kingsquare\Banking\Transaction":private]=> string(1) "C" [
"cancellation":"Kingsquare\Banking\Transaction":private]=> bool(false)
["description":"Kingsquare\Banking\Transaction":private]=> string(120) "0111111111 CUSTOMER NL SPOEDBETALINGGE2009120212345RE091202­3737 /RFB/NL­FMI­021209 NL­FMI­021209VOORSCHOTCOMMISSIE" ["valueTimestamp":"Kingsquare\Banking\Transaction":private]=> int(1279737000)
["entryTimestamp":"Kingsquare\Banking\Transaction":private]=> int(1279737000)
["transactionCode":"Kingsquare\Banking\Transaction":private]=> string(3) "077" }
}
["startPrice":"Kingsquare\Banking\Statement":private]=> float(44.89)
["endPrice":"Kingsquare\Banking\Statement":private]=> float(-9945.09)
["startTimestamp":"Kingsquare\Banking\Statement":private]=> int(1279737000)
["endTimestamp":"Kingsquare\Banking\Statement":private]=> int(1279823400)
["number":"Kingsquare\Banking\Statement":private]=> string(3) "100"
["currency":"Kingsquare\Banking\Statement":private]=> string(3) "EUR" } }
Here it returns with 'Kingsquare\Banking\Statement'. How do i remove that from entries results and how do I get the 'bank' , 'account', startprice, endprice and currency from the returned result.
here is the php program i have tried.
require 'vendor/autoload.php';
$parser = new \Kingsquare\Parser\Banking\Mt940();
$tmpFile = __DIR__ . '/test.mta';
$trans = $parser->parse(file_get_contents($tmpFile));
var_dump($trans);
You have those namespaces because you are dumping the varialbe out, to get the value you need just use the getter functions provided by Statement class:
$statement = $parser->parse(file_get_contents($tmpFile));
echo $statement[0]->getBank();
echo $statement[0]->getAccount();
echo $statement[0]->getStartPrice();
echo $statement[0]->getEndPrice();
echo $statement[0]->getCurrency();

How to access XML response

Good day, I want to access the XML response and echo it to display its value but I don't know how to do it. I already tried some few answers in StackOverflow but I fail.
This is my code.
<?php
error_reporting(E_ALL);
require_once 'ruFunctions.php';
$rentalsUnited = new rentalsUnited();
$ru= $rentalsUnited->getOwners();
if($ru != null){
$data= simplexml_load_string($ru);
var_dump($data); // it will return boof(false)
var_dump($ru);
echo $data->Pull_ListAllOwners_RS->Status['ID']; //Trying to get property of non-object
}
?>
Results for var_dump($ru);
object(SimpleXMLElement)#2 (3) {
["Status"]=>
string(7) "Success"
["ResponseID"]=>
string(32) "44065d9888304e8cba912bce4d131ab1"
["Owners"]=>
object(SimpleXMLElement)#3 (1) {
["Owner"]=>
object(SimpleXMLElement)#4 (7) {
["#attributes"]=>
array(1) {
["OwnerID"]=>
string(6) "429335"
}
["FirstName"]=>
string(5) "Test"
["SurName"]=>
string(7) "Tester"
["CompanyName"]=>
string(15) "Test Helpers"
["Email"]=>
string(23) "info#Test.com"
["Phone"]=>
string(12) "+13474707707"
["UserAccountId"]=>
string(3) "602"
}
}
}
It looks like $ru is already a SimpleXMLElement, so trying to call simplexml_load_string will fail on this.
You can see some of the details by
if($ru != null){
echo $ru->Status;
}
You can (probably) list the owners by...
if($ru != null){
foreach ($ru->Owners->Owner as $owner ) {
echo "ownerId=".$owner['OwnerID'].PHP_EOL;
echo "FirstName=".$owner->FirstName.PHP_EOL;
}
}

How to parse steam api JSON and print it out? PHP

I want to parse JSON that I recieve from steam via api link http://steamcommunity.com/id/theorangeass/inventory/json/753/1/ , but when I try to print it with echo it show nothing.
Here is the code
$data = file_get_contents('http://steamcommunity.com/id/theorangeass/inventory/json/753/1/');
$json = json_decode($data, true);
echo $json->success;
var_dump:
array(6) { ["success"]=> bool(true) ["rgInventory"]=> array(1) { ["922506184369981039"]=> array(5) { ["id"]=> string(18) "922506184369981039" ["classid"]=> string(10) "1254492673" ["instanceid"]=> string(10) "2070301907" ["amount"]=> string(1) "1" ["pos"]=> int(1) } } ["rgCurrency"]=> array(0) { } ["rgDescriptions"]=> array(1) { ["1254492673_2070301907"]=> array(18) { ["appid"]=> string(3) "753" ["classid"]=> string(10) "1254492673" ["instanceid"]=> string(10) "2070301907" ["icon_url"]=> string(116) "U8721VM9p9C2v1o6cKJ4qEnGqnE7IoTQgZI-VTdwyTBeimAcIowbqB-harb00cJ0fNdiCJoFB3O541FNc9ZPYXYjjL7UqfFEwOtgZKcs0eWlClqzSJn6" ["icon_url_large"]=> string(106) "U8721VM9p9C2v1o6cKJ4qEnGqnE7IoTQgZI-VTdwyTBeimAcIowbqB-harb00cJ0fNdiA54UEGOnqGQPJ9hDZHA50feEo7RMyO_GQNzkkA" ["icon_drag_url"]=> string(0) "" ["name"]=> string(4) "BEEP" ["market_name"]=> string(0) "" ["name_color"]=> string(0) "" ["background_color"]=> string(0) "" ["type"]=> string(4) "Gift" ["tradable"]=> int(0) ["marketable"]=> int(0) ["commodity"]=> int(0) ["cache_expiration"]=> string(20) "2017-01-02T00:00:00Z" ["fraudwarnings"]=> array(1) { [0]=> string(223) "This is a restricted gift which can only be redeemed in these countries: Armenia, Azerbaijan, Belarus, Georgia, Kyrgyzstan, Kazakhstan, Moldova, Republic of, Tajikistan, Turkmenistan, Uzbekistan, Ukraine, Russian Federation" } ["descriptions"]=> array(1) { [0]=> array(1) { ["value"]=> string(216) "A combination of Yoshi’s Island-style platforming with a gravity gun right out of Half-Life, BEEP is an amazing physics-platformer. Despite its friendly art style, this is a hardcore platformer in the truest sense." } } ["actions"]=> array(1) { [0]=> array(2) { ["name"]=> string(13) "View in store" ["link"]=> string(41) "http://store.steampowered.com/app/104200/" } } } } ["more"]=> bool(false) ["more_start"]=> bool(false) }
You are trying to echo a boolean. If you are wanting to echo true you're going to need to do an if, or a switch statement. An if statement would probably be easiest.
echo $json->success == true ? 'TRUE' : 'FALSE';
To parse the array to retrieve the item id's in the rgInventory to must do a foreach.
foreach ($json['rgInventory'] as $item) {
echo $item['id'];
}
Where you should start learning about arrays is Here

xmlfile to php array

i want to create an php array from an xml-file like this
test.xml
object(SimpleXMLElement)#3 (2) { ["uebertragung"]=> object(SimpleXMLElement)#5 (1) { ["#attributes"]=> array(9)
{ ["art"]=> string(7) "OFFLINE" ["umfang"]=> string(4) "TEIL"
["modus"]=> string(3) "NEW" ["version"]=> string(5) "1.2.7" ["sendersoftware"]=> string(7) "crm11" ["senderversion"]=> string(3) "1.1"
["techn_email"]=> string(18) "support#mail.com" ["timestamp"]=> string(19)
"2014-06-01T10:00:00" ["regi_id"]=> string(7) "ABCD143" } }
["anbieter"]=> object(SimpleXMLElement)#4 (3) { ["anbieternr"]=>
string(6) "144185" ["firma"]=> string(14) "redfirm"
["immobilie"]=> object(SimpleXMLElement)#6 (7) { ["objektkategorie"]=> object(SimpleXMLElement)#7 ...
mycode:
$xml = simplexml_load_file("test.xml") or die("Error: Cannot read xml file");
var_dump($xml);
echo "show1: " .$xml->openimmo->uebertragung->{'art'} . "<br>";
echo "show2: " .$xml->openimmo->uebertragung['art'];
show 1+2 show nothing, can someone help me, i dont understand the array structure ?
The art is an attribute, so you can get its value by
$xml->openimmo->uebertragung->attributes()->art
More here
http://php.net/manual/en/simplexmlelement.attributes.php

Update Check with PHP

I'm working on an update system that checks a remote file string
$local = simplexml_load_file(root_p.'/version.xml');
$remote = simplexml_load_file("mygithuburltoblob/version.xml");
if($local->build == $remote->build) {
} else {
echo "Version ".$remote->version." Available now";
}
But even if the build numbers match it still returns that the update is available. Does anyone know why that would be?
(Yes root_p is already defined, the problem isn't loading and retrieving the values)
Remote Var Dump
object(SimpleXMLElement)#12 (6) { ["title"]=> string(11) "Loopy Cubix" ["author"]=> string(12) "Morgan Green" ["version"]=> string(3) "1.0" ["build"]=> string(4) "1111" ["type"]=> string(5) "Alpha" ["feed"]=> object(SimpleXMLElement)#15 (0) { } }
Local Var Dump
object(SimpleXMLElement)#11 (6) { ["title"]=> string(24) "Looped Cubix Pre Release" ["author"]=> string(12) "Morgan Green" ["version"]=> string(3) "1.0" ["build"]=> string(4) "1111" ["type"]=> string(6) "Closed" ["feed"]=> object(SimpleXMLElement)#15 (0) { } }
On the top of the page is my output from
<?php
$local = simplexml_load_file(root_p.'/version.xml');
$remote = simplexml_load_file("https://raw.githubusercontent.com/Doxramos/Invontrol/master/version.xml");
echo "Local: ". gettype($local->build);
foreach($local->build as $build) {
echo $build. "<br />";
}
echo "Remote: ". gettype($remote->build);
foreach($remote->build as $build) {
echo $build. "<br />";
}
Shows both as an object with the same value.
As I see that oject is no equal, example of some compared elements:
["title"]=> string(11) "Loopy Cubix"
["title"]=> string(24) "Looped Cubix Pre Release"
The issue had to do with whitespace while parsing the XML data. I ended up fixing it by replacing
if($remote->build == $local->build) {
}
else {
//Output Update Information
}
with
$trimmed_local = trim($local->build);
$trimmed_remote = trim($remote->build);
And using the new variables as my comparison operators
if($trimmed_local == $trimmed_remote) {
}
else {
//Output Update Information
}

Categories