I am writing that post because i'm trying to solve a problem which seems to be stupid but unfortunately i cannot make it. I have an XML file (link) which i'm parsing with simplexml_load_file($link) as below:
function getListPoints($countryCode,$nbrPoints)
{
$xml_containt_url = "http://openchargemap.org/api/?output=xml&countrycode=".$countryCode."&maxresults=".$nbrPoints;
$xml_output = simplexml_load_file($xml_containt_url);
return $xml_output;
}
So when I print the xml output:
$infos_point = getListPoints("US",2);
print($infos_point);
I get everything I want, it gives me :
SimpleXMLElement Object
(
[ChargePoint] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ID] => 2381
[UUID] => BFE199D5-07D4-4310-86D7-8BCB9092541D
[DateLastConfirmed] => 31/08/2010 00:00:00
[OperatorID] => 1
[OperatorTitle] => (Unknown Operator)
[DataProviderID] => 2
[NumberOfPoints] =>
[DataQualityLevel] => 1
[PercentageSimilarity] =>
)
[GeneralComments] => 1 SP Inductive
[AddressInfo] => SimpleXMLElement Object
(
[LocationTitle] => Sacramento County Public Garage
[AddressLine1] => 725 7th St
[AddressLine2] => SimpleXMLElement Object
(
)
[Town] => Sacramento
[StateOrProvince] => CA
[Postcode] => 95814
[Country] => United States
[Latitude] => 38.5846
[Longitude] => -121.4961
[ContactTelephone1] => 916-874-6227
[AccessComments] => 24 hours daily; pay lot
[RelatedURL] => SimpleXMLElement Object
(
)
)
The problem appears when i want to get some values of the SimpleXMLElement object, for example, I want to get the Town value, so I proceed as it follows :
$Town = $infos_point->ChargePoint[0]->AdressInfo->Town;
print($Town);
And it gives me a blank page. I have tried everything I've read on the internet and still nothing.
Maybe somebody can tell me what's happening ? it would be great as i will be able to continue my project.
It's your spelling of adDress. Should be: $Town = $infos_point->ChargePoint[0]->AddressInfo->Town;
Related
I am working a smole project airport search using Rapidapi but
i can't display json data from API in my codeigniter view. i'm googling meany time but not perfect result. please help
here is my json output
HttpResponse Object
(
[code:HttpResponse:private] => 200
[raw_body:HttpResponse:private] => [{"airportId":"6f576bf7-090a-46e3-be70-6d8a55275e04","code":"YYZ","name":"Toronto, Ontario","location":{"longitude":-79.63083299999998,"latitude":43.677222},"cityId":"8f65ce90-aafb-42b4-8185-ae4f1b131889","city":"Toronto","countryCode":"CA","themes":[],"pointsOfSale":["CA"]}]
[body:HttpResponse:private] => Array
(
[0] => stdClass Object
(
[airportId] => 6f576bf7-090a-46e3-be70-6d8a55275e04
[code] => YYZ
[name] => Toronto, Ontario
[location] => stdClass Object
(
[longitude] => -79.630833
[latitude] => 43.677222
)
[cityId] => 8f65ce90-aafb-42b4-8185-ae4f1b131889
[city] => Toronto
[countryCode] => CA
[themes] => Array
(
)
[pointsOfSale] => Array
(
[0] => CA
)
)
)
)
my Controller
public function AirportSearch_form(){
$url="https://cometari-airportsfinder-v1.p.rapidapi.com/api/airports/by-code?code=yyz";
$response = $this->unirest->get($url, $headers = array("X-Mashape-Key" => "ced348bae5mshd648601c9de77cbp1e2dcejsn222973a7564d", "X-Mashape-Host" => "cometari-airportsfinder-v1.p.rapidapi.com"));
//echo'<pre>';
//print_r($response);
//exit();
$jdata=json_decode($response);
$app_title=$this->SuperAdmin_model->AppDataShow();
$data['title']=$app_title->app_title;
$data['menu_col']='';
$this->load->view('agent/header',$data);
$this->load->view('flight/airport_search',$jdata);
$this->load->view('agent/footer',$data);
}
you have both json and non-json versions retrieved. as the latter doesn't require decoding i suggest you use that.
it seems like you can do $response->body[0] to get the array.
and specific items via $response->body[0]->name .etc.
I am trying to create a wordpress shortcode which returns the price of a book from an API.
Ideally, I would like to be able to create a shortcode with this format [currency_isbn13] but for the moment I'd be happy to just create a fixed shortcode for each book and currency.
After various attempts, I was able to put together the following php code, but it is not working as it should
function Price() {
$isbn13 = 9783899735215;
$url = 'https://api.bookdepository.com/search/lookup?isbn13='.$isbn13.'&clientId={redact}&authenticationKey={redact}&IP={redact}¤cies=GBP';
$sxml = simplexml_load_file($url);
print_r($sxml);
return $sxml->price;
}
add_shortcode('isbn13', 'Price');
I would expect the shortcode to return the price of the book, but I get the following:
How can I fix this? I'm sure it is very simple but can't figure it out
SimpleXMLElement Object ( [resultset] => SimpleXMLElement Object ( [status] => Success [results] => 1 [totalResults] => 1 [currentPage] => 1 [totalPages] => 1 ) [items] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [identifiers] => SimpleXMLElement Object ( [isbn13] => 9783899735215 ) [url] => https://www.bookdepository.com/Crocodile-Newts-Axel-Hernandez/9783899735215 [biblio] => SimpleXMLElement Object ( [title] => Crocodile Newts [format] => Hardback ) [availability] => Available - dispatched from the UK in 4 business days [pricing] => SimpleXMLElement Object ( [price] => SimpleXMLElement Object ( [#attributes] => Array ( [currency] => GBP ) [selling] => 50.27 ) ) [contributors] => SimpleXMLElement Object ( [contributor] => SimpleXMLElement Object ( [name] => Axel Hernandez [roleDescription] => By (author) [url] => https://www.bookdepository.com/author/Axel-Hernandez ) ) ) ) )
Looks as though you need to expand
return $sxml->price;
as your structure is more complex. Looks as though
return (string)$sxml->items->item->pricing->price->selling;
The cast to (string) makes the value easier to use elsewhere.
First, I'm using sugarcrm pro 6.5 and accessing via rest v4, so I have this array that's being returned from printing $results that is working fine:
stdClass Object
(
[result_count] => 2000
[total_count] => 3390
[next_offset] => 2000
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => 77da301b-83dd-4fe6-e38f-53ba151fb084
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => 77da301b-83dd-4fe6-e38f-53ba151fb084
)
[name] => stdClass Object
(
[name] => name
[value] => Jim Beam
)
[status] => stdClass Object
(
[name] => status
[value] => Dead
)
[website] => stdClass Object
(
[name] => website
[value] => website.com
)
[phone_cr] => stdClass Object
(
[name] => phone_cr
[value] => 1-888-888-8888
)
)
)
[1] => stdClass Object
(
[id] => d0ecc069-d556-98f3-41f2-53ba1468327a
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => d0ecc069-d556-98f3-41f2-53ba1468327a
)
[name] => stdClass Object
(
[name] => name
[value] => John Doe
)
[status] => stdClass Object
(
[name] => status
[value] => New
)
[website] => stdClass Object
(
[name] => website
[value] => web.com
)
[phone_cr] => stdClass Object
(
[name] => phone_cr
[value] => 1-888-888-8888
)
)
)
I'm using a query from the api to filter the results for the user I'm targeting:
'query' => "leads.assigned_user_id='user_ID-here'",
'order_by' => "date_entered DESC",
This works fine. So I've ran a foreach () statement to retrieve only one field on a button click, which also works just fine. What I really need to accomplish is before this statement, a foreach() command (or something else?) to filter out and retrieve ONLY the "New" results in the status value, and from that group output an array showing only the website field. Seen in the "desired end result section of this question."
This is the code I'm filtering the field I'm targeting and having a new array created with if that helps bridge the gap:
$results = call('get_entry_list', $params, $url);
$eresult = array();
foreach ($results->entry_list as $index=>$value_list) {
$listed = $value_list->name_value_list->website->value;
$eresult[] = $listed;}
So the desired end result based on this data should be:
Array
(
[1] => web.com
)
I'm unsure what I need to do to filter the "Status" field to only then be ran with the $eresult array I created to achieve this. To be clear, everything is working as it should, and my print from $eresult is outputting exactly as it should by returning all results in the website value area, I just need some help to get it sorted before going to that step by sorting it by the "new" status first without all the extra 'stuff,' then sorting out the array in my desired format with the foreach() statement above. I tried to cut out all the other code, as it's a pretty long project, so this should be all the relevant information for the particular goal I need to accomplish in this segment. Any help is greatly appreciated! Thank you!
I've decided to create a second script for this as a temp solution by adding:
'query' => "(leads.assigned_user_id='user_ID-here') AND (status='New')"
So I guess that works, I was trying to avoid calling another script for just one separate function, but it is working fine.
I am using an API to get some values. When I print my variable returning an array from api it returns stdObject Class
This is how I request the data:
$graphObject = (new FacebookRequest( $session, 'GET', '/me?fields=id,first_name,last_name,tagged_places' ))->execute()->getGraphObject()->asArray();
This is how I am printing it:
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
This is what it returns:
Array
(
[id] => 111111
[first_name] => Bob
[last_name] => Stevensen
[tagged_places] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[id] => 111111
[created_time] => 1905-01-01T08:00:00+0000
[place] => stdClass Object
(
[id] => 1111111
[location] => stdClass Object
(
[latitude] => 36.1313
[longitude] => -95.9373
)
[name] => Tulsa, Oklahoma
)
)
[1] => stdClass Object
(
[id] => 11111
[created_time] => 2014-05-30T21:41:11+0000
[place] => stdClass Object
(
[id] => 111111
[location] => stdClass Object
(
[city] => Okmulgee
[country] => United States
[latitude] => 35.623012460758
[longitude] => -95.972782756346
[state] => OK
[street] => 104 S Morton Ave
[zip] => 74447-5022
)
[name] => Ike's Downtown Pub & Eatery
)
)
I have found some examples of this stating the way to get a value out of this class object would be like this,
echo $graphObject->{'$STDCLASS'}->{'$FIELD I'M TRYING TO GET'};
I am swapping out these values trying to figure out what the object name is but I get this error:
Notice: Trying to get property of non-object in /file/path
What is the correct way to get the values from this stdClass Object I think maybe I am just not understanding the name of the Object Is it possible to tell from the code I have posted here?
the object contains an array called Data you can access:
foreach ($graphObject->data as $data) {
echo $data->id
}
This takes the object and should make it directly accessible
though on second thought .. you may also need to try echo $data['id']; as its not an object any more..
If that doenst work .. try :
$info = json_decode($graphObject,true);
and see if you can work the $info variable easier.
For the life of me I can not figure out how to access the values of this array. Every example the stdClass Object has some type of value. If I try for example $obj->0->0->city; I get an error.
Can someone show me a example how to access [city] => toronto or even [date_created] => 2011-05-03 14:33:58?
I also tried this with no luck.
$object = $buy[1];
$title = $object->title[0];
echo "$title";
Thanks
This is what the api gives me.
stdClass Object
(
[id] => 1
[name] => toronto
[date_modified] => 2011-03-08 13:07:10
[tax_rate_provincial] =>
)
<br/>
Array
(
[0] => stdClass Object
(
[0] => stdClass Object
(
[id] => 28131844
[full_date] => 20110506
[end_date] => 20110511
[city] => toronto
[saved] => 1651
[discount_percentage] => 52
[deal_option] => Array
(
[0] => stdClass Object
(
[id] => 2600
[title] =>
[date_modified] => 0000-00-00 00:00:00
[date_created] => 2011-05-03 14:33:58
[value] => 3150
[price] => 1499
[deal_id] => 28131844
[is_default] => 0
)
)
[options] =>
[option_quantity] =>
[option_remaining] =>
[purchase_limit] => 1
[gift_limit] => 0
There is a special evil syntax to bypass numeric object attributes:
print $obj->{'0'}->{'0'}->city;
Is the correct syntax, and equivalent to the path you already determined.
Your second example is an array however, so it's probably:
print $array[0]->{'0'}->city;
The alternative is always to just foreach over a specific level - that works for objects and arrays likewise.