Display Part of the output in Browser - php

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;

Related

Stripe success page trying to get details from session_id

I'm trying to just retrieve the details from a successful transaction from Stripe in php. The official documentation tells me to install and use Slim, something I can't do in my current situation.
So far I've got the following code which throws me a report of some kind, but i don't fully understand what i'm looking at and I'm not sure where to go from here, or even if I'm getting the right information back in the first place!
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
require_once('../../stripe/init.php');
$stripe = new \Stripe\StripeClient(
'[my test key]'
);
$stripe->checkout->sessions->retrieve(
$_GET['session_id'],
[]
);
echo "<pre>";
print_r($stripe);
echo "</pre>";
?>
this give me the following:
Stripe\Service\Checkout\SessionService Object
(
[client:protected] => Stripe\StripeClient Object
(
[coreServiceFactory:Stripe\StripeClient:private] => Stripe\Service\CoreServiceFactory Object
(
[client:Stripe\Service\AbstractServiceFactory:private] => Stripe\StripeClient Object
*RECURSION*
[services:Stripe\Service\AbstractServiceFactory:private] => Array
(
[checkout] => Stripe\Service\Checkout\CheckoutServiceFactory Object
(
[client:Stripe\Service\AbstractServiceFactory:private] => Stripe\StripeClient Object
*RECURSION*
[services:Stripe\Service\AbstractServiceFactory:private] => Array
(
[sessions] => Stripe\Service\Checkout\SessionService Object
*RECURSION*
)
)
)
)
[config:Stripe\BaseStripeClient:private] => Array
(
[api_key] => [my_api_key]
[client_id] =>
[stripe_account] =>
[stripe_version] =>
[api_base] => https://api.stripe.com
[connect_base] => https://connect.stripe.com
[files_base] => https://files.stripe.com
)
[defaultOpts:Stripe\BaseStripeClient:private] => Stripe\Util\RequestOptions Object
(
[apiKey] =>
[headers] => Array
(
[Stripe-Account] =>
[Stripe-Version] =>
)
[apiBase] =>
)
)
[streamingClient:protected] => Stripe\StripeClient Object
(
[coreServiceFactory:Stripe\StripeClient:private] => Stripe\Service\CoreServiceFactory Object
(
[client:Stripe\Service\AbstractServiceFactory:private] => Stripe\StripeClient Object
*RECURSION*
[services:Stripe\Service\AbstractServiceFactory:private] => Array
(
[checkout] => Stripe\Service\Checkout\CheckoutServiceFactory Object
(
[client:Stripe\Service\AbstractServiceFactory:private] => Stripe\StripeClient Object
*RECURSION*
[services:Stripe\Service\AbstractServiceFactory:private] => Array
(
[sessions] => Stripe\Service\Checkout\SessionService Object
*RECURSION*
)
)
)
)
[config:Stripe\BaseStripeClient:private] => Array
(
[api_key] => [my_api_key]
[client_id] =>
[stripe_account] =>
[stripe_version] =>
[api_base] => https://api.stripe.com
[connect_base] => https://connect.stripe.com
[files_base] => https://files.stripe.com
)
[defaultOpts:Stripe\BaseStripeClient:private] => Stripe\Util\RequestOptions Object
(
[apiKey] =>
[headers] => Array
(
[Stripe-Account] =>
[Stripe-Version] =>
)
[apiBase] =>
)
)
)
I worked it out based on the code I originally had. Here it is so other people don't have to go through the frustration I had:
require_once('../../stripe/init.php');
$stripe = new \Stripe\StripeClient(
'{{my api key}}'
);
$line_items = $stripe->checkout->sessions->retrieve(
$_GET['session_id'],
[]
);
echo "<pre>";
print_r($line_items);
echo "</pre>";
I just needed to put the retrieve into an object, then I can output that object in the form of a json. Or something like that anyway. You'll know what I mean when you try it.
There you go
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_Your_Test_Key');
$session = \Stripe\Checkout\Session::retrieve($_GET['session_id']);
$customer = \Stripe\Customer::retrieve($session->customer);
print_r($customer);

assigning SOAP value to a variable [duplicate]

This question already has an answer here:
Accessing value in PHP SoapClient response element with attribute
(1 answer)
Closed 1 year ago.
I am trying to extract, for example, ServiceType from this SOAP call. In the below example, it is showing FTTN
if (VocusDevController::login()) {
$client = new SoapClient('wsdl/VocusSchemas/WholesaleServiceManagement.wsdl', array(
'trace' => 1,
'exception' => true
));
try {
$response = $client->Get(array(
"AccessKey" => "SOMEKEY",
"ProductID" => "FIBRE",
"Scope" => "QUALIFY",
"Parameters" => array('Param' => array('_' => 'LOC000023125730', 'id' => 'DirectoryID'))
));
echo '<pre>';
print_r($response);
echo '</pre>';
echo '<hr>';
// $output = $response->Parameters->Param->ServiceType; // This is where I am trying to extract ServiceType
} catch (Exception $exception) {
echo 'Exception Thrown: ' . $exception->getMessage();
}
}
This is the output I get from the above call
stdClass Object
(
[TransactionID] => 17A935CDC7E1906
[ResponseType] => SYNC
[Parameters] => stdClass Object
(
[Param] => Array
(
[0] => stdClass Object
(
[_] => PASS
[id] => Result
)
[1] => stdClass Object
(
[_] => FTTN
[id] => ServiceType
)
[2] => stdClass Object
(
[_] => 13
[id] => ServiceClass
)
[3] => stdClass Object
(
[_] => Type 1
[id] => ConnectionType
)
[4] => stdClass Object
(
[_] => 20210910
[id] => CopperDisconnectionDate
)
[5] => stdClass Object
(
[_] => Urban
[id] => Zone
)
[6] => stdClass Object
(
[_] => FALSE
[id] => DevelopmentCharge
)
[7] => stdClass Object
(
[_] => CSA200000000332
[id] => CSA
)
[8] => stdClass Object
(
[_] => Auto-Assigned
[id] => CVCID
)
[9] => stdClass Object
(
[_] => CPI300009724591N/ALine In Use13N/AFALSE5Mbps,10Mbps,20Mbps33-3577-82TRUEFALSE
[id] => CopperPairRecord
)
[10] => stdClass Object
(
[_] => LOC000023125730NBNLOT: 18 1 CHILDS CCT BELROSE NSW
[id] => BroadbandAddressRecord
)
)
)
)
I am trying to extract ServiceType with $output = $response->Parameters->Param->ServiceType;, However, I just get this error:-
ErrorException
Attempt to read property "ServiceType" on array
So, I am obviously trying to extract it incorrectly. Can someone shed some light on what I am doing wrong? Thanks
Thanks to #miken32 's respose here Accessing value in PHP SoapClient response element with attribute
I have used the following to pull ServiceType succesfully.
$data = $response->Parameters->Param[1]->_;
dd($data);

Mailchimp JSON using PHP foreach with multiple keys

Im using Mailchimp API and getting their user activity. From the array, I have to GET the campaign_id = 1ce6d076f4 first and then match the Campaign ID to all arrays that has action = click so that I can get value of url. Here's the JSON array of the mailchimp.
Array
(
[0] => stdClass Object
(
[action] => click
[timestamp] => 2019-11-12T03:08:40+00:00
[url] => https://nasis.sb:8890/article/burlington-vermont-market-overview
[campaign_id] => 1ce6d076f4
[title] => Sample Campaign v2
)
[1] => stdClass Object
(
[action] => click
[timestamp] => 2019-11-12T02:54:07+00:00
[url] => https://nasis.sb:8890/property/walgreens-burlington-vermont?lid=*|HTML:LINKID|*
[campaign_id] => 1ce6d076f4
[title] => Sample Campaign v2
)
[2] => stdClass Object
(
[action] => open
[timestamp] => 2019-11-12T02:33:55+00:00
[campaign_id] => 1ce6d076f4
[title] => Sample Campaign v2
)
[3] => stdClass Object
(
[action] => sent
[timestamp] => 2019-11-12T02:33:40+00:00
[type] => regular
[campaign_id] => 1ce6d076f4
[title] => Sample Campaign v2
)
[4] => stdClass Object
(
[action] => open
[timestamp] => 2019-10-31T00:38:02+00:00
[campaign_id] => fbe8dfde89
[title] => Sample Campaign v1
)
[5] => stdClass Object
(
[action] => click
[timestamp] => 2019-10-31T00:15:44+00:00
[url] => https://nasis.sb:8890?lid=*|HTML:LINKID|*
[campaign_id] => fbe8dfde89
[title] => Sample Campaign v1
)
)
I am connecting the mailchimp api via wordpress plugin. And here's my code. However, nothing happen when trying to display the $x
$activityDetails = wp_remote_get( 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members/'.$subscriber_hash.'/activity', $args );
$body = json_decode( wp_remote_retrieve_body( $activityDetails ) );
// echo '<pre>'; print_r($body->activity); echo '</pre>';
foreach ( $body->activity as $act ) {
if ( $act->campaign_id == '1ce6d076f4' ) {
$x = $act->action;
}
}
echo '<p> Campaign ID: ' . $x . '</p>';
UPDATE
It looks like I got it to work...
foreach ( $body->activity as $act ) {
if ( $act->campaign_id == '1ce6d076f4' ) {
if ( $act->action == 'click' ) {
$x[] = $act->url;
}
}
}
echo '<pre>'; print_r($x); echo '</pre>';
// Output
Array
(
[0] => https://www.nasinvestmentsolutions.com/article/burlington-vermont-market-overview
[1] => https://nasis.sb:8890/property/walgreens-burlington-vermont?lid=*|HTML:LINKID|*
)

Converting an nested xAPI JSON output into PHP Array

Array
(
[0] => TinCan\Statement Object
(
[id:protected] => 0a53e06c-64a7-4902-930e-993bb228cd49
[stored:protected] => 2018-02-24T04:21:22.456Z
[authority:protected] => TinCan\Agent Object
(
[objectType:protected] => Agent
[name:protected] => sabarish
[mbox:protected] =>
[mbox_sha1sum:protected] =>
[openid:protected] =>
[account:protected] => TinCan\AgentAccount Object
(
[name:protected] => 248a06f20aa62f
[homePage:protected] => https://sandbox.watershedlrs.com
)
)
[version:protected] => 1.0.0
[attachments:protected] => Array
(
)
[actor:protected] => TinCan\Agent Object
(
[objectType:protected] => Agent
[name:protected] => Akshaya Manikandan
[mbox:protected] => mailto:aksh.m14#gmail.com
[mbox_sha1sum:protected] =>
[openid:protected] =>
[account:protected] =>
)
[verb:protected] => TinCan\Verb Object
(
[id:protected] => http://adlnet.gov/expapi/verbs/skipped
[display:protected] => TinCan\LanguageMap Object
(
[_map:protected] => Array
(
[en] => skipped
)
)
)
[target:protected] => TinCan\Activity Object
(
[objectType:TinCan\Activity:private] => Activity
[id:protected] => https://app.acuizen.com/populate_form/965/1573/4690
[definition:protected] =>
)
[result:protected] =>
[context:protected] => TinCan\Context Object
(
[registration:protected] =>
[instructor:protected] =>
[team:protected] =>
[contextActivities:protected] => TinCan\ContextActivities Object
(
[category:protected] => Array
(
[0] => TinCan\Activity Object
(
[objectType:TinCan\Activity:private] => Activity
[id:protected] => http://acuizen.com/ActivitySkipped
[definition:protected] => TinCan\ActivityDefinition Object
(
[type:protected] => http://id.tincanapi.com/activitytype/Assignment
[name:protected] => TinCan\LanguageMap Object
(
[_map:protected] => Array
(
)
)
[description:protected] => TinCan\LanguageMap Object
(
[_map:protected] => Array
(
)
)
[moreInfo:protected] =>
[extensions:protected] => TinCan\Extensions Object
(
[_map:protected] => Array
(
)
)
[interactionType:protected] =>
[correctResponsesPattern:protected] =>
[choices:protected] =>
[scale:protected] =>
[source:protected] =>
[target:protected] =>
[steps:protected] =>
)
)
)
[parent:protected] => Array
(
)
[grouping:protected] => Array
(
)
[other:protected] => Array
(
)
)
[revision:protected] =>
[platform:protected] =>
[language:protected] =>
[statement:protected] =>
[extensions:protected] => TinCan\Extensions Object
(
[_map:protected] => Array
(
)
)
)
[timestamp:protected] => 2018-02-24T04:21:22.456Z
)
...
+ 100 more like these.
I get this output after running my php code to retrive all information from the LRS. How to change this into PHP ARRAY?
You need to learn how to use their API documentation, There is no simple "get all the data" that I can see because everything is wrapped in a class with private/protected properties....
Using your provided sample, here is an example of how to "get the actors"
$Statements = getAllActivity()->content->getStatements();
foreach( $Statements as $Statement )
{
print_r( $Statement->getActor() );
print_r( $Statement->getActor()->getName() );
print_r( $Statement->getActor()->getMbox() );
}
Review the script \src\StatementBase.php, this is where I found the getAcator() method
Without your php code I can only assume you are using json_decode($response) instead of json_decode($response, true). Second parameter in this function decides whether to decode into a class as you have or simple array.

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'}

Categories