0

When I print print_r($event_json); it gives me the array below. I need to get just one value from it [type] as in [type] => charge.succeeded (see the top of the array). I tried echo $event_json[0]['type']; but I am getting nothing.

stdClass Object
(
[created] => 1326853478
[livemode] => 
[id] => charge.succeeded_00000000000000
[type] => charge.succeeded
[object] => event
[request] => 
[pending_webhooks] => 1
[api_version] => 2018-10-31
[data] => stdClass Object
    (
        [object] => stdClass Object
            (
                [id] => ch_00000000000000
                [object] => charge
                [amount] => 500
                [amount_refunded] => 0
                [application] => 
                [application_fee] => 
                [application_fee_amount] => 
                [balance_transaction] => txn_00000000000000
                [billing_details] => stdClass Object
                    (
                        [address] => stdClass Object
                            (
                                [city] => 
                                [country] => 
                                [line1] => 
                                [line2] => 
                                [postal_code] => 
                                [state] => 
                            )

                        [email] => 
                        [name] => someemail@yahoo.com
                        [phone] => 
                    )

                [captured] => 
                [created] => 1553691107
                [currency] => usd
                [customer] => cus_00000000000000
                [description] => 
                [destination] => 
                [dispute] => 
                [failure_code] => card_declined
                [failure_message] => Your card was declined.
                [fraud_details] => stdClass Object
                    (
                        [stripe_report] => fraudulent
                    )

                [invoice] => 
                [livemode] => 
                [metadata] => stdClass Object
                    (
                    )

                [on_behalf_of] => 
                [order] => 
                [outcome] => stdClass Object
                    (
                        [network_status] => not_sent_to_network
                        [reason] => merchant_blacklist
                        [risk_level] => highest
                        [risk_score] => 98
                        [seller_message] => Stripe blocked this payment.
                        [type] => blocked
                    )

                [paid] => 1
                [payment_intent] => 
                [payment_method_details] => stdClass Object
                    (
                        [card] => stdClass Object
                            (
                                [brand] => visa
                                [checks] => stdClass Object
                                    (
                                        [address_line1_check] => 
                                        [address_postal_code_check] => 
                                        [cvc_check] => unavailable
                                    )

                                [country] => US
                                [exp_month] => 1
                                [exp_year] => 2022
                                [fingerprint] => Gawa768Trrk4fEmb
                                [funding] => credit
                                [last4] => 0019
                                [three_d_secure] => 
                                [wallet] => 
                            )

                        [type] => card
                    )

                [receipt_email] => 
                [receipt_number] => 
                [receipt_url] => https://pay.stripe.com/receipts/acct_1DU4J1IcP22cq9aQ/ch_1EIbENIcP22cq9aQk7r9xN3u/rcpt_Em9Xy3Ambv1b8QZISOsIHdbxCnLdgup
                [refunded] => 
                [refunds] => stdClass Object
                    (
                        [object] => list
                        [data] => Array
                            (
                            )

                        [has_more] => 
                        [total_count] => 0
                        [url] => /v1/charges/ch_1EIbENIcP22cq9aQk7r9xN3u/refunds
                    )

                [review] => 
                [shipping] => 
                [source] => stdClass Object
                    (
                        [id] => card_00000000000000
                        [object] => card
                        [address_city] => 
                        [address_country] => 
                        [address_line1] => 
                        [address_line1_check] => 
                        [address_line2] => 
                        [address_state] => 
                        [address_zip] => 
                        [address_zip_check] => 
                        [brand] => Visa
                        [country] => US
                        [customer] => cus_00000000000000
                        [cvc_check] => unavailable
                        [dynamic_last4] => 
                        [exp_month] => 1
                        [exp_year] => 2022
                        [fingerprint] => Gawa768Trrk4fEmb
                        [funding] => credit
                        [last4] => 0019
                        [metadata] => stdClass Object
                            (
                            )

                        [name] => someemail@yahoo.com
                        [tokenization_method] => 
                    )

                [source_transfer] => 
                [statement_descriptor] => 
                [status] => failed
                [transfer_data] => 
                [transfer_group] => 
            )

    )

)
Signs7771
  • 51
  • 6
  • Try `$event_json->type` as you have decoded your JSON as an object, not an array. – Nick Mar 29 '19 at 13:24
  • Unfortunately, that doesn't work. It gets me the event name "customer.source.created" nothing else. That's why I asked the question. Can I do something like this? `foreach ($event_json as $key => $val) { if(is_array($val)) { echo "$key:\n"; } else { echo "$key => $val\n"; } }` The problem is I do not get any output. Any ideas why? – Signs7771 Mar 29 '19 at 14:26

1 Answers1

0

What you are seeing is an stdClass Object and not an array. Do like below:

<?php

echo $event_json->type;
nice_dev
  • 17,053
  • 2
  • 21
  • 35
  • Unfortunately, that doesn't work. It gets me the event name "customer.source.created" nothing else. That's why I asked the question. Can I do something like this? `foreach ($event_json as $key => $val) { if(is_array($val)) { echo "$key:\n"; } else { echo "$key => $val\n"; } }` The problem is I do not get any output. Any ideas why? – Signs7771 Mar 29 '19 at 14:25
  • 1
    @Signs7771 Can you edit your question and share what you **exactly** have in `$event_json` ? There is no reason for it to not work from the example shown. – nice_dev Mar 29 '19 at 14:29
  • It's a simple Stripe webhook. `require_once('stripe/init.php'); \Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxx"); $input = @file_get_contents("php://input"); $event_json = json_decode($input);` the `$event_json->type;` doesn't give me what I need `charge.succeeded` or `charge.failed` not sure why. So I thought maybe I can get a specific value from that array. If I can. – Signs7771 Mar 29 '19 at 14:35
  • @Signs7771 I can't say much without looking at the data you are receiving. – nice_dev Mar 29 '19 at 18:23