I'm using following code php to get timezone:
$url = 'http://api.geonames.org/timezone?lat=' . $latitude . '&lng=' . $longitude . '&username=demo';
$xml = simplexml_load_file($url);
foreach($xml->children() as $timezone)
{
echo "TimezoneId: ".$timezone->timezoneId." ";
echo "DstOffset : ".$timezone->dstOffset." ";
echo "GmtOffset : ".$timezone->gmtOffset." ";
}
it work but for latitude and longitude of Antartica for example it give error status message:
<status message="no timezone information found for lat/lng" value="15"/>
How to echo this message?
I'm tryng this:
if ($xml->status) {
echo "error: ".$timezone->status['message']. "";
}
but don't work
You are trying to get an element from object, which doesn't exist. In such a XML element you have attributes and some values like in your case: countryCode, countryName, dstOffset, gmtOffset and etc. If you use var_dump() the result you can see the error message is in these attributes, which is an array.
Here you are an example:
var_dump() on a location without problem:
object(SimpleXMLElement)#4 (12) {
["#attributes"]=>
array(1) {
["tzversion"]=>
string(11) "tzdata2014i"
}
["countryCode"]=>
string(2) "KG"
["countryName"]=>
string(10) "Kyrgyzstan"
["lat"]=>
string(7) "40.4246"
["lng"]=>
string(7) "74.0021"
["timezoneId"]=>
string(12) "Asia/Bishkek"
["dstOffset"]=>
string(3) "6.0"
["gmtOffset"]=>
string(3) "6.0"
["rawOffset"]=>
string(3) "6.0"
["time"]=>
string(16) "2015-07-09 19:53"
["sunrise"]=>
string(16) "2015-07-09 05:41"
["sunset"]=>
string(16) "2015-07-09 20:36"
}
And here a var_dump() of Antartica:
object(SimpleXMLElement)#4 (1) {
["#attributes"]=>
array(2) {
["message"]=>
string(41) "no timezone information found for lat/lng"
["value"]=>
string(2) "15"
}
}
You can easily handle and print this error message like that:
if ($xml->status) {
echo 'error:' . $timezone->attributes()->message;
}
try this,
<?php
$url = 'http://api.geonames.org/timezone?lat=' . $latitude . '&lng=' . $longitude . '&username=demo';
$xml = simplexml_load_file($url);
foreach ($xml->geoname as $o_location){
printf(
'Name %s<br>
lat is %s<br>
lon is %s<br>
geonameId is %s<br>
countryCode is %s<br>
countryName is %s<br>
fcl is %s<br>
fcode is %<br>
',
$o_location->name,
$o_location->lat,
$o_location->lng,
$o_location->geonameId,
$o_location->countryCode,
$o_location->countryName,
$o_location->fcl,
$o_location->fcode
);
}
?>
Related
First time working with Wordpress. I am looking to get a random row from a formcraft table so I can publish the winner of a draw from a bunch of registrations on a page.
The content I want from the formcraft table is stored like this in a column called content in a table called wp_formcraft_3_submissions:
[
{\"label\":\"Name\",\"value\":\"Annette\",\"identifier\":\"field1\",\"type\":\"oneLineText\",\"page\":1,\"page_name\":\"Step 1\",\"width\":\"100%\",\"altLabel\":\"Name\"},
{\"label\":\"Company Name\",\"value\":\"My Co \",\"identifier\":\"field3\",\"type\":\"oneLineText\",\"page\":1,\"page_name\":\"Step 1\",\"width\":\"100%\",\"altLabel\":\"Company Name\"},
{\"label\":\"Email\",\"value\":\"annette#email.com\",\"identifier\":\"field6\",\"type\":\"email\",\"page\":1,\"page_name\":\"Step 1\",\"width\":\"100%\",\"altLabel\":\"Email\"}
]
I am trying to pull a random row, then publish name, company name.
I am stuck, and wordpress is not logging any errors though I have turned on error logging. I am doing this via a shortcode. I would like to echo the contents on the screen to start with, but ultimately, echo just the three fields.
function wa_awards_winner() {
global $wpdb;
$sql = $wpdb->query("
SELECT *
FROM wp_formcraft_3_submissions
ORDER BY RAND()
LIMIT 1
");
$result = $wpdb->get_results( $sql );
if ( ! $result ) { return false; }
$winner = json_encode($result[0]->content);
echo $winner;
}
add_shortcode( 'get_wa_awards_winner', 'wa_awards_winner' );
Untilmately I am trying to get these values like this but it is not working:
// NAME
echo '<h3 class="col">' . $winner[0][0] . '</h3>';
// COMPANY NAME
echo '<h3 class="col">' . $winner[1][0] . '</h3>';
// EMAIL
echo '<h3 class="col">' . $winner[2][0] . '</h3>';
Appreciate any help. Thanks.
Trying suggestion below like this:
$winner = json_decode(stripslashes($result[0]->content),true);
echo '<div style="color:white;">' . print_r($winner, true) . '</div>';
I get;
[13-Jun-2019 12:28:25 UTC] PHP Notice: Undefined offset: 0 in /var/www/site.com/wp-content/themes/dp-click-Child/functions.php on line 30
[13-Jun-2019 12:28:25 UTC] PHP Notice: Trying to get property 'content' of non-object in /var/www/site.com/wp-content/themes/dp-click-Child/functions.php on line 30
This works thanks to suggestion below:
$sql = "
SELECT *
FROM wp_formcraft_3_submissions
ORDER BY RAND()
LIMIT 1
";
$result = $wpdb->get_results( $sql );
I then try and access the info lke this:
$winner = json_decode(stripslashes($result[0]->content),true);
echo var_dump($winner);
echo '<h3 style="color: white;">NAME: ' . $winner[0]["Name"] . '</h3>';
echo '<h3 style="color: white;">COMPANY: ' . $winner[1]["Company Name"] . '</h3>';
echo '<h3 style="color: white;">EMAIL: ' . $winner[2]["Email"] . '</h3>';
The var_dump returns:
array(3) { [0]=> array(8) { ["label"]=> string(4) "Name" ["value"]=> string(14) "Annette" ["identifier"]=> string(6) "field1" ["type"]=> string(11) "oneLineText" ["page"]=> int(1) ["page_name"]=> string(6) "Step 1" ["width"]=> string(4) "100%" ["altLabel"]=> string(4) "Name" } [1]=> array(8) { ["label"]=> string(12) "Company Name" ["value"]=> string(19) "My Company" ["identifier"]=> string(6) "field3" ["type"]=> string(11) "oneLineText" ["page"]=> int(1) ["page_name"]=> string(6) "Step 1" ["width"]=> string(4) "100%" ["altLabel"]=> string(12) "Company Name" } [2]=> array(8) { ["label"]=> string(5) "Email" ["value"]=> string(20) "annette#email.com" ["identifier"]=> string(6) "field6" ["type"]=> string(5) "email" ["page"]=> int(1) ["page_name"]=> string(6) "Step 1" ["width"]=> string(4) "100%" ["altLabel"]=> string(5) "Email" } }
But my variables return empty:
NAME:
COMPANY NAME:
EMAIL:
First you need to make "stripslashing" and to turn the given string to JSON data. Then you need to decode it, not encode.
Here it is:
$winner = json_decode(stripslashes($result[0]->content),true);
echo $winner[0]["value"];
And one more thing. $wpdb->query can't be used inside get_results. Remove those and use this instead
$sql = "
SELECT *
FROM wp_formcraft_3_submissions
ORDER BY RAND()
LIMIT 1
";
$result = $wpdb->get_results( $sql );
I am trying to retrieve my city visitor from my website using codeigniter
Here is code to show city
$city = $this->geolocation->get_city();
Here the output from geolocation is like
array(11) { ["statusCode"]=>string(2) "OK" ["statusMessage"]=> string(0) "" ["ipAddress"]=> string(12) "202.60.21.15" ["countryCode"]=> string(2) "ID" ["countryName"]=> string(9) "Indonesia" ["regionName"]=> string(10) "Jawa Timur" ["cityName"]=> string(11) "Pacarkeling" ["zipCode"]=> string(5) "60132" ["latitude"]=> string(6) "-7.258" ["longitude"]=> string(7) "112.758" ["timeZone"]=> string(6) "+07:00" } OK202.67.41.25IDIndonesiaJawa
I am using
$cities = json_decode($city, true);
foreach ($cities as $c) {
echo $c;
}
and the output is like this
OK202.67.01.25IDIndonesiaJawa TimurPacarkeling60132-7.258112.758+07:00
How can I get each data from the output? I want to print each data from Geolocation library in Codeingiter like
Your Visitor Country : Indonesia
Your IP Visitor : 202.67.01.25
Your City Name : Pacarkeling
Thank you very much :)
Are you asking for the keys of the array? If so, you can do something like this:
$city = $this->geolocation->get_city();
$city_info = json_decode($city, true);
foreach ($city_info as $key=>$value) {
echo 'Your ' . $key . ' : ' . $value . '<br>';
}
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.
I get "Invalid argument supplied" when using the following code. I can successfully parse an ip address and port number but i dont know how to get more than one at a time. My foreach loop is not working. Any ideas?
$dom = new DOMDocument();
$dom->loadHTMLFile($url);
$xml = simplexml_import_dom($dom);
$dom_results = $xml->xpath("/html/body/div[#id='subpagebgtabs']/div[#id='container']/table[#id='listtable']");
$ip_address = $dom_results[0]->tr->td[1]->span;
$ip_post = $dom_results[0]->tr->td[2];
$address_parts = $ip_address.":".$ip_post;
foreach ($address_parts as $address_full){
echo $address_full . "<br>";
}
$Dom_Results Output
["tr"]=>
array(50) {
[0]=>
object(SimpleXMLElement)#5 (3) {
["#attributes"]=>
array(2) {
["class"]=>
string(0) ""
["rel"]=>
string(7) "9054676"
}
["comment"]=>
object(SimpleXMLElement)#56 (0) {
}
["td"]=>
array(8) {
[0]=>
object(SimpleXMLElement)#57 (2) {
["#attributes"]=>
array(2) {
["class"]=>
string(20) "leftborder timestamp"
["rel"]=>
string(10) "1309047901"
}
["span"]=>
string(10) "2 minutes"
}
[1]=>
object(SimpleXMLElement)#58 (1) {
["span"]=>
string(13) "122.72.10.201"
}
[2]=>
string(3) "80"
I think this is what you're looking for:
// If results are found
if ( ! empty($dom_results) )
// Loop through each result. Based on your XPath query, the $dom_results
// contains tables. This loops through the rows of the first table.
foreach ( $dom_results[0]->tr as $row )
{
$ip_address = $row->td[1]->span;
$ip_post = $row->td[2];
// Output the address
echo $ip_address . ":" . $ip_post . "<br />";
}
it seems you want to extract all the ip address and port numbers and concatenate it like
ipaddress:port
So try this
foreach($dom_results as $dom) {
$ip = $dom->tr->td[1]->span;
$port = $dom->tr->td[2];
$address = $ip . ":". $port;
echo $address . "<br />";
}
So I run some code like this:
$quote = simplexml_load_string($xml);
$quote = $quote->Stock;
echo 'Name: ';
echo $quote->Name;
echo '<br>';
echo 'Sybmol: ';
echo $quote->Symbol;
echo '<br>';
echo 'Last Price: ';
echo $quote->Last;
echo '<br>';
echo 'Earnings To Price Ratio: ';
echo $quote->P-E;
echo '<br>';
I know that the second to last line ($quote->P-E) is incorrect - I don't think you can use dashes like that. But for some reason I can't figure out how to access that property. The weird thing is that's how it's written if I var_dump($quote) (It's towards the end):
object(SimpleXMLElement)#17 (16) { ["Symbol"]=> string(4) "AAPL" ["Last"]=> string(6) "271.87" ["Date"]=> string(9) "6/17/2010" ["Time"]=> string(6) "3:59pm" ["Change"]=> string(5) "+4.62" ["Open"]=> string(6) "270.72" ["High"]=> string(6) "272.90" ["Low"]=> string(6) "269.50" ["Volume"]=> string(8) "31195032" ["MktCap"]=> string(6) "247.4B" ["PreviousClose"]=> string(6) "267.25" ["PercentageChange"]=> string(6) "+1.73%" ["AnnRange"]=> string(15) "132.88 - 272.90" ["Earns"]=> string(6) "11.796" ["P-E"]=> string(5) "22.66" ["Name"]=> string(10) "Apple Inc." }
How should I be accessing this attribute/property?
$quote->{'P-E'};