getting private values in object - php

I have this data generate, but how do I get the values for the data such as the
User ID -> [id] => 1429517
message -> [data:ProtocolNode:private] => get my message
Array
(
[0] => ProtocolNode Object
(
[tag:ProtocolNode:private] => message
[attributeHash:ProtocolNode:private] => Array
(
[id] => 1429517
[offline] => 0
[type] => text
[t] => 14722889
)
[children:ProtocolNode:private] => Array
(
[0] => ProtocolNode Object
(
[tag:ProtocolNode:private] => body
[attributeHash:ProtocolNode:private] => Array
(
)
[children:ProtocolNode:private] =>
[data:ProtocolNode:private] => get my message
)
)
[data:ProtocolNode:private] =>
)
)
I have tried using this:
foreach($msgs as $msg) {
echo 'test: ' . $msg->tag ;
}
but I get the error:
Fatal error: Cannot access private property ProtocolNode::$tag

$reflection = new ReflectionClass($msg);
$prop = $reflection->getProperty("tag");
$prop->setAccessible(true);
$tag = $prop->getValue();
But this is little bit hack way, if you has ProtocolNode in your sorce then it is better to add getter "getTag", if ProtocolNode is some vendor lib class, then you can extend ProtocolNode to MyProtocolNode and add getter

Related

Display Part of the output in Browser

Background :
I am calling Soap Url & getting messages in browser based on ShipmentNumber
Case 1 : When i ran code in browsers , For some ShipmentNumber i got below result as output:
stdClass Object
(
[Transaction] => stdClass Object
(
[Reference1] => 10000128254545
[Reference2] =>
[Reference3] =>
[Reference4] =>
[Reference5] =>
)
[Notifications] => stdClass Object
(
)
[HasErrors] => 1
[ProcessedShipmentHolds] => stdClass Object
(
[ProcessedShipmentHold] => stdClass Object
(
[ID] => 42863418581
[HasErrors] => 1
[Notifications] => stdClass Object
(
[Notification] => stdClass Object
(
[Code] => ERR65
[Message] => Hold of the same type already exists
)
)
)
)
)
Case 2 : For some other ShipmentNumber , i got below result :
stdClass Object
(
[Transaction] => stdClass Object
(
[Reference1] => 10000128254545
[Reference2] =>
[Reference3] =>
[Reference4] =>
[Reference5] =>
)
[Notifications] => stdClass Object
(
)
[HasErrors] =>
[ProcessedShipmentHolds] => stdClass Object
(
[ProcessedShipmentHold] => stdClass Object
(
[ID] => 42863421156
[HasErrors] =>
[Notifications] => stdClass Object
(
)
)
)
)
Requirement :
But I want to display only [Message] Value in Browser. Means, in Case 1, i want to display "Hold of the same type already exists" , in Case 2 , i want to display "No Message" in browser....
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$soapClient = new SoapClient('https://ws.aramex.net/ShippingAPI.V2/Shipping/Service_1_0.svc?wsdl');
$params = array(
'ClientInfo' => array('AccountNumber' => 'IN',
'Version' => 'v1.0'
),
'Transaction' => array(
'Reference1' => '',
'Reference5' => ''
),
'ShipmentHolds'=> array(
'ShipmentHoldDetails' => array(
'ShipmentNumber' =>'42863421156',
'Comment' =>'test Order'
)
)
);
try {
$auth_call = $soapClient->HoldShipments($params);
echo '<pre>';
print_r($auth_call);
die();
} catch (SoapFault $fault) {
die('Error : ' . $fault->faultstring);
}
?>
try this code. You need to access the right field to display the message.
your message is in:
$auth_call->ProcessedShipmentHolds->ProcessedShipmentHold->Notifications->Notification->Message
But based on your second question sometimes you don't have it there so go for something like:
if(isset($auth_call->ProcessedShipmentHolds->ProcessedShipmentHold->Notifications->Notification->Message)){
echo $auth_call->ProcessedShipmentHolds->ProcessedShipmentHold->Notifications->Notification->Message;
}
else{
echo "No Message";
}
You can make it more "elegant" and use ternary operators like
isset($auth_call->ProcessedShipmentHolds->ProcessedShipmentHold->Notifications->Notification->Message)?$Message=$auth_call->ProcessedShipmentHolds->ProcessedShipmentHold->Notifications->Notification->Message:$Message='No Message';
echo $Message;

Issue accessing objects within objects in a file with XML format

I have a XML format file which has objects inside objects. This is how I get values of first object, which works fine for me:
$soapclient = new SoapClient('http://anywb/book.asmx?WSDL');
$params = array('ISBN' => "1111");
$response = $soapclient->GetBookByISBN($params);
//This will give me the value "Success"
$result = $response->GetBookByISBNResult->ResponseText;
Now my question is how to access the object which is inside object. For example, how to get "BookID" which is 4 and how to get value of "Type" which is 1?
Any suggestion would be appreciated. Here is the object:
stdClass Object
(
[GetBookByISBN] => stdClass Object
(
[ResponseText] => Success
[SearchResult] => stdClass Object
(
[Search] => Array
(
[0] => stdClass Object
(
[Date] => 2015-10-20
[BookID] => 4
[Discription] => stdClass Object
(
[Type] => 1
)
[Probability] => stdClass Object
(
[Kids] =>
[Adult] => 00
)
)
[1] => stdClass Object
(
[Date] => 2016-11-15
[BookID] => 5
[Discription] => stdClass Object
(
[Type] => 2
)
[Probability] => stdClass Object
(
[Kids] =>
[Adult] => 00
)
)
))))
You do it like this:
$response->GetBookByISBN->SearchResult->Search[0]->BookID;
$response->GetBookByISBN->SearchResult->Search[0]->Discription->Type;
If you would like to have an array containing all book ids you would have something like this:
$BookIDs = array();
foreach($response->GetBookByISBN->SearchResult->Search as $key => $value) {
$BookIDs[$key] = $value->BookID;
}
Or if you would like to be able to modify the values in $response using the $BookIDs (as an example) iot would look like this:
$BookIDs = array();
foreach($response->GetBookByISBN->SearchResult->Search as $key => $value) {
$BookIDs[$key] = &$value->BookID;
}

Returning an array within multiple objects and arrays

I can't seem to figure out the correct syntax to print:
[Agriculture] => AGR
[Animals] => AN
[Arts and Humanities] => ART
within the $TOPIC object.
What makes this a bit more confusing is that $TOPIC is in an object that can be called in a different order than this example. So instead of [3] => stdClass Object, it could be [4] => stdClass Object, (or any number).
Some sample foreach syntax is included below; it doesn't work though.
stdClass Object
(
[123456] => stdClass Object
(
[required_actions] => Array
(
[1] => stdClass Object
(
[maxlength] =>
[value] => $MESSAGE
[options_hash] =>
)
[2] => stdClass Object
(
[maxlength] =>
[value] => $NAME_PREFIX
[options_hash] => stdClass Object
(
[Ms.] => Ms.
[Mrs.] => Mrs.
[Mr.] => Mr.
)
)
[3] => stdClass Object
(
[maxlength] =>
[value] => $TOPIC
[options_hash] => stdClass Object
(
[Agriculture] => AGR
[Animals] => AN
[Arts and Humanities] => ART
)
)
)
)
)
foreach ($json->123456->required_actions as $info) {
echo $info->value => $TOPIC->options_hash;
}
The solution is to search for the parent object by value, and then request the child object's array. Here is what I used:
foreach ($json->123456->required_actions as $info) {
if ('$TOPIC' == $info->value) {
print_r($info->options_hash);
}
}

HubSpot api json decode

I am trying to parse some data out of the Hubspot API response. The response looks like this json_decoded:
stdClass Object(
[addedAt] => 1411052909604
[vid] => 24
[canonical-vid] => 24
[merged-vids] => Array
(
)
[portal-id] => XXXXX
[is-contact] => 1
[profile-token] => AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[profile-url] => https://app.hubspot.com/contacts/XXXXX/lists/public/contact/_AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[properties] => stdClass Object
(
[lastname] => stdClass Object
(
[value] => testtt
)
[firstname] => stdClass Object
(
[value] => test
)
[lastmodifieddate] => stdClass Object
(
[value] => 1411052906670
)
)
[form-submissions] => Array
(
[0] => stdClass Object
(
[conversion-id] => 85d24dd2-9ee9-4d47-b8f3-3035acbd8f3b
[timestamp] => 1411052834097
[form-id] => fb16efd9-23cc-4511-889c-204fc8b41dba
[portal-id] => 401824
[page-url] => http://wbl-1.hs-sites.com/test
[canonical-url] => http://wbl-1.hs-sites.com/test
[content-type] => landing-page
[page-title] => test
[page-id] => 1570433242
[title] => Default Form (Sample)
[first-visit-url] => http://wbl-1.hs-sites.com/test
[first-visit-timestamp] => 1411052722970
[meta-data] => Array
(
)
)
)
[list-memberships] => Array
(
)
[identity-profiles] => Array
(
[0] => stdClass Object
(
[vid] => 24
[identities] => Array
(
[0] => stdClass Object
(
[type] => EMAIL
[value] => test#user.com
[timestamp] => 1411052834097
)
[1] => stdClass Object
(
[type] => LEAD_GUID
[value] => 0b6acf21-6cee-4c7b-b664-e65c11ee2d8e
[timestamp] => 1411052834201
)
)
)
)
[merge-audits] => Array
(
)
)
I'm looking specifically to try to dig out an email inside the indentities-profile area.
I've tried to do the following:
echo $results->contacts[0]->identity-profiles;
But it just gives me a value of 0
Then I try to go further into the array by doing:
echo $results->contacts[0]->identity-profiles[0];
But at that point - I get a parse error:
Parse error: syntax error, unexpected '['
What am I doing wrong? And how can I dig all the way down to
identity-profiles[0]->identities->[0]->value
which should equal: test#user.com
What am I missing?
As mentioned in the comment I would suggest to decode the JSON to an associative array by passing true as second parameter to json_decode. Example: json_decode($data, true) Than you could access your identity profiles by:
$results['contacts'][0]['identitiy-profiles']
If you still want to get the results as an object you have to access the properties the following way because they contain a -:
$results->contacts[0]->{'identity-profiles'}

Filtering an Array

I'm working on a website that displays properties. I have a huge XML feed in which I put into an array.
function search_results($department = '', $post_code = '', $min_price = '', $max_price = '', $type = '', $bedrooms = '') {
foreach($this->xml->property as $property) {
if($property->department == $department) {
$this->properties[] = $property;
}
}
$this->filter_results();
}
this first filters the XML based on the department such as 'For Sale' or 'To Rent'. I want to now be able to search this array based on the variables I pass though the function. For example:
if($this->properties->regionID == $post_code) {
$this->properties[] = $property;
}
But this doesn't work. This will be in a class called Results. How will I go about searching the array. I had a look at using array_filter(); but I couldn't get it working it would return Array() when I print_r it.
Does anyone know how to search/filter an array?
This is what the array looks like when I print_r:
Array ( [0] => SimpleXMLElement Object ( [propertyID] => 1 [branchID] => 1 [clientName] => Name [branchName] => Branch [department] => Lettings [referenceNumber] => 1 [addressName] => 4 [addressNumber] => SimpleXMLElement Object ( ) [addressStreet] => address [address2] => address2 [address3] => address3 [address4] => SimpleXMLElement Object ( ) [addressPostcode] => postcode [country] => postcode [displayAddress] => address [propertyBedrooms] => 1 [propertyBathrooms] => 1 [propertyEnsuites] => 0 [propertyReceptionRooms] => 1 [propertyKitchens] => 1 [displayPropertyType] => Flat/Apartment [propertyType] => 2 [propertyStyle] => 16 [propertyAge] => 0 [floorArea] => 0.00 [floorAreaUnits] => sq ft [propertyFeature1] => Converted School House [propertyFeature2] => City Centre Location [propertyFeature3] => Modern Fittings [propertyFeature4] => SimpleXMLElement Object ( ) [propertyFeature5] => SimpleXMLElement Object ( ) [propertyFeature6] => SimpleXMLElement Object ( ) [propertyFeature7] => SimpleXMLElement Object ( ) [propertyFeature8] => SimpleXMLElement Object ( ) [propertyFeature9] => SimpleXMLElement Object ( ) [propertyFeature10] => SimpleXMLElement Object ( ) [rent] => 525 [rentFrequency] => 1 [toLetPOA] => 0 [studentProperty] => SimpleXMLElement Object ( ) [availability] => 2 [mainSummary] => summary. [fullDescription] => SimpleXMLElement Object ( ) [dateLastModified] => 2014-05-02 [featuredProperty] => 0 [regionID] => 27 [latitude] => SimpleXMLElement Object ( ) [longitude] => SimpleXMLElement Object ( ) [flags] => SimpleXMLElement Object ( ) [images] => SimpleXMLElement Object ( [image] => Array ( [0] => image [1] => image [2] => image [3] => image [4] => image ) ))
Does anyone know how I would be able to search this query for example
$post_code = 43; $min_price = 300; $max_price = 500
and so on.
Looking at the code, the logic seems good to me.
There is something though that might cause the issue.
On line 4 of your code, you have:
$this->properties[] = $property;
It means that you have an array of properties.
But then, you are trying to access the object directly:
if($this->properties->regionID == $post_code) {
$this->properties[] = $property;
}
This looks strange to me. You are trying to see if an abject has the property you want, and then save the same object as it is an array.
So I guess, once you have built your $this->properties array, you just have to loop through it and apply your filters to return an array of properties with just what you are looking for:
foreach($this->properties as $singleProperty)
{
if($singleProperty->regionID == $post_code)
{
$this->foundProperties[] = $singleProperty;
}
}

Categories