Looping through a SOAP response - php

I have created an application that sends out a SOAP request and gets a response back. This is working fine but I am having trouble looping through all the individual results in order to organize them into an array. Here is the response:
stdClass Object
(
[FITgymlistResult] => stdClass Object
(
[FITgym] => Array
(
[0] => stdClass Object
(
[GYMGUID] => 45124542-bca5-e211-8f4a-00155d007722
[GYMNAME] => Belfast
[Postcode] =>
[Phone] =>
[Email] => belfast#fitspace.co.uk
)
[1] => stdClass Object
(
[GYMGUID] => aece7776-bca5-e211-8f4a-00155d007722
[GYMNAME] => Bournemouth
[Addressline1] => St Paul's Road
[Postcode] =>
[Phone] =>
[Email] => Bournemouth#fitspace.co.uk
)
[2] => stdClass Object
(
[GYMGUID] => 8eaa258e-bca5-e211-8f4a-00155d007722
[GYMNAME] => Bradford
[Postcode] =>
[Phone] =>
[Email] => Bradford#fitspace.co.uk
)
[3] => stdClass Object
(
[GYMGUID] => 935bfdca-bca5-e211-8f4a-00155d007722
[GYMNAME] => Islington
[Postcode] =>
[Phone] =>
[Email] => holloway#fitspace.co.uk
)
[4] => stdClass Object
(
[GYMGUID] => fe104008-bda5-e211-8f4a-00155d007722
[GYMNAME] => Lincoln
[Postcode] =>
[Phone] =>
[Email] => lincoln#fitspace.co.uk
)
[5] => stdClass Object
(
[GYMGUID] => ff3cd339-bda5-e211-8f4a-00155d007722
[GYMNAME] => Mitcham
[Postcode] =>
[Phone] =>
[Email] => Mitcham#fitspace.co.uk
)
[6] => stdClass Object
(
[GYMGUID] => 496e8149-bda5-e211-8f4a-00155d007722
[GYMNAME] => Nottingham
[Postcode] =>
[Phone] =>
[Email] => Nottingham#fitspace.co.uk
)
[7] => stdClass Object
(
[GYMGUID] => 48f26656-bda5-e211-8f4a-00155d007722
[GYMNAME] => Sheffield
[Postcode] =>
[Phone] =>
[Email] => Sheffield#fitspace.co.uk
)
[8] => stdClass Object
(
[GYMGUID] => 1c136968-bda5-e211-8f4a-00155d007722
[GYMNAME] => Woolwich
[Postcode] =>
[Phone] =>
[Email] => Woolwich#fitspace.co.uk
)
)
)
)
What would be the best and quickest way to loop through each individual item in this response?
Thanks

Let say that this object is stored in $ret variable and you want to store individual results in array $data.
$data = array();
foreach($ret->FITgymlistResult->FITgym as $item)
{
$data[] = get_object_vars($item);
}
get_object_vars dumps as array all variables from the object and theirs values
also
in foreach loop you can access, print or store item data like this:
echo $item->email;
$test = $item->GYMNAME;
$data[] = array($item->email, $item->GYMNAME);

Use a foreach loop on the array:
foreach( $response->FITgymlistResult->FITgym as $row )
{
var_dump( $row->GYMGUID, $row->GYMNAME );
}
See also: stdClass object and foreach loops

Related

Loop through a JSON decoded array

I am using randonuser API to generate dummy images. The API returns JSON that I have used json_decode to decode and using print_r, I got the array below:
Array
(
[results] => Array
(
[0] => Array
(
[user] => Array
(
[gender] => male
[name] => Array
(
[title] => mr
[first] => dwight
[last] => evans
)
[location] => Array
(
[street] => 6822 hunters creek dr
[city] => fresno
[state] => vermont
[zip] => 63409
)
[email] => dwight.evans44#example.com
[username] => ticklishostrich542
[password] => ebony
[salt] => 4xuAIjmh
[md5] => 648f472ff152a194c410d774ff9a4b9d
[sha1] => f23cc7ffd2b8980d10de86bccc85068ecf9b7b45
[sha256] => fec06f7df352a06aab9c30af9d7ab9b5b81dc0bd6b7567b59fba1a731dea6bba
[registered] => 1129218274
[dob] => 409533355
[phone] => (797)-563-6160
[cell] => (200)-718-4014
[SSN] => 213-46-5200
[picture] => Array
(
[large] => http://api.randomuser.me/portraits/men/98.jpg
[medium] => http://api.randomuser.me/portraits/med/men/98.jpg
[thumbnail] => http://api.randomuser.me/portraits/thumb/men/98.jpg
)
[version] => 0.4.1
)
[seed] => cf744a697a08f256
)
. .. .....
and so on.
I just need the large key value under parent picture. How do I loop through it using a foreach statement?
Just access it as you normally would:
$data = json_decode('that json string', true);
foreach($data['results'] as $value) {
echo $value['user']['picture']['large'];
}
Use json_decode($var, true), then you'll have an array, and looping will be easier.

parsing xml array response in PHP objects

i am working on a UPS tracking API, i have called the API to track the package, i am getting the right response, but in array format, i am new to json decoding, please tell me how to parse this in PHP, do i have to create multiple objects in PHP? i am attaching the output as well as the PHP code.
PHP Code
if (isset($_POST['af0'])) {
if (preg_match('/^[a-z\d_]{4,80}$/i', $_POST['trackingNumber'])) {
$cleanTrackingNumber = $_POST['trackingNumber'];
$someArray = upsTrack("$cleanTrackingNumber");
echo '<pre>'; print_r($someArray); echo '</pre>';
} else {
echo 'Invalid tracking number... sigh...';
}
}
Output
Array
(
[TRACKRESPONSE] => Array
(
[RESPONSE] => Array
(
[TRANSACTIONREFERENCE] => Array
(
[XPCIVERSION] => 1.0
)
[RESPONSESTATUSCODE] => 1
[RESPONSESTATUSDESCRIPTION] => Success
)
[SHIPMENT] => Array
(
[SHIPPER] => Array
(
[SHIPPERNUMBER] => A6161A
[ADDRESS] => Array
(
[ADDRESSLINE1] => 132 E 43RD ST
[CITY] => NEW YORK
[STATEPROVINCECODE] => NY
[POSTALCODE] => 10017 4019
[COUNTRYCODE] => US
)
)
[SHIPTO] => Array
(
[ADDRESS] => Array
(
[CITY] => TORONTO
[STATEPROVINCECODE] => ON
[POSTALCODE] => M5V3X1
[COUNTRYCODE] => CA
)
)
[SHIPMENTWEIGHT] => Array
(
[UNITOFMEASUREMENT] => Array
(
[CODE] => LBS
)
[WEIGHT] => 3.20
)
[SERVICE] => Array
(
[CODE] => 011
[DESCRIPTION] => STANDARD
)
[REFERENCENUMBER] => Array
(
[CODE] => 13
[VALUE] => A6161AD9HPK
)
[SHIPMENTIDENTIFICATIONNUMBER] => 1ZA6161A6832763249
[PICKUPDATE] => 20140519
[SCHEDULEDDELIVERYDATE] => 20140521
[PACKAGE] => Array
(
[TRACKINGNUMBER] => 1ZA6161A6832763249
[ACTIVITY] => Array
(
[ACTIVITYLOCATION] => Array
(
[ADDRESS] => Array
(
[CITY] => SECAUCUS
[STATEPROVINCECODE] => NJ
[COUNTRYCODE] => US
)
)
[STATUS] => Array
(
[STATUSTYPE] => Array
(
[CODE] => I
[DESCRIPTION] => DEPARTURE SCAN
)
[STATUSCODE] => Array
(
[CODE] => DP
)
)
[DATE] => 20140520
[TIME] => 053000
)
[MESSAGE] => Array
(
[CODE] => 01
[DESCRIPTION] => On Time
)
[PACKAGEWEIGHT] => Array
(
[UNITOFMEASUREMENT] => Array
(
[CODE] => LBS
)
[WEIGHT] => 3.20
)
[REFERENCENUMBER] => Array
(
[CODE] => 19
[VALUE] => MMTD71EUY061A
)
)
)
)
)
If you already have the output in Array format then you don't have anything more to do with json decoding.
You can use $someArray to get any data from the array.
It's up to you if you want to just display it or save it, for example in a database.
What is it you trying to accomplish?

get customer payment ids from CIM

How can I get customer payment method ids from AuthorizeNetCIM_Response object?
There's a method in AuthorizeNetCIM_Response which is supposed to return payment method ids
public function getCustomerPaymentProfileIds()
{
$ids = (array)$this->xml->customerPaymentProfileIdList;
return $ids["numericString"];
}
but calling this function results in error
Notice: Undefined index: numericString
The response object outputs as:
AuthorizeNet_AuthorizeNetCIMResponse Object
(
[xml] => SimpleXMLElement Object
(
[messages] => SimpleXMLElement Object
(
[resultCode] => Ok
[message] => SimpleXMLElement Object
(
[code] => I00001
[text] => Successful.
)
)
[profile] => SimpleXMLElement Object
(
[merchantCustomerId] => 10
[email] => user#nine.com
[customerProfileId] => 25441529
[paymentProfiles] => Array
(
[0] => SimpleXMLElement Object
(
[billTo] => SimpleXMLElement Object
(
[firstName] =>
[lastName] =>
[address] =>
[city] =>
[zip] =>
[country] =>
[phoneNumber] =>
)
[customerPaymentProfileId] => 23298664
[payment] => SimpleXMLElement Object
(
[creditCard] => SimpleXMLElement Object
(
[cardNumber] => XXXX2224
[expirationDate] => XXXX
)
)
)
[1] => SimpleXMLElement Object
(
[customerType] => individual
[billTo] => SimpleXMLElement Object
(
[firstName] => Test
[lastName] => Individual
[company] => SimpleXMLElement Object
(
)
[address] =>
[city] =>
[state] =>
[zip] =>
[country] =>
[phoneNumber] => SimpleXMLElement Object
(
)
[faxNumber] => SimpleXMLElement Object
(
)
)
[customerPaymentProfileId] => 23299421
[payment] => SimpleXMLElement Object
(
[creditCard] => SimpleXMLElement Object
(
[cardNumber] => XXXX0027
[expirationDate] => XXXX
)
)
)
)
)
)
and I supposed to get the array of paymentProfiles as
$response->xml->profile->paymentProfiles;
but it only returning the first element of paymentProfiles not an array.
Since there can be more then one, as per your example, you need to loop through each profile to get that information:
$paymentProfileIds = array();
foreach ($response->xml->profile->paymentProfiles AS $profile) {
$paymentProfileIds[] = (string) $profile->customerPaymentProfileId;
}
print_r($paymentProfileIds);

how to retrive all object array with one value?

This question is a follow up from this Now for my follow up question I also have this object on page:
Array
(
[registrants] => Array
(
[0] => Registrant Object
(
[title] => D C
[link] => **********
[id] => ***************
[updated] => 2013-03-06T12:11:49-05:00
[lastName] => C
[firstName] => D
[email] => *********
[personalInformation] => PersonalInformation Object
(
[cellPhone] =>
[label] =>
[addr1] =>
[addr2] =>
[addr3] =>
[city] =>
[state] =>
[postalCode] =>
[province] =>
[country] =>
[phone] =>
)
[businessInformation] => BusinessInformation Object
(
[fax] =>
[website] =>
[blog] =>
[company] =>
[jobTitle] =>
[department] =>
[label] =>
[addr1] =>
[addr2] =>
[addr3] =>
[city] =>
[state] =>
[postalCode] =>
[province] =>
[country] =>
[phone] =>
)
[customInformation1] => Array
(
)
[customInformation2] => Array
(
)
[registrationStatus] => REGISTERED
[registrationDate] => 2013-03-06T12:11:49-05:00
[guestCount] => 0
[paymentStatus] => NA
[orderAmount] =>
[currencyType] =>
[paymentType] =>
[costs] => Array
(
)
)
[1] => Registrant Object
(
[title] => Test Test
[link] => ****
[id] => *************
[updated] => 2013-03-06T12:47:47-05:00
[lastName] => Test
[firstName] => Test
[email] => ***************
[personalInformation] => PersonalInformation Object
(
[cellPhone] =>
[label] =>
[addr1] =>
[addr2] =>
[addr3] =>
[city] =>
[state] =>
[postalCode] =>
[province] =>
[country] =>
[phone] =>
)
[businessInformation] => BusinessInformation Object
(
[fax] =>
[website] =>
[blog] =>
[company] =>
[jobTitle] =>
[department] =>
[label] =>
[addr1] =>
[addr2] =>
[addr3] =>
[city] =>
[state] =>
[postalCode] =>
[province] =>
[country] =>
[phone] =>
)
[customInformation1] => Array
(
)
[customInformation2] => Array
(
)
[registrationStatus] => REGISTERED
[registrationDate] => 2013-03-06T12:47:47-05:00
[guestCount] => 0
[paymentStatus] => NA
[orderAmount] =>
[currencyType] =>
[paymentType] =>
[costs] => Array
(
)
)
)
[nextLink] =>
)
So following the same theory I am retriving the values like this:
<?php echo $Registrant->lastName; echo $Registrant->firstName; echo $Registrant->email; ?>
but this only retrieves the first lastname and firstname from [0] => Registrant Object not from 1 => Registrant Object how to i get all of the first names and last names?
Thank everyone for there interest and there time.
Kind regards
Chris
To explain your scenario a bit further than other answers.
You have an array of (Registrant) objects here. This is actually an associative array (as all PHP arrays) with indices from 0 - 1.
$registrantObjects[0] // would give first Registrant object
$registrantObjects[1] // would give second Registrant object
You can access them both. But if you want to iterate the array (i.e. going over all elements and do the same for each one), you should use a loop.
PHP has a nice foreach loop for this use case:
foreach ($registrantObjects as $registrant) {
// $registrant is a Registrant object here
echo $registrant->lastName;
}
You could also try this:
foreach ($registrantObjects as $index => $registrant) {
// $registrant is a also Registrant object here
// But we have a variable $index, too. It represents the current 'key'
// We have a normal (numbered) array thus the keys are [0..1]
echo $registrant->lastName;
}
And both loops are equal to this for loop:
for ($i = 0, $len = count($registrantObjects); $i < $len; $i++) {
// $registrantObjects[$i] gives a Registrant object
}
Lets say the object you printed is "$RegistrantObjects"
You can do the following thing :
foreach ($RegistrantObjects as $registrant)
{
echo $registrant->lastName;
}
Within the foreach, the $registrant object will be accessible the same way that you code accesses it.
Try the below code;
<?php
foreach($Registrant as $reg) {
echo $reg->firstname;
echo $reg->lastname;
}
?>

PHP Facebook API - Access Object Array Item

I have an object array (shown at the bottom) and can access the data by doing this:
echo $MyVariable->id;
echo $MyVariable->type;
and so on. However, I can't access the item [count] by doing this:
echo $MyVariable->id;
Anybody know why? Count seems to be used for something else, a count perhaps?
stdClass Object (
[id] => 573948779291487
[from] => stdClass Object (
[category] => Bar
[category_list] => Array (
[0] => stdClass Object (
[id] => 218693881483234
[name] => Pub
)
[1] => stdClass Object (
[id] => 164049010316507
[name] => Gastropub
) )
[name] => The Melbourne Arms
[id] => 533195070033525
)
[name] => The Melbourne Arms Gallery
[location] => The Melbourne Arms
[place] => stdClass Object (
[id] => 533195070033525
[name] => The Melbourne Arms
[location] => stdClass Object (
[street] => Main Street
[city] => Melbourne
[country] => United Kingdom
[zip] => YO42 4QJ
[latitude] => 53.8872441
[longitude] => -0.8554097
) )
[link] => http://www.facebook.com/album.php?fbid=573948779291487&id=533195070033525&aid=1073741825
[cover_photo] => 573948812624817
[count] => 28
[type] => normal
[created_time] => 2013-03-13T22:22:45+0000
[updated_time] => 2013-03-13T22:34:40+0000
[can_upload] =>
[likes] => stdClass Object (
[data] => Array (
[0] => stdClass Object (
[id] => 1361671776
[name] => Lesley Maiden
)
[1] => stdClass Object (
[id] => 518580863
[name] => Diane Maiden
)
[2] => stdClass Object (
[id] => 1254875611
[name] => Vanessa Wilson
)
[3] => stdClass Object (
[id] => 533195070033525
[name] => The Melbourne Arms
)
[4] => stdClass Object (
[id] => 100001664649752
[name] => Bowen Lee
) )
[paging] => stdClass Object (
[next] => https://graph.facebook.com/573948779291487/likes?limit=25&offset=25&__after_id=100001664649752
) ) ) 1
Try handling your data differently
$data = file_get_contents("https://graph.facebook.com/573948779291487");
$data = json_decode($data, true);
echo $data['count'];

Categories