Extract value from Stripe object - php

How do I get the value of 'name'?
I've been trying various methods, including array($charge) and __toArray.
First works, second does not:
$charge_id = $charge->id;
echo $charge_id;
$name=$charge->_values->name;
echo $name;
Here is portion of the object returned by Stripe (up to the name I want to extract):
Stripe\Charge::__set_state(array(
'_opts' =>
Stripe\Util\RequestOptions::__set_state(array(
'headers' =>
array (
),
'apiKey' => 'sk_test_...',
)),
'_values' =>
array (
'id' => 'ch_73JaKii01nmbpZ',
'object' => 'charge',
'created' => 1443251926,
'livemode' => false,
'paid' => true,
'status' => 'succeeded',
'amount' => 1000,
'currency' => 'usd',
'refunded' => false,
'source' =>
Stripe\Card::__set_state(array(
'_opts' =>
Stripe\Util\RequestOptions::__set_state(array(
'headers' =>
array (
),
'apiKey' => 'sk_test_...',
)),
'_values' =>
array (
'id' => 'card_73JZrJCDzl877J',
'object' => 'card',
'last4' => '1111',
'brand' => 'Visa',
'funding' => 'unknown',
'exp_month' => 12,
'exp_year' => 2021,
'fingerprint' => 'hhzQQZIXaAVDNMPP',
'country' => 'US',
'name' => 'Steve Veltkamp',

according to this:
https://github.com/stripe/stripe-php/blob/master/lib/Charge.php
you may try to play with retrieve method:
$charge::retrieve($charge->id);
or ( https://github.com/stripe/stripe-php/blob/master/lib/StripeObject.php ):
$chargeArr = $charge->__toArray(true);
add true as argument for recursion

(Note: you should edit your question to remove your secret key and replace it with e.g. 'sk_test_...'. Even if it's just a test key, secret keys are not meant to be shared.)
The property you're trying to access is the cardholder's name. The source attribute of a charge object is actually a card object:
php > echo get_class($charge);
Stripe\Charge
php > echo get_class($charge->source);
Stripe\Card
So to get the cardholder's name, all you have to do is:
$name = $charge->source->name;
Note that name is an optional attribute for cards, so it's possible that you'll get NULL, an empty string, an email address, etc.

Related

Access nested objects and arrays

I am working with the Mailgun API. I have a variable with some objects and arrays inside that looks like this:
$result =
stdClass::__set_state(array(
'http_response_body' =>
stdClass::__set_state(array(
'items' =>
array (
0 =>
stdClass::__set_state(array(
'geolocation' =>
stdClass::__set_state(array(
'country' => 'UK',
'region' => '72',
'city' => 'London',
)),
'tags' =>
array (
),
'mailing-list' =>
stdClass::__set_state(array(
'list-id' => 'email#address.com',
'address' => 'email#address.com',
'sid' => '123456',
)),
'ip' => '123.456.789',
'log-level' => 'info',
'id' => 'ABCA',
'campaigns' =>
array (
0 =>
stdClass::__set_state(array(
'name' => 'TEST',
'id' => 'test123',
)),
),
)),
),
'http_response_code' => 200,
));
?>
I need to access every single information inside (e.g. the IP, the country, the region), . I tried multiple solutions, including using a foreach.
foreach ($result as $row) {
echo $row->items->ip;
}
However I keep getting
Catchable fatal error: Object of class stdClass could not be converted to string
I know I am not accessing the data the right way. Could you advice?
I think that you mean to be looping over $result->http_response_body->items. What you are actually doing is looping over $result->items which doesn't seem to exist.
foreach($result->http_response_body->items as $item) {
echo $item->ip;
}

getting value from stdclass object/array

ive been trying for a while now trying to get a value from this std object
stdClass::__set_state(array(
'regions' =>
array (
0 =>
stdClass::__set_state(array(
'id' => 1,
'name' => 'Canada',
'image_url' => 'https://s3-us-west-2.amazonaws.com/staticimageskiind/flags/CA#3x.png',
)),
1 =>
stdClass::__set_state(array(
'id' => 2,
'name' => 'USA',
'image_url' => 'https://s3-us-west-2.amazonaws.com/staticimageskiind/flags/US#3x.png',
)),
2 =>
stdClass::__set_state(array(
'id' => 3,
'name' => 'Global',
'image_url' => 'https://s3-us-west-2.amazonaws.com/staticimageskiind/flags/GLBL#3x.png',
)),
3 =>
stdClass::__set_state(array(
'id' => 4,
'name' => 'Australia',
'image_url' => 'https://s3-us-west-2.amazonaws.com/staticimageskiind/flags/AU#3x.png',
)),
),
'info' =>
stdClass::__set_state(array(
'code' => 'INFO_MARKETPLACE_RETRIEVED_REGIONS',
'name' => 'Marketplace Regions Retrieved',
'message' => 'A list of marketplace regions has been retrieved.',
)),
));
but i can't seem to get a value from this object, help would be appreciated, ive searched online but couldnt find out a solution
Did you try:
echo $object->regions[0]->name;
For example?
i have managed to get the correct output i wanted by
$abc = $result->regions;
echo $abc[1]->name;
hope this helps anyone in need

How to access this array

array (
'_attributes' =>
array (
'name' => 'Rothco Product API - Variations',
),
'item_variations' =>
array (
0 =>
stdclass::__set_state(array(
'item_variation_id' => 2,
'item_index' => 3146,
'rothco_item_no' => '10002',
'upc' => '023063601212',
'inventory' => 99,
'created_date' => '2014-11-28 10:06:45.000',
'weight' => '.4000',
'image_filename' => '10002-A.jpg',
'catalog_page_no' => 183,
'msrp' => '20.9900',
'map' => '.0000',
'diameter' => '',
'price' => '8.100000',
'case_price' => NULL,
'case_quantity' => NULL,
'statuses' => '',
)),
),
)
This is my array $list.
I want to access 'item_variations' value from this array,
but if I try $list['item_variations'], or $list->['item_variations']
it is not working
If you want to reach just item_variations then
echo $list['item_variations'];
is sufficient. Things get more tricky if you would like to i.e. get value if created_date from your sample data as you got mix of arrays and objects so that require different access:
echo $list['item_variations'][0]->created_date;
which would output
2014-11-28 10:06:45.000

Change array value and save to database in PHP

how i can change specific array value in PHP? i have a Movie casts info that is a array:
array (
0 =>
array (
'name' => 'Mia Wasikowska',
'id' => 'nm1985859',
'url' => 'http://www.imdb.com/name/nm1985859/',
'image' => 'http://ia.media-imdb.com/images/M/MV5BMTUzNzI0MDczN15BMl5BanBnXkFtZTgwNTQ0MjMxOTE#._V1_.jpg',
'character' => 'Alice Kingsleigh',
),
1 =>
array (
'name' => 'Johnny Depp',
'id' => 'nm0000136',
'url' => 'http://www.imdb.com/name/nm0000136/',
'image' => 'http://ia.media-imdb.com/images/M/MV5BMTM0ODU5Nzk2OV5BMl5BanBnXkFtZTcwMzI2ODgyNQ##._V1_.jpg',
'character' => 'Hatter Tarrant Hightopp',
),
2 =>
array (
'name' => 'Helena Bonham Carter',
'id' => 'nm0000307',
'url' => 'http://www.imdb.com/name/nm0000307/',
'image' => 'http://ia.media-imdb.com/images/M/MV5BMTUzMzUzMDg5MV5BMl5BanBnXkFtZTcwMDA5NDMwNA##._V1_.jpg',
'character' => 'Iracebeth',
),
3 =>
array (
'name' => 'Anne Hathaway',
'id' => 'nm0004266',
'url' => 'http://www.imdb.com/name/nm0004266/',
'image' => 'http://ia.media-imdb.com/images/M/MV5BNjQ5MTAxMDc5OF5BMl5BanBnXkFtZTcwOTI0OTE4OA##._V1_.jpg',
'character' => 'Mirana',
),
)
i need to change image value to something different. for example, change this:
'image' => 'http://ia.media-imdb.com/images/M/MV5BNjQ5MTAxMDc5OF5BMl5BanBnXkFtZTcwOTI0OTE4OA##._V1_.jpg',
to this:
'image' => '/uploads/images/artirst_0214.jpg',
and in the end i need to have it like this:
array (
0 =>
array (
'name' => 'Mia Wasikowska',
'id' => 'nm1985859',
'url' => 'http://www.imdb.com/name/nm1985859/',
'image' => '/uploads/images/artirst_032.jpg',
'character' => 'Alice Kingsleigh',
),
1 =>
array (
'name' => 'Johnny Depp',
'id' => 'nm0000136',
'url' => 'http://www.imdb.com/name/nm0000136/',
'image' => '/uploads/images/artirst_07897.jpg',
'character' => 'Hatter Tarrant Hightopp',
),
2 =>
array (
'name' => 'Helena Bonham Carter',
'id' => 'nm0000307',
'url' => 'http://www.imdb.com/name/nm0000307/',
'image' => '/uploads/images/artirst_0987.jpg',
'character' => 'Iracebeth',
),
3 =>
array (
'name' => 'Anne Hathaway',
'id' => 'nm0004266',
'url' => 'http://www.imdb.com/name/nm0004266/',
'image' => '/uploads/images/artirst_0214.jpg',
'character' => 'Mirana',
),
)
thank you
$ArrayKeys=array_keys($data);
$cntKeys=count($ArrayKeys);
for ($i=0;$i<$cntKeys;$i++)
{
$data[$ArrayKeys[$i]]["image"]="ciro";
}
print_r ($data);
Let's suppose that your array is called "data".
In this way you get can the keys of your array (0,1,2....) and on each of them you iterate to access to the img value...
Then you can access to the value using $data[key_your_array]["image"] = "something". I use
$data[$ArrayKeys[$i]]
because, as the name suggest, $ArrayKeys is an array and I want to access to the i-th element

Parsing Output from Stripe_Charge Stripe Payments

The following output is a result of calling var_export($charge);
How can I access the output of 'paid'=>true from $charge? Any ideas would be appreciated.
I have tried $charge->_values->paid, $charge->paid, etc.. I haven't had any luck.
I have also tried $charge['_values']['paid'];
Stripe_Charge::__set_state(array(
'_apiKey' => 'sk_test_BPZyFpcAM',
'_values' =>
array (
'id' => 'ch_102kMF29T6',
'object' => 'charge',
'created' => 1381688,
'livemode' => false,
'paid' => true,
'amount' => 104000,
'currency' => 'usd',
'refunded' => false,
'card' =>
Stripe_Card::__set_state(array(
'_apiKey' => 'sk_test_BPZyFpc',
'_values' =>
array (
'id' => 'card_102kMF29T6',
'object' => 'card',
'last4' => '4242',
'type' => 'Visa',
'exp_month' => 2,
'exp_year' => 2015,
'fingerprint' => '7sRY4jiFM',
'customer' => NULL,
'country' => 'US',
'name' => NULL,
'address_line1' => NULL,
'address_line2' => NULL,
'address_city' => NULL,
'address_state' => NULL,
'address_zip' => NULL,
'address_country' => NULL,
'cvc_check' => 'pass',
'address_line1_check' => NULL,
'address_zip_check' => NULL,
),
'_unsavedValues' =>
Stripe_Util_Set::__set_state(array(
'_elts' =>
array (
),
)),
'_transientValues' =>
Stripe_Util_Set::__set_state(array(
'_elts' =>
array (
),
)),
'_retrieveOptions' =>
array (
),
)),
'captured' => true,
'refunds' =>
array (
),
'balance_transaction' => 'txn_102kMF29T6Z',
'failure_message' => NULL,
'failure_code' => NULL,
'amount_refunded' => 0,
'customer' => NULL,
'invoice' => NULL,
'description' => 'admin#fra.org',
'dispute' => NULL,
'metadata' =>
array (
),
),
'_unsavedValues' =>
Stripe_Util_Set::__set_state(array(
'_elts' =>
array (
),
)),
'_transientValues' =>
Stripe_Util_Set::__set_state(array(
'_elts' =>
array (
),
)),
'_retrieveOptions' =>
array (
),
))
You can use the __toArray($recursive = false) function to get the data in array form.
Example:
$chargeArray = $charge->__toArray(true);
echo $chargeArray['paid'];
I was also able to access the object's data using an array structure before even converting to an array. For me, $charge['paid'] gets me the value.
you can use __toArray() method:
$array = $collection->__toArray(true);
This will work for this case
whatever returns please parse it with json_decode and you will get a assoc array in php
i.e suppose return array is $ret
$ret = json_decode($ret);
$ret->paid;
$ret->captured;
$ret->card->id;
and so on..
Hope it will help you.
echo gettype($charge->paid); //boolean
var_dump($charge->paid); //bool(true)
if($charge->paid==true)echo "==true"; //==true
if($charge->paid===true)echo "===true"; //===true
if($charge->paid==1)echo "==1"; //==1
if($charge->paid===1)echo "===1"; //nada, it's a boolean, not numeric
So using if($charge->paid===true), you should be guaranteed that you are testing what you want to know.
Here in the output via print_r, you can see that paid is displayed as 1.
[_values:protected] => Array ( [id] => ch_10as29724hknftGBm9TxC [object] => charge [created] => 1384696845 [livemode] => [paid] => 1 [amount] => 1400
Thus, depending on where and how paid is being evaluated, it may appear as a number, string, or a boolean. You could run a series of tests for the three possibilities, or just use if($charge->paid){...}
When paid is false, it would probably be returned as empty (""), 0, or a boolean false. A possible thing to look out for is the boolean false being transposed to a string, in which case it would be interpreted as true.
$val = "false";
if($val){
echo "true"; //true
}
So, in your test, just add intval($val) to normalize the values and be on the safe side.
if(intval($charge->paid)){...}
Cheers

Categories