How to interpret results of FedEx API tracking request - php

I need to do some basic Fedex package tracking. I'm using Jeremy Dunn's php-fedex-api-wrapper. My FedEx developer credentials are setup and I am able to authenticate via the FedEx api.
The problem: when I run a basic track request using the api-wrapper code I get back a result that I am unsure how to interpret. It looks like a structure that I should have no trouble parsing but I cannot.
Here are the first few lines of the result code that I see when I var_dump($trackReply)
object(FedEx\TrackService\ComplexType\TrackReply)#66 (2) {
["name":protected]=>
string(10) "TrackReply"
["values":protected]=>
array(4) {
If I cast the result as an array and print_r it looks like this (truncated version):
Array
(
[0] => FedEx\TrackService\ComplexType\TrackReply Object
(
[name:protected] => TrackReply
[values:protected] => Array
(
[HighestSeverity] => SUCCESS
[Notifications] => Array
(
[0] => FedEx\TrackService\ComplexType\Notification Object
(
[name:protected] => Notification
[values:protected] => Array
(
[Severity] => SUCCESS
[Source] => trck
[Code] => 0
[Message] => Request was successfully processed.
[LocalizedMessage] => Request was successfully
processed.
)
)
)
The $trackResult is obviously an object, but I cannot figure out how to access its specific properties. When it is cast as an array, element zero is clearly an object, so I would think I could access the object properties with
I tried $test = $trackResult[0];// which should now be an object, right?
and then
I looked at this StackOverflow post and it seems to be relevant: Convert a PHP object to an associative array
So I tried one of the solutions from the link above:
$array = json_decode(json_encode($nested_object), true);
But, when I print_r($array) I get essentially an empty array like this:
Array
(
)

Related

How to access attributes and values from the output xml result in php

I am integrating a payment gateway into my website via an API provided by a third party, and I DID succeed. When it processes the payment and returns a successful result in the so-called success.php page, I receive the already-output xml from their simplexml_load_string($result), which is printed out in the success.php page like this.
SimpleXMLElement Object
(
[error_code] => 00
[token] => 1182263526325de72aw828369e7aa
[description] => SimpleXMLElement Object
(
)
[transaction_status] => 00
[receiver_email] => myemail#gmail.com
[order_code] => 3212423
[total_amount] => 200
[payment_method] => SimpleXMLElement Object
(
)
[bank_code] => NLAZ
[payment_type] => 1
[order_description] => SimpleXMLElement Object
(
)
[tax_amount] => 0
[discount_amount] => 0
[fee_shipping] => 0
[return_url] => success.php
[cancel_url] => order.php
[buyer_fullname] => eric phuong
[buyer_email] => eric#domain.com
[buyer_mobile] => 012108609
[buyer_address] => abc
[affiliate_code] => SimpleXMLElement Object
(
)
[transaction_id] => 12345
)
What I am trying to do are:
Put the output result above into a variable so that I can get the values. How can I do that in a php and with .php? should I put it like this or how?:
< ?php
$variable = <<< XML
SimpleXMLElement Object
(
[error_code] => 00
....
XML;
?>
Try to get ALL the values such as the buyer_mobile, buyer_email, etc which are 012108609, eric#domain.com, etc.
Can you help?
Thanks,
Eric
P/S: I also tried to read this entry https://stackoverflow.com/questions/25522047/php-xml-to-array-object-with-attributes-and-values and applied it, but no success.
Note: I already know how to parse the xml file to get the array value, but the process here in this situation is quite different that it is already output and echo'ed out in the success.php page. I TRY TO GET THE VALUES, but it seems to be out of my knowledge.
If the question is not clear enough to you, I can update it as per request.
Your can get the SimpleXMLElement from the return of simplexml_load_string($result)
Then you can access the properties and cast them to a string because the SimpleXMLElement has a __toString method.
$variable = simplexml_load_string($result);
$buyerMobile = (string)$variable->buyer_mobile;
$buyerEmail = (string)$variable->buyer_email;
If you want to get the values from that response you might use a regex with an alternation and use \K to reset the starting point of the reported match:
\[buyer_(?:email|mobile|fullname|address)\] =>\s*\K.*
Demo

Getting data from Yelp API

I'm starting to look into Yelp API. When I send a search request I get a data returned in an array $response. So If I output it like this
echo '<pre>';
print_r($response);
echo '</pre>';
I see results in the following format
stdClass Object
(
[message] => stdClass Object
(
[text] => OK
[code] => 0
[version] => 1.1.1
)
[businesses] => Array
(
[0] => stdClass Object
(
[rating_img_url] => http://s3-media2.ak.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png
[country_code] => US
...
)
)
)
So, let's say I want to get the country code, shouldn't I be able to get it with something like?
echo $response['businesses'][0]->country_code;
I'm not getting any results. What am I missing?
echo $response->businesses[0]->country_code;
businesses is a property, not an array element.
Everything below stdClass Object are properties.
Everything below => Array are Array Elements.
Let me guess, $response = json_decode(...); ?
You can tell this function to return associative arrays instead of objects by putting up the second parameter true:
$response = json_decode(..., true);
Then the values would be in:
echo $response['businesses'][0]['country_code'];

SugarCRM REST API, how to get Contacts related to an Account

I am using SugarCRM Pro 6.5.5. I am trying to do some integration into another application, so I need to do some API calls from that other application. I am using the REST API v2. At this point, I need to get a Contact that is related to an Account via the account ID. I have tried both get_relationships() and get_entry_list(), but I can't get either of them to work.
Here's my input for get_relationships():
{"session":"eujfbfsfjgni98m0mivl6jm6r2","module_name":"Accounts","module_id":"c03d0649-0525-2f90-1206-50881e87d7dd","link_field_name":"contacts"}
I have tried many variations of this, and several other options, but nothing did what I wanted. This is the output from above:
stdClass Object
(
[entry_list] => Array
(
[0] => stdClass Object
(
[id] =>
[module_name] => Contacts
[name_value_list] => stdClass Object
(
[deleted] => stdClass Object
(
[name] => deleted
[value] => 0
)
[do_not_call] => stdClass Object
(
[name] => do_not_call
[value] => 0
)
)
)
)
[relationship_list] => Array
(
)
)
I don't see how this information could ever be useful.
Next up, I tried get_entry_list() with a query to select only the Contacts that had the given account ID. Here's my input for that:
{"session":"8mol33e7kdq6girugu30dnt074","module_name":"Contacts","query":"accounts.id = 'c03d0649-0525-2f90-1206-50881e87d7dd'"}
I am using accounts.id because I have read in similar questions, that is correct. I've tried many other things, like: contacts.account_id, account_id, etc. In every case, I get a MySQL error in the log stating the column is invalid.
SugarCRM: how to get all contacts for an account via REST API
This question is relevant, and seems to be the exact same problem. I tried adding a link_name_to_fields_array option to my input, but no matter what value I provide, it results in the following MySQL error in the log:
AND jt11.deleted=0 where (Array) AND contacts.deleted=0: MySQL error 1054: Unknown column 'Array' in 'where clause'
Dafuq?
Thanks.
Thanks to ychaouche, I figured it out.
I was able to use the get_relationships() method after all. I had tried before adding the related_fields to my input, but all that did was result in an empty result set. The answer? Set related_module_query to an empty string... and now the result set is not empty. Yep, my 3+ hours of furious Googleing last night is because of that.
So, here is some working code in case anyone else has this problem:
{
"session": "iov9pg9kvvl60librung1h5fh6",
"module_name": "Accounts",
"module_id": "c03d0649-0525-2f90-1206-50881e87d7dd",
"link_field_name": "contacts",
"related_module_query": "",
"related_fields": [
"first_name",
"last_name"
],
"deleted": false
}
This got me the following result set:
stdClass Object
(
[entry_list] => Array
(
[0] => stdClass Object
(
[id] =>
[module_name] => Contacts
[name_value_list] => stdClass Object
(
[first_name] => stdClass Object
(
[name] => first_name
[value] => John
)
[last_name] => stdClass Object
(
[name] => last_name
[value] => Smith
)
)
)
)
[relationship_list] => Array
(
)
)
You can of course specify different fields in related_fields and get different pieces of data.
Take a look at sugarcrm/tests/service/RESTAPI4Test.php. It's got plenty of examples on how to use the REST API. It's a common practice to look at test code for code examples on how to use a library/framework/API. Tests are in this sense a very good documentation.
Hope that helps.

What kind of API response does this script produce?

I am experimenting with differnt API's of websites right now because I am building my own API for site, On bebo.com through there API they have a php cient which passes a key and secret that the owner of the app has. you then have a client library with a bunch of methods/functions you can call, all of the methods work like this:
public function score_getHigh($uid='', $name='') {
return $this->execute('score.getHigh', array('member_id' => $uid, 'name' => $name));
}
You can see they all just pass in a name of a function and put the params into an array and pass it through the execute(METHOD-NAME, METHOD PARAMS) function. This function then runs code like this
//execute function
//flatten array
foreach ($params as $k => $v) {
if (is_array($v)) {
$params[$k] = implode(',', $v);
}
}
To make a list of all the functions and params to run, it then POST or GET this to the API page with CURL and this is the result that comes back below in my browser if I visit the page myself in a browser instead of letting curl post it then I view the page source of the web browser it shows this array just how I posted it in the browser,
Array
(
[error_code] => 102
[error_msg] => Session key invalid or no longer valid
[request_args] => Array
(
[0] => Array
(
[key] => v
[value] => 1.0
)
[1] => Array
(
[key] => api_key
[value] => Qnw1Moc22Y9m3XY5zUZohbxiwfkURaPJpN3m
)
[2] => Array
(
[key] => method
[value] => friends.get
)
[3] => Array
(
[key] => call_id
[value] => 1262417906.33
)
[4] => Array
(
[key] => sig
[value] => 18b8592f383a5f0abc332745284a0e99
)
)
)
So finally the question here, What kind of response is this, it's not JSON and I don't think it is XML, what would this be called and the script that is trying to get this result with CURL, how can it process this back into something to work with?
From your PHP script, you should be able to do something like this:
$foo = $bebo->score_getHigh(...);
echo $foo['error_code']; // Should output 102 in this case
The response that you posted looks like the output of PHP's serialize function. That would make sense if you use Bebo's PHP client to make a request and then printed the resulting object. Bebo's service is actually returning XML to you when you make your request, as demonstrated by the example below.
$ curl http://apps.bebo.com/restserver.php?\
v=1.0&api_key=Qnw1Moc22Y9m3XY5zUZohbxiwfkURaPJpN3m\
&method=friends.get&call_id=1262417906.33&\
sig=18b8592f383a5f0abc332745284a0e99
<error_response>
<error_code>102</error_code>
<error_msg>Session key invalid or no longer valid</error_msg>
<request_args list="true">
<arg>
<key>v</key>
<value>1.0</value>
</arg>
<arg>
<key>api_key</key>
<value>Qnw1Moc22Y9m3XY5zUZohbxiwfkURaPJpN3m</value>
</arg>
<arg>
<key>method</key>
<value>friends.get</value>
</arg>
<arg>
<key>call_id</key>
<value>1262417906.33</value>
</arg>
<arg>
<key>sig</key>
<value>18b8592f383a5f0abc332745284a0e99</value>
</arg>
</request_args>
</error_response>

Parsing PHP array which has an abject inside

I've got a response form the solr query in php. below is the response form apache solr in drupal6, i need to access the id field inside the Apache_Solr_Document, can some bosy help me with this.
i was able to print this using print_r($result);
Array ( [type] => Bookmarks [node] => Apache_Solr_Document Object ( [_documentBoost:protected] => [_fields:protected] => Array ( [id] => b17692e4ad53/node/274 )))
if i do print_r($result[node]); i am getting
Apache_Solr_Document Object ( [_documentBoost:protected] => [_fields:protected] => Array ( [id] => b17692e4ad53/node/274 ))
from here i can't figure out how to access the id.
Have you tried reading through IBM's article on Solr? The end of the article deals specifically with PHP.
Edit: After finding a source class to read through, it looks like you can do:
$result['node']->getField("id");
or using the magic __get:
$result['node']->id;
You can try using the {} brackets if the object name is not a valid object property name (like SimpleXML stuff).
$object->{'strang&$*#nmame'}->value;

Categories