PHP parse soapClient response transfer to a table - php

I'm just on the hose and dont get on.
I am not a professional programmer but have so far with much reading and over numerous attempts everything so far created what I have undertaken, only with this I have probably found my masterpiece.
I have a response of a SOAP query and would like to display the values in a table for each powerUnitidentifier. What is the best way to do this?
(
[RawData] => stdClass Object
(
[from] => 2022-05-10T01:00:00+02:00
[to] => 2022-05-10T01:20:00+02:00
[dataRecords] => stdClass Object
(
[record] => Array
(
[0] => stdClass Object
(
[powerUnitIdentifier] => abc123
[time] => 2022-05-10T01:00:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.27
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
[1] => stdClass Object
(
[powerUnitIdentifier] => zyx321
[time] => 2022-05-10T01:00:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.19
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
[2] => stdClass Object
(
[powerUnitIdentifier] => abc123
[time] => 2022-05-10T01:10:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.15
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
[3] => stdClass Object
(
[powerUnitIdentifier] => zyx321
[time] => 2022-05-10T01:10:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.09
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
)
)
)
)```

You loop over the section of the data, starting the foreach loop at the right level of your data structure
foreach($theName->RawData->DataRecords->record as $obj) {
echo $obj->powerUnitIdentifier;
}
Or if you ment to process the sub array of that
foreach($theName->RawData->DataRecords->record as $obj) {
echo $obj->powerUnitIdentifier . '<br>';
foreach( $obj->fields as $field) {
echo $field->identifier . ',' . $field->value . '<br>';
}
}

Related

How to fetch data from array and insert to database

Hello i Have a problem related to array,i am trying to request graph api and saves data in database.here is my array
Array
(
[threadId] => t_mid.1445648572476:316f06a4adc6d7dd57
[sender] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
[1] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
)
)
[messages] => Array
(
[messages] => Array
(
[0] => Array
(
[message] => Same to u dear.
[from] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
[created_time] => 2015-12-31T11:31:24+0000
[id] => m_mid.1451561484487:5b3276ddd9d86d1480
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => source:chat
)
)
)
)
[1] => Array
(
[message] => Happy New Year to all of you
[from] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
[created_time] => 2015-12-31T10:37:47+0000
[id] => m_mid.1451558267514:4c017e5a1f8e997586
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => sent
)
[3] => stdClass Object
(
[name] => source:web
)
)
)
)
[2] => Array
(
[message] => Happy New Year to all of you
[from] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
[created_time] => 2015-12-31T09:44:49+0000
[id] => m_mid.1451555089630:1036078c7bcabeed78
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => sent
)
[3] => stdClass Object
(
[name] => source:web
)
)
)
)
[3] => Array
(
[message] => Happy New Year to all of you
[from] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
[created_time] => 2015-12-31T09:43:28+0000
[id] => m_mid.1451555008621:62c3365e35ededc773
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => sent
)
[3] => stdClass Object
(
[name] => source:web
)
)
)
)
[4] => Array
(
[message] => Very Good
[from] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
[created_time] => 2015-09-01T09:13:34+0000
[id] => m_mid.1445648574720:c2746f8b55fff17369
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => source:chat
)
)
)
)
[5] => Array
(
[message] => Good
[from] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
[created_time] => 2015-09-01T09:13:32+0000
[id] => m_mid.1445648572476:316f06a4adc6d7dd57
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => source:chat
)
)
)
)
)
)
)
This is my php code from that i fetch data
foreach($data as $evaluatemessage){
//echo "<pre>";print_r($evaluatemessage); die;
$inbox=array();
$inbox['thread_id']=$evaluatemessage['threadId'];
foreach($evaluatemessage['messages'] as $messagedet){
//print_r($messagedet[0]['id']); die;
$inbox['smg_id']=$messagedet[$j]['id'];
$inbox['body']=$messagedet[$j]['message'];
$inbox['time']=$messagedet[$j]['created_time'];
//$inbox['msg_type']=$data;
//$inbox['user_id']=$data;
$inbox['From_name']=$messagedet[$j]['from']->name;
$inbox['To_name']=$messagedet[$j]['to']->data[0]->name;
$inbox['From_id']=$messagedet[$j]['from']->id;
$inbox['To_id']=$messagedet[$j]['to']->data[0]->id;
$inbox['tags']=$messagedet[$j]['tags']->data[1]->name;
//$inbox['Page_id']=$data;
//echo "<pre>";print_r($inbox); die;
$inbox['Page_id']='12345';
$this->facebook_inboxes->save($inbox);
$j++;
}
$i++;
}
when i hit with this code only a single threadid and single message contents are saved.but rest are not,i want every thread id with there all messages id and message body are saved row by row but unable to do that can anyone help me how i do that.
Write this code before Save function
$this->facebook_inboxes->create();
This is enbuild function of cake to store multiple row.
There is another messages array inside the messages array. So according to your array structure, your second loop will be like this :
foreach($evaluatemessage['messages']['messages'] as $messagedet){
//print_r($messagedet[0]['id']); die;
$inbox['smg_id']=$messagedet[$j]['id'];
$inbox['body']=$messagedet[$j]['message'];
$inbox['time']=$messagedet[$j]['created_time'];
.
.
.
}
and you are using only:
foreach($evaluatemessage['messages'] as $messagedet){}
here is the full code :
foreach($data as $evaluatemessage){
//echo "<pre>";print_r($evaluatemessage); die;
$inbox=array();
$inbox['thread_id']=$evaluatemessage['threadId'];
foreach($evaluatemessage['messages']['messages'] as $messagedet){
//print_r($messagedet[0]['id']); die;
$inbox['smg_id']=$messagedet[$j]['id'];
$inbox['body']=$messagedet[$j]['message'];
$inbox['time']=$messagedet[$j]['created_time'];
//$inbox['msg_type']=$data;
//$inbox['user_id']=$data;
$inbox['From_name']=$messagedet[$j]['from']->name;
$inbox['To_name']=$messagedet[$j]['to']->data[0]->name;
$inbox['From_id']=$messagedet[$j]['from']->id;
$inbox['To_id']=$messagedet[$j]['to']->data[0]->id;
$inbox['tags']=$messagedet[$j]['tags']->data[1]->name;
//$inbox['Page_id']=$data;
//echo "<pre>";print_r($inbox); die;
$inbox['Page_id']='12345';
$this->facebook_inboxes->save($inbox);
$j++;
}
$i++;
}

Using a PHP for loop to display repeating nodes from a huge external XML feed

I've been at this problem for a week or so and haven't found any information in already exiting questions that have lead to a solution. I am pulling in a very large XML file from a company (it's a MITS Feed) that easily tops a million lines of output. I unfortunately can not give the location of this file due to legal reasons, but i'll explain what I'm trying to do and maybe you all can help me put this issue to rest.
I'm calling in the file as such:
<?php
$xml=simplexml_load_file("location-of-file/feed.xml");
?>
After this I want to run through and pull out pieces of information for each property. Here is the exact schema we are using -
http://www.mitsproject.com/Content/ServeFile.cfm?FileID=4075
I've tried importing this as a DOMDocument and traversing through the XML that way, but have had no luck. Possibly just an example of how to pull all of the Property nodes in a PHP for loop would be the bomb. Thank you in advance.
P.S. I know the file is at least being grabbed, because when I run -
<?php
$xml = simplexml_load_file("location-of-file/feed.xml", null, LIBXML_NOCDATA);
print_r($xml);
?>
I am getting an insanely large output.
UPDATE:
Here is an example of one 'Property' as a SimpleXMLElement, maybe this will help -
[111] => SimpleXMLElement Object
(
[#attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
)
[PropertyID] => SimpleXMLElement Object
(
[Identification] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
[OrganizationName] => rentershq
[IDType] => property
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[IDValue] => 1a47b013fa94796a8973a23cc8b01192ba603460
[OrganizationName] => rentershq
[IDType] => Company
)
)
)
[MarketingName] => Clean Older Unit
[WebSite] => http://rentershq.appfolio.com/listings/listings/56bce392-b9fb-4290-97bc-900b9ebf9a1e
[Address] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AddressType] => property
)
[Description] => Address of Available Listing
[AddressLine1] => 2850 PIONEER DRIVE - 17
[City] => REDDING
[State] => CA
[PostalCode] => 96001
[Country] => US
)
[Phone] => SimpleXMLElement Object
(
[#attributes] => Array
(
[PhoneType] => office
)
[PhoneNumber] => (530) 722-0800
)
[Email] => james#rentershq.com
)
[ILS_Identification] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ILS_IdentificationType] => Apartment
[RentalType] => Market Rate
)
[Latitude] => 40.5873599
[Longitude] => -122.41376
[LastUpdate] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Month] => 12
[Day] => 4
[Year] => 2013
)
)
)
[Information] => SimpleXMLElement Object
(
[StructureType] => Standard
[UnitCount] => 1
[ShortDescription] => Clean Older Unit
[LongDescription] => Please drive by property. Please contact onsite manager Joe Skeen to view inside of unit. 530-255-8375
[Rents] => SimpleXMLElement Object
(
[StandardRent] => 500.00
)
[PropertyAvailabilityURL] => http://rentershq.appfolio.com/listings/listings/56bce392-b9fb-4290-97bc-900b9ebf9a1e
)
[Fee] => SimpleXMLElement Object
(
[ProrateType] => Standard
[LateType] => Standard
[LatePercent] => 0
[LateMinFee] => 0
[LateFeePerDay] => 0
[NonRefundableHoldFee] => 0
[AdminFee] => 0
[ApplicationFee] => 15.00
[BrokerFee] => 0
)
[Deposit] => SimpleXMLElement Object
(
[#attributes] => Array
(
[DepositType] => Security Deposit
)
[Amount] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmountType] => Actual
)
[ValueRange] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Exact] => 700.00
[Currency] => USD
)
)
)
)
[Policy] => SimpleXMLElement Object
(
[Pet] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Allowed] => false
)
)
)
[Phase] => SimpleXMLElement Object
(
[#attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
)
[Name] => SimpleXMLElement Object
(
)
[Description] => SimpleXMLElement Object
(
)
[UnitCount] => 1
[RentableUnits] => 1
[TotalSquareFeet] => 625
[RentableSquareFeet] => 625
)
[Building] => SimpleXMLElement Object
(
[#attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
)
[Name] => SimpleXMLElement Object
(
)
[Description] => SimpleXMLElement Object
(
)
[UnitCount] => 1
[SquareFeet] => 625
)
[Floorplan] => SimpleXMLElement Object
(
[#attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
)
[Name] => SimpleXMLElement Object
(
)
[UnitCount] => 1
[Room] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[RoomType] => Bedroom
)
[Count] => 2
[Comment] => SimpleXMLElement Object
(
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[RoomType] => Bathroom
)
[Count] => 1
[Comment] => SimpleXMLElement Object
(
)
)
)
[SquareFeet] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Min] => 625
[Max] => 625
)
)
[MarketRent] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Min] => 500
[Max] => 500
)
)
[EffectiveRent] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Min] => 500
[Max] => 500
)
)
)
[ILS_Unit] => SimpleXMLElement Object
(
[#attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
)
[Units] => SimpleXMLElement Object
(
[Unit] => SimpleXMLElement Object
(
[Identification] => SimpleXMLElement Object
(
[#attributes] => Array
(
[IDValue] => 56bce392-b9fb-4290-97bc-900b9ebf9a1e
[OrganizationName] => RentersHQ
)
)
[MarketingName] => Clean Older Unit
[UnitBedrooms] => 2
[UnitBathrooms] => 1.0
[MinSquareFeet] => 625
[MaxSquareFeet] => 625
[SquareFootType] => internal
[UnitRent] => 500.00
[MarketRent] => 500.00
[Address] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AddressType] => property
)
[AddressLine1] => 2850 PIONEER DRIVE - 17
[City] => REDDING
[PostalCode] => 96001
[Country] => US
)
)
)
[Availability] => SimpleXMLElement Object
(
[VacateDate] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Month] => 7
[Day] => 26
[Year] => 2013
)
)
[VacancyClass] => Unoccupied
[MadeReadyDate] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Month] => 7
[Day] => 26
[Year] => 2013
)
)
)
[Amenity] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Other
)
[Description] => Ground Level Apartment
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Other
)
[Description] => HUD Considered
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Other
)
[Description] => Month-to-Month Lease
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Other
)
[Description] => Credit Check Required
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Other
)
[Description] => Stove
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Other
)
[Description] => Window Coverings
)
[6] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Carpet
)
[Description] => Tille and Carpet
)
[7] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Other
)
[Description] => Off Street Parking
)
[8] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Dryer
)
[Description] => Coin-Op Washer / Dryer
)
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Washer
)
[Description] => Coin-Op Washer / Dryer
)
[10] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Refrigerator
)
[Description] => Refrigerator
)
[11] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => DishWasher
)
[Description] => Dishwasher
)
[12] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Washer
)
[Description] => Dishwasher
)
[13] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Disposal
)
[Description] => Garbage Disposal
)
[14] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Heat
)
[Description] => Elec. Heating
)
[15] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Other
)
[Description] => Window Cooler
)
[16] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Other
)
[Description] => Dog Not Allowed
)
[17] => SimpleXMLElement Object
(
[#attributes] => Array
(
[AmenityType] => Other
)
[Description] => Cat Not Allowed
)
)
)
[File] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Active] => true
[FileID] => 809492874
)
[FileType] => Photo
[Description] => Unit Photo
[Name] => SimpleXMLElement Object
(
)
[Caption] => SimpleXMLElement Object
(
)
[Format] => image/jpeg
[Src] => http://pa.cdn.appfolio.com/rentershq/images/8ed6dbf4-36cf-47e4-b519-7783bea727dc/medium.jpg
[Width] => 1280
[Height] => 960
[Rank] => 1
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Active] => true
[FileID] => 809492885
)
[FileType] => Photo
[Description] => Unit Photo
[Name] => SimpleXMLElement Object
(
)
[Caption] => SimpleXMLElement Object
(
)
[Format] => image/jpeg
[Src] => http://pa.cdn.appfolio.com/rentershq/images/9b6b35e7-0ca1-4c20-a396-4be6fb21bf80/medium.jpg
[Width] => 1280
[Height] => 960
[Rank] => 2
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Active] => true
[FileID] => 809492887
)
[FileType] => Photo
[Description] => Unit Photo
[Name] => SimpleXMLElement Object
(
)
[Caption] => SimpleXMLElement Object
(
)
[Format] => image/jpeg
[Src] => http://pa.cdn.appfolio.com/rentershq/images/c6ab69e3-0787-4221-893d-fbd982c2a3bd/medium.jpg
[Width] => 1280
[Height] => 960
[Rank] => 3
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Active] => true
[FileID] => 809492889
)
[FileType] => Photo
[Description] => Unit Photo
[Name] => SimpleXMLElement Object
(
)
[Caption] => SimpleXMLElement Object
(
)
[Format] => image/jpeg
[Src] => http://pa.cdn.appfolio.com/rentershq/images/6ac14176-73c3-452b-80ae-b41820887c73/medium.jpg
[Width] => 1280
[Height] => 960
[Rank] => 4
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Active] => true
[FileID] => 809492892
)
[FileType] => Photo
[Description] => Unit Photo
[Name] => SimpleXMLElement Object
(
)
[Caption] => SimpleXMLElement Object
(
)
[Format] => image/jpeg
[Src] => http://pa.cdn.appfolio.com/rentershq/images/252a2ae1-66ea-471d-8cf1-48e795add81d/medium.jpg
[Width] => 960
[Height] => 1280
[Rank] => 5
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Active] => true
[FileID] => 809492895
)
[FileType] => Photo
[Description] => Unit Photo
[Name] => SimpleXMLElement Object
(
)
[Caption] => SimpleXMLElement Object
(
)
[Format] => image/jpeg
[Src] => http://pa.cdn.appfolio.com/rentershq/images/b93c93f6-b571-4a33-a4e3-e0ac663679bc/medium.jpg
[Width] => 1280
[Height] => 960
[Rank] => 6
)
)
)
try outputting the feed using
echo '<pre>';
print_r($feed);
echo '</pre>';
This should give you a much 'prettier' view of the scheme and what nodes are/aren't accessible!
I'd recommend something similar to this:
$xml = simplexml_load_file("location-of-file/feed.xml", null, LIBXML_NOCDATA);
foreach($xml as $key => $item){
// to select an element do something like this
$item->WebSite;
}
however please note that $item is a copy of the array $xml so to manipulate this object you will need to change $item to &$item or you will need to manipulate the actual value as such in your loop
$test = $item->WebSite;
//do something to $test
$xml->$key->WebSite = $test;
if however you are building a new array/xml document you don't need to worry about this issue
Update:
I have tested this on an example feed and you should be able to get this too work with a little editing
header("Content-Type: application/xml; charset=utf-8");
$xml = simplexml_load_file("test.xml", null, LIBXML_NOCDATA);
$values = array("Belgian Waffles","French Toast");
$new = new SimpleXMLElement("<tester></tester>");
foreach($xml->food as $item){
if(in_array($item->name,$values)){
$type = $new->addChild('type');
$movie = $type->addChild('movie');
$movie->addChild('price', $item->price);
}else{
}
}
echo $new->asXML();
basically give this ago, edit it to use the right nodes and build the feed right for you then this should work. the asXML() has a optional parameter you can pass in to write the file
so changing echo $new->asXML(); to $new->asXML("test.xml"); will instead write the file to test.xml instead of outputting the data. if you do save it make sure you get rid of header("Content-Type: application/xml; charset=utf-8");

How to loop through a LinkedIn API response?

I am retrieving connections via LinkedIn's API.
This is a sample of the response. All I want to do is loop through this using PHP, accessing first and last names. Should be simple, but I am stumped. Thanks.
stdClass Object ( [_total] => 420 [values] => Array ( [0] => stdClass
Object ( [apiStandardProfileRequest] => stdClass Object ( [headers] =>
stdClass Object ( [_total] => 1 [values] => Array ( [0] => stdClass
Object ( [name] => x-li-auth-token [value] => name:AnUY ) ) ) [url] =>
http://api.linkedin.com/v1/people/7yw8k_sjqf ) [firstName] => John
[headline] => Finance Manager at ABC. [id] => 7yw8k_sjqf [industry] =>
Insurance [lastName] => Doe [location] => stdClass Object ( [country]
=> stdClass Object ( [code] => us ) [name] => Greater Atlanta Area ) [pictureUrl] =>
http://m.c.lnkd.licdn.com/mpr/mprx/0_73E_PMM3nzAIZLG03bHpPRo3qvCwZL_0DhYpPRogIxiFs7jUYPVRGNRGC1mGegyyqdihoC
[siteStandardProfileRequest] => stdClass Object ( [url] =>
http://www.linkedin.com/profile/view?id=2633833&authType=name&authToken=AnUY&trk=api*a189561*s197687*
) ) [1] => stdClass Object ( [apiStandardProfileRequest] => stdClass
Object ( [headers] => stdClass Object ( [_total] => 1 [values] =>
Array ( [0] => stdClass Object ( [name] => x-li-auth-token [value] =>
name:tpAo ) ) ) [url] => http://api.linkedin.com/v1/people/FW8obZbI8R
) [firstName] => Jeremy [headline] => Owner, BCD [id] => FW8obZbI8R
[industry] => Computer Software [lastName] => Doe [location] =>
stdClass Object ( [country] => stdClass Object ( [code] => us ) [name]
=> Greater Chicago Area ) [pictureUrl] => http://m.c.lnkd.licdn.com/mpr/mprx/0_Lw2NlImcd1e6LmQpL2mIleOMIcf9LeFpbu4IlexHq-R6V2ryWSpZ0HI6LM7rXeaEYf3Gt69H
[siteStandardProfileRequest] => stdClass Object ( [url] =>
http://www.linkedin.com/profile/view?id=6700159&authType=name&authToken=tpAo&trk=api*a189561*s197687*
) ) [2] => stdClass Object ( [apiStandardProfileRequest] => stdClass
Object ( [headers] => stdClass Object ( [_total] => 1 [values] =>
Array ( [0] => stdClass Object ( [name] => x-li-auth-token [value] =>
name:eETp ) ) ) [url] => http://api.linkedin.com/v1/people/a7-dvlvc7K
) [firstName] => Jane [headline] => Sr. Consultant System Integration
at AT&T [id] => a7-dvlvc7K [industry] => Telecommunications [lastName]
=> Doe [location] => stdClass Object ( [country] => stdClass Object ( [code] => us ) [name] => Greater New York City Area ) [pictureUrl] =>
http://m.c.lnkd.licdn.com/mpr/mprx/0_459zxGU8TBpTZjvisicExTgSSvZCsYXisCGExTR-t1ut5V1_N_6JA37p_Z4gJQqIK653RvmW
[siteStandardProfileRequest] => stdClass Object ( [url] =>
http://www.linkedin.com/profile/view?id=119413921&authType=name&authToken=eETp&trk=api*a189561*s197687*
) ) [3] => stdClass Object ( [apiStandardProfileRequest] => stdClass
Object ( [headers] => stdClass Object ( [_total] => 1 [values] =>
Array ( [0] => stdClass Object ( [name] => x-li-auth-token [value] =>
name:HjUG ) ) ) [url] => http://api.linkedin.com/v1/people/aie8Zpk9Gm
) [firstName] => John [headline] => Financial Information/Market Data
Professional [id] => aie8Zpk9Gm [industry] => Financial Services
[lastName] => Smith [location] => stdClass Object ( [country] =>
stdClass Object ( [code] => gb ) [name] => Glasgow, United Kingdom )
[pictureUrl] =>
http://m.c.lnkd.licdn.com/mpr/mprx/0_N0TLAL3BTlHZCVAbTNTAD03RCjngvsAbCIY-JfOxvg4JldxF9L7MShCp_Avp_ROhKS8LNY
[siteStandardProfileRequest] => stdClass Object ( [url] =>
http://www.linkedin.com/profile/view?id=1562499&authType=name&authToken=HjUG&trk=api*a189561*s197687*
) ) [4] => stdClass Object ( [apiStandardProfileRequest] => stdClass
Object ( [headers] => stdClass Object ( [_total] => 1 [values] =>
Array ( [0] => stdClass Object ( [name] => x-li-auth-token [value] =>
name:aZIS ) ) ) [url] => http://api.linkedin.com/v1/people/XTGgSuagWI
) [firstName] => Bob [headline] => CEO, Nycomed US at NMO [id] =>
XTGgSuagWI [industry] => Pharmaceuticals [lastName] => brown
[location] => stdClass Object ( [country] => stdClass Object ( [code]
=> us ) [name] => Greater New York City Area ) [pictureUrl] => http://m.c.lnkd.licdn.com/mpr/mprx/0_UciCehxmKngbM15je_qytdVG-zSbJrcje_92S7a-DlsFcA1m6iCGyXsjl-uwsq3ybXXbrd1L
[siteStandardProfileRequest] => stdClass Object ( [url] =>
What you have as the response is an stdclass which will have all of its properties public.
This is how you iterate it:
$response = $this->getResponseFromLinkedIn();
foreach ($response->values as $value) {
$firstName = $value->firstName;
$lastName = $value->lastName;
// do something with the extracted values!
}

Having problems with recursive array objects (stdClass Object)

I'm hoping someone can help me with my syntax. I think that's the issue. I'm trying to have the loop go through display the chosen object, but it's not working. Here is my code.
stdClass Object
(
[result_count] => 2
[total_count] => 2
[next_offset] => 2
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => da2b0ab5-873b-d9a8-0fcd-5180871e57eb
[module_name] => Project
[name_value_list] => stdClass Object
(
[description] => stdClass Object
(
[name] => description
[value] => soccer is fun!!!
)
[priority] => stdClass Object
(
[name] => priority
[value] => Primary
)
[total_years_played_c] => stdClass Object
(
[name] => total_years_played_c
[value] => 5
)
[sport_c] => stdClass Object
(
[name] => sport_c
[value] => Soccer
)
[high_school_exp_c] => stdClass Object
(
[name] => high_school_exp_c
[value] => 2
)
[varsity_exp_c] => stdClass Object
(
[name] => varsity_exp_c
[value] => default
)
[hand_right_c] => stdClass Object
(
[name] => hand_right_c
[value] => 0
)
[hand_left_c] => stdClass Object
(
[name] => hand_left_c
[value] => 1
)
[foot_right_c] => stdClass Object
(
[name] => foot_right_c
[value] => 1
)
[foot_left_c] => stdClass Object
(
[name] => foot_left_c
[value] => 0
)
[specialskills_c] => stdClass Object
(
[name] => specialskills_c
[value] =>
)
[varsity_starter_c] => stdClass Object
(
[name] => varsity_starter_c
[value] => 0
)
)
)
[1] => stdClass Object
(
[id] => 2e805552-c887-2b2e-a9e5-518087f3f87a
[module_name] => Project
[name_value_list] => stdClass Object
(
[description] => stdClass Object
(
[name] => description
[value] => I bleed for hockey... I will teach your kid to put them on the boards and take no prisoners!
)
[priority] => stdClass Object
(
[name] => priority
[value] => Secondary
)
[total_years_played_c] => stdClass Object
(
[name] => total_years_played_c
[value] => 7
)
[sport_c] => stdClass Object
(
[name] => sport_c
[value] => Hockey
)
[high_school_exp_c] => stdClass Object
(
[name] => high_school_exp_c
[value] => 3
)
[varsity_exp_c] => stdClass Object
(
[name] => varsity_exp_c
[value] => 2
)
[hand_right_c] => stdClass Object
(
[name] => hand_right_c
[value] => 0
)
[hand_left_c] => stdClass Object
(
[name] => hand_left_c
[value] => 1
)
[foot_right_c] => stdClass Object
(
[name] => foot_right_c
[value] => 1
)
[foot_left_c] => stdClass Object
(
[name] => foot_left_c
[value] => 0
)
[specialskills_c] => stdClass Object
(
[name] => specialskills_c
[value] =>
)
[varsity_starter_c] => stdClass Object
(
[name] => varsity_starter_c
[value] => 0
)
)
)
)
[relationship_list] => Array
(
)
)
Here is the PHP that I am using. For now, I'm just wanting to echo the name of sport_c (one should be Hockey, the other should be Soccer)
<?
foreach($response->entry_list as $sports) {
if(!empty($sports->name_value_list->sport_c)) {
$sport_list = $sports->name_value_list->sport_c;
$sport_name = $sport_list->value;
}
}
echo $sport_name;
?>
Put the echo statement within the foreach loop:
foreach($response->entry_list as $sports) {
if(!empty($sports->name_value_list->sport_c)) {
$sport_list = $sports->name_value_list->sport_c;
$sport_name = $sport_list->value;
}
echo $sport_name;
}

PHP - Objects and Arrays - How to access an stdClass object "name" "value" pair inside an array?

I would like to know how to return a value of an object with a name value pair that's inside an array. I've been trying all sorts of methods and frankly I realized I may be way over my head on this. I'd like some assistance trying to get the AirportsInformation_DataExtension value inside the property array.
stdClass Object
(
[OverallStatus] => OK
[RequestID] => 19e41b46-df68-47ba-8858-d728f3a92036
[Results] => stdClass Object
(
[PartnerKey] =>
[ObjectID] =>
[Type] => DataExtensionObject
[Properties] => stdClass Object
(
[Property] => Array
(
[0] => stdClass Object
(
[Name] => CampaignName
[Value] => 20130107_FlightDealsHotelPricePoints
)
[1] => stdClass Object
(
[Name] => StartDate
[Value] => 1/7/2013 12:00:00 AM
)
[2] => stdClass Object
(
[Name] => EndDate
[Value] => 1/15/2013 5:59:59 AM
)
[3] => stdClass Object
(
[Name] => CampaignType
[Value] => FlightDeals
)
[4] => stdClass Object
(
[Name] => LandingPage_ExpireDate
[Value] => 1/15/2013 5:59:59 AM
)
[5] => stdClass Object
(
[Name] => LandingPage_AutoRedirectOnExpire
[Value] => True
)
[6] => stdClass Object
(
[Name] => LandingPage_ExpireTargetURL
[Value] => test
)
[7] => stdClass Object
(
[Name] => BookByDate
[Value] => 1/22/2013 12:00:00 AM
)
[8] => stdClass Object
(
[Name] => TravelStartDate
[Value] =>
)
[9] => stdClass Object
(
[Name] => TravelEndDate
[Value] =>
)
[10] => stdClass Object
(
[Name] => FlightDeals_DataExtension
[Value] => 20130107_DestinationFlightDeals
)
[11] => stdClass Object
(
[Name] => FlightDeals_SortOrder_DataExtension
[Value] => FlightDeals_DestinationSortOrder
)
[12] => stdClass Object
(
[Name] => HotelDeals_DataExtension
[Value] => 20130107_FlightDealsHotelPricePoints
)
[13] => stdClass Object
(
[Name] => HotelDeals_All_DataExtension
[Value] => 20130107_HotelPackageDeals_ALL
)
[14] => stdClass Object
(
[Name] => HotelInformation_DataExtension
[Value] => EmailHotelInformation
)
[15] => stdClass Object
(
[Name] => AirportsInformation_DataExtension
[Value] => Airports
)
[16] => stdClass Object
(
[Name] => RoutesInformation_DataExtension
[Value] => Routes
)
[17] => stdClass Object
(
[Name] => DFP_DataExtension
[Value] => ET_DestinationIframeSrc
)
[18] => stdClass Object
(
[Name] => DeepLinkConnectorURL
[Value] => http://www.somewebsite/BookingConnector.html?mode=run
)
[19] => stdClass Object
(
[Name] => DefaultDestinationScenery
[Value] => LAS
)
[20] => stdClass Object
(
[Name] => DefaultHomeAirportCode
[Value] =>
)
[21] => stdClass Object
(
[Name] => FailSafeHomeAiportCode
[Value] =>
)
[22] => stdClass Object
(
[Name] => DFP_Campaign_Banner
[Value] => True
)
[23] => stdClass Object
(
[Name] => EmailID
[Value] => 44388
)
)
)
)
)
Using a foreach loop I was able to print out all lines with name/value sets
foreach ($results->Results->Properties->Property as $CurrentProp){
print('<br>');
print('Name: '.$CurrentProp->Name. ' Value: '.$CurrentProp->Value.'<br>');
};
Sadly I can't get passed that. I just need to retrieve the value. Thanks in advance.
In order to get the value, you could loop over them and test for the name matching AirportsInformation_DataExtension:
foreach ($results->Results->Properties->Property as $CurrentProp){
if($CurrentProp->Name == 'AirportsInformation_DataExtension')
{
echo 'The value is: ' . $CurrentProp->Value;
}
}
If you would need to be able to get all the values based on their name, it could be useful to turn it in to an associative array, like this
$results->Results->Properties->PropertyArray = array();
foreach($results->Results->Properties->Property as $arrCurrentProperty) {
$results->Results->Properties->PropertyArray[$arrCurrentProperty->Name] = $arrCurrentProperty->Value;
};
Then you can get the values later by indexing them directly, i.e.
echo 'The value is: ' . $results->Results->Properties->PropertyArray['AirportsInformation_DataExtension'];
Use the above solution by MrCode or, simply just use $results->Results->Properties->Property[15]->Value if the index of AirportsInformation_DataExtension is always 15. Since an array is an ordered list, it is very probably that the index does not change unless some of the items are removed/added from the array/
foreach ($results->Results->Properties->Property as $CurrentProp){
$tempArr[$CurrentProp->Name] = $CurrentProp->Value;
}
echo $tempArr['AirportsInformation_DataExtension'];
By this you can access any other key of that object.

Categories