Accesing JSON response data via PHP - php

I'm using the Paypal API PHP REST SDK. I got a response that looks like in JSON format but i'm unable to access the properties inside the object and array.
How do I access the "state" properties from this JSON response via PHP? The response is being wrapped by the object of Paypal\Api\Payment type. foreach looping returns NULL
var_dump($response) looks like below:
object(PayPal\Api\Payment)#8 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(8) {
["id"]=>
string(28) "PAY-8JC052XXXXKKZMQNQ"
["create_time"]=>
string(20) "2013-12-19T10:19:34Z"
["update_time"]=>
string(20) "2013-12-19T10:20:38Z"
["state"]=>
string(8) "approved"
["intent"]=>
string(4) "sale"
["payer"]=>
object(PayPal\Api\Payer)#33 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(2) {
["payment_method"]=>
string(6) "paypal"
["payer_info"]=>
object(PayPal\Api\PayerInfo)#30 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(5) {
["email"]=>
string(11) "some#email.com"
["first_name"]=>
string(6) "fname"
["last_name"]=>
string(5) "lname"
["payer_id"]=>
string(13) "UAGGF3392CUTG"
["shipping_address"]=>
object(PayPal\Api\Address)#31 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(5) {
["line1"]=>
string(26) "Address"
["city"]=>
string(13) "City"
["state"]=>
string(8) "State"
["postal_code"]=>
string(5) "000000"
["country_code"]=>
string(2) "US"
}
}
}
}
}
}
["transactions"]=>
array(1) {
[0]=>
object(PayPal\Api\Transaction)#34 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(4) {
["amount"]=>
object(PayPal\Api\Amount)#35 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["total"]=>
string(4) "1.00"
["currency"]=>
string(3) "USD"
["details"]=>
object(PayPal\Api\Details)#36 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["subtotal"]=>
string(4) "1.00"
}
}
}
}
["description"]=>
string(33) "Item name: 1"
["item_list"]=>
object(PayPal\Api\ItemList)#37 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["items"]=>
array(1) {
[0]=>
object(PayPal\Api\Item)#38 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(4) {
["name"]=>
string(20) "Item name"
["price"]=>
string(4) "1.00"
["currency"]=>
string(3) "USD"
["quantity"]=>
string(1) "1"
}
}
}
}
}
["related_resources"]=>
array(1) {
[0]=>
object(PayPal\Api\RelatedResources)#40 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["sale"]=>
object(PayPal\Api\Sale)#42 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(7) {
["id"]=>
string(17) "5DH04XXX63X"
["create_time"]=>
string(20) "2013-12-19T10:19:34Z"
["update_time"]=>
string(20) "2013-12-19T10:20:38Z"
["state"]=>
string(9) "completed"
["amount"]=>
object(PayPal\Api\Amount)#44 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(2) {
["total"]=>
string(4) "1.00"
["currency"]=>
string(3) "USD"
}
}
["parent_payment"]=>
string(28) "PAY-8JC05XXXXKZMQNQ"
["links"]=>
array(3) {
[0]=>
object(PayPal\Api\Links)#46 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(65) "https://api.sandbox.paypal.com/v1/payments/sale/5DHXX91763X"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
[1]=>
object(PayPal\Api\Links)#47 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(72) "https://api.sandbox.paypal.com/v1/payments/sale/5DHXXA691763X/refund"
["rel"]=>
string(6) "refund"
["method"]=>
string(4) "POST"
}
}
[2]=>
object(PayPal\Api\Links)#48 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8JC052914XX1034SKKZMQNQ"
["rel"]=>
string(14) "parent_payment"
["method"]=>
string(3) "GET"
}
}
}
}
}
}
}
}
}
}
}
["links"]=>
array(1) {
[0]=>
object(PayPal\Api\Links)#49 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8JC0XX914D601034SKKZMQNQ"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
}
}
}
I tried json_decode($response) but it returned NULL so i assumed that this is already the correct JSON format.
I tried echo $response->id and it returns blank
I've also tried multiple variations of foreach ($response->id as $value) { var_dump($value); } which also returns nothing
Help!

If you use only json_decode($result) it will not convert entire objects into an array.
So simply use
$result=json_decode($result, true, 512);
It will convert all the objects into associative array recursively.
Try it. It works for me.

It turns out that this is not a standard JSON format. For some reason the Paypal API SDK return it in their own "json" format through this line
$ret->fromJson($json);
return $ret;
I just skipped that and return $json instead and it gives me the format that i can put into json_decode for further processing.
return $json;
That took me 1 full freaking day! Pff...

You can also use
$response->toJSON();
then you can use
$result = json_decode($response);
echo ($result->state);

Convert json to a string then store the content in an array (decode it with json_decode).

Related

How can I extract values from the array of JSON data that this var_dump gives [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
How can I extract title and content from the array of JSON data that gives the following when I var_dump(WpApi::posts());
array(3) { ["results"]=> array(1)
{ [0]=> array(23)
{ ["id"]=> int(8)
["date"]=> string(19) "2017-01-31T07:08:21"
["date_gmt"]=> string(19) "2017-01-31T07:08:21"
["guid"]=> array(1) { ["rendered"]=> string(34) "http://idybrand.com/wordpress/?p=8" }
["modified"]=> string(19) "2017-01-31T07:08:21"
["modified_gmt"]=> string(19) "2017-01-31T07:08:21"
["slug"]=> string(34) "february-is-just-around-the-corner"
["type"]=> string(4) "post"
["link"]=> string(76) "http://idybrand.com/wordpress/2017/01/31/february-is-just-around-the-corner/"
["title"]=> array(1) { ["rendered"]=> string(34) "February is just around the corner" }
["content"]=> array(2) { ["rendered"]=> string(39) "
Appreciate this wonderful month
" ["protected"]=> bool(false) }
["excerpt"]=> array(2) { ["rendered"]=> string(39) "
Appreciate this wonderful month
" ["protected"]=> bool(false) }
["author"]=> int(1)
["featured_media"]=> int(0)
["comment_status"]=> string(4) "open"
["ping_status"]=> string(4) "open"
["sticky"]=> bool(false)
["template"]=> string(0) ""
["format"]=> string(8) "standard"
["meta"]=> array(0) { }
["categories"]=> array(1) { [0]=> int(1) }
["tags"]=> array(0) { }
["_links"]=> array(9) { ["self"]=> array(1) { [0]=> array(1) { ["href"]=> string(51) "http://idybrand.com/wordpress/wp-json/wp/v2/posts/8" } } ["collection"]=> array(1) { [0]=> array(1) { ["href"]=> string(49) "http://idybrand.com/wordpress/wp-json/wp/v2/posts" } } ["about"]=> array(1) { [0]=> array(1) { ["href"]=> string(54) "http://idybrand.com/wordpress/wp-json/wp/v2/types/post" } } ["author"]=> array(1) { [0]=> array(2) { ["embeddable"]=> bool(true) ["href"]=> string(51) "http://idybrand.com/wordpress/wp-json/wp/v2/users/1" } } ["replies"]=> array(1) { [0]=> array(2) { ["embeddable"]=> bool(true) ["href"]=> string(59) "http://idybrand.com/wordpress/wp-json/wp/v2/comments?post=8" } } ["version-history"]=> array(1) { [0]=> array(1) { ["href"]=> string(61) "http://idybrand.com/wordpress/wp-json/wp/v2/posts/8/revisions" } } ["wp:attachment"]=> array(1) { [0]=> array(1) { ["href"]=> string(58) "http://idybrand.com/wordpress/wp-json/wp/v2/media?parent=8" } } ["wp:term"]=> array(2) { [0]=> array(3) { ["taxonomy"]=> string(8) "category" ["embeddable"]=> bool(true) ["href"]=> string(61) "http://idybrand.com/wordpress/wp-json/wp/v2/categories?post=8" } [1]=> array(3) { ["taxonomy"]=> string(8) "post_tag" ["embeddable"]=> bool(true) ["href"]=> string(55) "http://idybrand.com/wordpress/wp-json/wp/v2/tags?post=8" } } ["curies"]=> array(1) { [0]=> array(3) { ["name"]=> string(2) "wp" ["href"]=> string(23) "https://api.w.org/{rel}" ["templated"]=> bool(true) } } } } }
["total"]=> string(1) "1"
["pages"]=> string(1) "1" }
Using PHP please
This will do it according to your JSON.
echo ['results'][0]['title']['rendered']; //Title
echo ['results'][0]['content']['rendered']; //Content
You've got a multi-dimensional array so you've to get data from it.
$arr = WpApi::posts();
$arr['results'][0]['content']['rendered'] //this is your content string
$arr['results'][0]['title']['rendered'] // this is your title string

After Drupal update show Errors

I've updated a Drupal from version 7.2 to version 7.26.
After do that I've some errors:
throw new EntityMalformedException (Missing bundle property on entity of type taxonomy_term)
It appears in file common . inc:
if (!isset($entity->{$info['entity keys']['bundle']}) || $entity->{$info['entity keys']['bundle']} === '') {
I insert some "echos" to see when is shown this message, and I get this:
var_dump( $info['entity keys']);
array(4) {
["id"]=>
string(3) "tid"
["bundle"]=>
string(23) "vocabulary_machine_name"
["label"]=>
string(4) "name"
["revision"]=>
string(0) ""
}
array(4) {
["id"]=>
string(3) "tid"
["bundle"]=>
string(23) "vocabulary_machine_name"
["label"]=>
string(4) "name"
["revision"]=>
string(0) ""
}
var_dump( $info);
array(22) {
["label"]=>
string(22) "Término de taxonomía"
["controller class"]=>
string(22) "TaxonomyTermController"
["base table"]=>
string(18) "taxonomy_term_data"
["uri callback"]=>
string(17) "taxonomy_term_uri"
["fieldable"]=>
bool(true)
["entity keys"]=>
array(4) {
["id"]=>
string(3) "tid"
["bundle"]=>
string(23) "vocabulary_machine_name"
["label"]=>
string(4) "name"
["revision"]=>
string(0) ""
}
["bundle keys"]=>
array(1) {
["bundle"]=>
string(12) "machine_name"
}
["bundles"]=>
array(3) {
["pais"]=>
array(3) {
["label"]=>
string(7) "Regions"
["admin"]=>
array(4) {
["path"]=>
string(58) "admin/structure/taxonomy/%taxonomy_vocabulary_machine_name"
["real path"]=>
string(29) "admin/structure/taxonomy/pais"
["bundle argument"]=>
int(3)
["access arguments"]=>
array(1) {
[0]=>
string(19) "administer taxonomy"
}
}
["rdf_mapping"]=>
array(5) {
["rdftype"]=>
array(1) {
[0]=>
string(12) "skos:Concept"
}
["name"]=>
array(1) {
["predicates"]=>
array(2) {
[0]=>
string(10) "rdfs:label"
[1]=>
string(14) "skos:prefLabel"
}
}
["description"]=>
array(1) {
["predicates"]=>
array(1) {
[0]=>
string(15) "skos:definition"
}
}
["vid"]=>
array(2) {
["predicates"]=>
array(1) {
[0]=>
string(13) "skos:inScheme"
}
["type"]=>
string(3) "rel"
}
["parent"]=>
array(2) {
["predicates"]=>
array(1) {
[0]=>
string(12) "skos:broader"
}
["type"]=>
string(3) "rel"
}
}
}
["auto_created_voc38877"]=>
array(3) {
["label"]=>
string(10) "Provincias"
["admin"]=>
array(4) {
["path"]=>
string(58) "admin/structure/taxonomy/%taxonomy_vocabulary_machine_name"
["real path"]=>
string(46) "admin/structure/taxonomy/auto_created_voc38877"
["bundle argument"]=>
int(3)
["access arguments"]=>
array(1) {
[0]=>
string(19) "administer taxonomy"
}
}
["rdf_mapping"]=>
array(5) {
["rdftype"]=>
array(1) {
[0]=>
string(12) "skos:Concept"
}
["name"]=>
array(1) {
["predicates"]=>
array(2) {
[0]=>
string(10) "rdfs:label"
[1]=>
string(14) "skos:prefLabel"
}
}
["description"]=>
array(1) {
["predicates"]=>
array(1) {
[0]=>
string(15) "skos:definition"
}
}
["vid"]=>
array(2) {
["predicates"]=>
array(1) {
[0]=>
string(13) "skos:inScheme"
}
["type"]=>
string(3) "rel"
}
["parent"]=>
array(2) {
["predicates"]=>
array(1) {
[0]=>
string(12) "skos:broader"
}
["type"]=>
string(3) "rel"
}
}
}
["busquedas_destacadas_"]=>
array(3) {
["label"]=>
string(21) "Búsquedas destacadas"
["admin"]=>
array(4) {
["path"]=>
string(58) "admin/structure/taxonomy/%taxonomy_vocabulary_machine_name"
["real path"]=>
string(46) "admin/structure/taxonomy/busquedas_destacadas_"
["bundle argument"]=>
int(3)
["access arguments"]=>
array(1) {
[0]=>
string(19) "administer taxonomy"
}
}
["rdf_mapping"]=>
array(5) {
["rdftype"]=>
array(1) {
[0]=>
string(12) "skos:Concept"
}
["name"]=>
array(1) {
["predicates"]=>
array(2) {
[0]=>
string(10) "rdfs:label"
[1]=>
string(14) "skos:prefLabel"
}
}
["description"]=>
array(1) {
["predicates"]=>
array(1) {
[0]=>
string(15) "skos:definition"
}
}
["vid"]=>
array(2) {
["predicates"]=>
array(1) {
[0]=>
string(13) "skos:inScheme"
}
["type"]=>
string(3) "rel"
}
["parent"]=>
array(2) {
["predicates"]=>
array(1) {
[0]=>
string(12) "skos:broader"
}
["type"]=>
string(3) "rel"
}
}
}
}
["view modes"]=>
array(1) {
["full"]=>
array(2) {
["label"]=>
string(34) "Página de términos de taxonomía"
["custom settings"]=>
bool(false)
}
}
["static cache"]=>
bool(true)
["field cache"]=>
bool(true)
["load hook"]=>
string(18) "taxonomy_term_load"
["translation"]=>
array(0) {
}
["schema_fields_sql"]=>
array(1) {
["base table"]=>
array(8) {
[0]=>
string(3) "tid"
[1]=>
string(3) "vid"
[2]=>
string(4) "name"
[3]=>
string(11) "description"
[4]=>
string(6) "format"
[5]=>
string(6) "weight"
[6]=>
string(8) "language"
[7]=>
string(9) "i18n_tsid"
}
}
["token type"]=>
string(4) "term"
["access callback"]=>
string(31) "entity_metadata_taxonomy_access"
["creation callback"]=>
string(29) "entity_metadata_create_object"
["save callback"]=>
string(18) "taxonomy_term_save"
["deletion callback"]=>
string(20) "taxonomy_term_delete"
["view callback"]=>
string(27) "entity_metadata_view_single"
["form callback"]=>
string(34) "entity_metadata_form_taxonomy_term"
["configuration"]=>
bool(false)
}
I supposed this is related with something missing in the database. But I can't find it.
Any help, please?
Thanks in advance.

json_decode of Paypal PHP REST SDK api/payment JSON response returns NULL

I'm trying to get my head around the Paypal PHP REST SDK response from the Payment API,
var_dump of json_decode of the response is NULL.
I've also tried changing the format to UTF_8 with utf8_encode($response) but still getting null.
var_dump($response) is per below
object(PayPal\Api\Payment)#8 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(8) {
["id"]=>
string(28) "PAY-8JC052XXXXKKZMQNQ"
["create_time"]=>
string(20) "2013-12-19T10:19:34Z"
["update_time"]=>
string(20) "2013-12-19T10:20:38Z"
["state"]=>
string(8) "approved"
["intent"]=>
string(4) "sale"
["payer"]=>
object(PayPal\Api\Payer)#33 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(2) {
["payment_method"]=>
string(6) "paypal"
["payer_info"]=>
object(PayPal\Api\PayerInfo)#30 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(5) {
["email"]=>
string(11) "some#email.com"
["first_name"]=>
string(6) "fname"
["last_name"]=>
string(5) "lname"
["payer_id"]=>
string(13) "UAGGF3392CUTG"
["shipping_address"]=>
object(PayPal\Api\Address)#31 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(5) {
["line1"]=>
string(26) "Address"
["city"]=>
string(13) "City"
["state"]=>
string(8) "State"
["postal_code"]=>
string(5) "000000"
["country_code"]=>
string(2) "US"
}
}
}
}
}
}
["transactions"]=>
array(1) {
[0]=>
object(PayPal\Api\Transaction)#34 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(4) {
["amount"]=>
object(PayPal\Api\Amount)#35 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["total"]=>
string(4) "1.00"
["currency"]=>
string(3) "USD"
["details"]=>
object(PayPal\Api\Details)#36 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["subtotal"]=>
string(4) "1.00"
}
}
}
}
["description"]=>
string(33) "Item name: 1"
["item_list"]=>
object(PayPal\Api\ItemList)#37 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["items"]=>
array(1) {
[0]=>
object(PayPal\Api\Item)#38 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(4) {
["name"]=>
string(20) "Item name"
["price"]=>
string(4) "1.00"
["currency"]=>
string(3) "USD"
["quantity"]=>
string(1) "1"
}
}
}
}
}
["related_resources"]=>
array(1) {
[0]=>
object(PayPal\Api\RelatedResources)#40 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["sale"]=>
object(PayPal\Api\Sale)#42 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(7) {
["id"]=>
string(17) "5DH04XXX63X"
["create_time"]=>
string(20) "2013-12-19T10:19:34Z"
["update_time"]=>
string(20) "2013-12-19T10:20:38Z"
["state"]=>
string(9) "completed"
["amount"]=>
object(PayPal\Api\Amount)#44 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(2) {
["total"]=>
string(4) "1.00"
["currency"]=>
string(3) "USD"
}
}
["parent_payment"]=>
string(28) "PAY-8JC05XXXXKZMQNQ"
["links"]=>
array(3) {
[0]=>
object(PayPal\Api\Links)#46 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(65) "https://api.sandbox.paypal.com/v1/payments/sale/5DHXX91763X"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
[1]=>
object(PayPal\Api\Links)#47 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(72) "https://api.sandbox.paypal.com/v1/payments/sale/5DHXXA691763X/refund"
["rel"]=>
string(6) "refund"
["method"]=>
string(4) "POST"
}
}
[2]=>
object(PayPal\Api\Links)#48 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8JC052914XX1034SKKZMQNQ"
["rel"]=>
string(14) "parent_payment"
["method"]=>
string(3) "GET"
}
}
}
}
}
}
}
}
}
}
}
["links"]=>
array(1) {
[0]=>
object(PayPal\Api\Links)#49 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8JC0XX914D601034SKKZMQNQ"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
}
}
}

How to access transaction data from the PayPal SDK?

I am working with the PayPal SDK, and when I finish a transaction it returns this:
object(PayPal\Api\Payment)#8 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(8) {
["id"]=>
string(28) "PAY-66N6061121216644JKKS6LVQ"
["create_time"]=>
string(20) "2013-12-09T15:46:30Z"
["update_time"]=>
string(20) "2013-12-09T15:53:32Z"
["state"]=>
string(8) "approved"
["intent"]=>
string(4) "sale"
["payer"]=>
object(PayPal\Api\Payer)#33 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(2) {
["payment_method"]=>
string(6) "paypal"
["payer_info"]=>
object(PayPal\Api\PayerInfo)#30 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(5) {
["email"]=>
string(19) "indi3.rok#gmail.com"
["first_name"]=>
string(7) "Cliente"
["last_name"]=>
string(6) "Orozco"
["payer_id"]=>
string(13) "U8C2RMNA4SP9E"
["shipping_address"]=>
object(PayPal\Api\Address)#31 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(5) {
["line1"]=>
string(9) "1 Main St"
["city"]=>
string(8) "San Jose"
["state"]=>
string(2) "CA"
["postal_code"]=>
string(5) "95131"
["country_code"]=>
string(2) "US"
}
}
}
}
}
}
["transactions"]=>
array(1) {
[0]=>
object(PayPal\Api\Transaction)#34 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(4) {
["amount"]=>
object(PayPal\Api\Amount)#35 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["total"]=>
string(6) "100.00"
["currency"]=>
string(3) "USD"
["details"]=>
object(PayPal\Api\Details)#36 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["subtotal"]=>
string(6) "100.00"
}
}
}
}
["description"]=>
string(14) "Lo que pagaras"
["item_list"]=>
object(PayPal\Api\ItemList)#37 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["items"]=>
array(1) {
[0]=>
object(PayPal\Api\Item)#38 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(4) {
["name"]=>
string(23) "video: federer en paris"
["price"]=>
string(6) "100.00"
["currency"]=>
string(3) "USD"
["quantity"]=>
string(1) "1"
}
}
}
}
}
["related_resources"]=>
array(1) {
[0]=>
object(PayPal\Api\RelatedResources)#40 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["sale"]=>
object(PayPal\Api\Sale)#42 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(7) {
["id"]=>
string(17) "2ES44750XJ1684301"
["create_time"]=>
string(20) "2013-12-09T15:46:30Z"
["update_time"]=>
string(20) "2013-12-09T15:53:32Z"
["state"]=>
string(9) "completed"
["amount"]=>
object(PayPal\Api\Amount)#44 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(2) {
["total"]=>
string(6) "100.00"
["currency"]=>
string(3) "USD"
}
}
["parent_payment"]=>
string(28) "PAY-66N6061121216644JKKS6LVQ"
["links"]=>
array(3) {
[0]=>
object(PayPal\Api\Links)#46 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(65) "https://api.sandbox.paypal.com/v1/payments/sale/2ES44750XJ1684301"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
[1]=>
object(PayPal\Api\Links)#47 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(72) "https://api.sandbox.paypal.com/v1/payments/sale/2ES44750XJ1684301/refund"
["rel"]=>
string(6) "refund"
["method"]=>
string(4) "POST"
}
}
[2]=>
object(PayPal\Api\Links)#48 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-66N6061121216644JKKS6LVQ"
["rel"]=>
string(14) "parent_payment"
["method"]=>
string(3) "GET"
}
}
}
}
}
}
}
}
}
}
}
["links"]=>
array(1) {
[0]=>
object(PayPal\Api\Links)#49 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-66N6061121216644JKKS6LVQ"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
}
}
}
I am not sure what it is. (I think it is JSON but I am not sure.)
What I want to do is get access to that info in this kind of array:
$transaction["payment"]
How can I do that?
PayPal returns an object that is formatted using its own internal fromJSON() function - in the OP's case it was called from the Payment class:
$json = $call->execute(array('PayPal\Rest\RestHandler'), "/v1/payments/payment/{$this->getId()}/execute", "POST", $payLoad);
$ret = new Payment();
$ret->fromJson($json);
return $ret;
To use this as a JSON object, you need to use the toJSON() function:
$execution = new PaymentExecution();
$execution->setPayerId($_GET['PayerID']);
//Execute the payment
// (See bootstrap.php for more on `ApiContext`)
$result = $payment->execute($execution, $apiContext);
// use the result
$jsonResult = $result->toJSON();
I hope this helps.
Try the following code :
$card->toJSON()

Parsing particular value in JSON via PHP from Mapquest API

I am having trouble targeting the particular value in the array. I am trying to target the 'lng' and 'lat'. I feel like I'm close, and have tried several different ways to target. Currently trying this:
$json = file_get_contents($jsonurl);
$output = json_decode($json, true);
$latitude=$output['results']['locations']['latLng'][0];
$longitude=$output['results']['locations']['latLng'][1];
The JSON is:
array(3) {
["results"]=>
array(1) {
[0]=>
array(2) {
["locations"]=>
array(1) {
[0]=>
array(19) {
["latLng"]=>
array(2) {
["lng"]=>
float(-122.5008)
["lat"]=>
float(47.2629)
}
["adminArea4"]=>
string(13) "Pierce County"
["adminArea5Type"]=>
string(4) "City"
["adminArea4Type"]=>
string(6) "County"
["adminArea5"]=>
string(6) "Tacoma"
["street"]=>
string(0) ""
["adminArea1"]=>
string(2) "US"
["adminArea3"]=>
string(2) "WA"
["type"]=>
string(1) "s"
["displayLatLng"]=>
array(2) {
["lng"]=>
float(-122.5008)
["lat"]=>
float(47.2629)
}
["linkId"]=>
int(0)
["postalCode"]=>
string(5) "98406"
["sideOfStreet"]=>
string(1) "N"
["dragPoint"]=>
bool(false)
["adminArea1Type"]=>
string(7) "Country"
["geocodeQuality"]=>
string(3) "ZIP"
["geocodeQualityCode"]=>
string(5) "Z1XAA"
["mapUrl"]=>
string(186) "http://www.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luub2gu2ll,7w=o5-9uaauz&type=map&size=225,160&pois=purple-1,47.2629,-122.5008,0,0|&center=47.2629,-122.5008&zoom=12&rand=-8386350"
["adminArea3Type"]=>
string(5) "State"
}
}
["providedLocation"]=>
array(1) {
["location"]=>
string(5) "98406"
}
}
}
["options"]=>
array(3) {
["ignoreLatLngInput"]=>
bool(false)
["maxResults"]=>
int(-1)
["thumbMaps"]=>
bool(true)
}
["info"]=>
array(3) {
["copyright"]=>
array(3) {
["text"]=>
string(22) "© 2013 MapQuest, Inc."
["imageUrl"]=>
string(35) "http://api.mqcdn.com/res/mqlogo.gif"
["imageAltText"]=>
string(22) "© 2013 MapQuest, Inc."
}
["statuscode"]=>
int(0)
["messages"]=>
array(0) {
}
}
}
Try
$latitude=$output['results'][0]['locations'][0]['latLng']['lat'];
$longitude=$output['results'][0]['locations'][0]['latLng']['lng'];

Categories