简体   繁体   中英

parsing the Paypal REST API response

I have json_decoded my paypal rest api response and gotten this:

["body"]=> object(stdClass)#4 (8) { 

    ["id"]=> string(28) "PAY-66D616332R6551639KJLSMVQ" 
    ["create_time"]=> string(20) "2013-10-10T22:12:38Z" 
    ["update_time"]=> string(20) "2013-10-10T22:12:39Z" 
    ["state"]=> string(7) "created" 
    ["intent"]=> string(4) "sale" 
    ["payer"]=> object(stdClass)#5 (2) { 
        ["payment_method"]=> string(6) "paypal" 
        ["payer_info"]=> object(stdClass)#6 (0) {} 
    } 
    ["transactions"]=> array(1) { 
        [0]=> object(stdClass)#7 (3) { 
            ["amount"]=> object(stdClass)#8 (3) { 
                ["total"]=> string(6) "500.85" 
                ["currency"]=> string(3) "USD" 
                ["details"]=> object(stdClass)#9 (2) { 
                    ["subtotal"]=> string(6) "460.90" 
                    ["shipping"]=> string(5) "39.95" 
                } 
            } 
            ["description"]=> string(43) "Mike and Maureen Photography - Order ID #10" 
            ["item_list"]=> object(stdClass)#10 (1) { 
                ["items"]=> array(2) { 
                    [0]=> object(stdClass)#11 (5) { 
                        ["name"]=> string(48) "The Bean-8" x 10" - floating frame - black frame" 
                        ["sku"]=> string(7) "20 - 13" 
                        ["price"]=> string(6) "160.95" 
                        ["currency"]=> string(3) "USD" 
                        ["quantity"]=> string(1) "1" 
                    } 
                    [1]=> object(stdClass)#12 (5) { 
                        ["name"]=> string(62) "40 Steps and a View-36" x 48" - 0.75" thin gallery wrap canvas" 
                        ["sku"]=> string(5) "7 - 6" 
                        ["price"]=> string(6) "299.95" 
                        ["currency"]=> string(3) "USD" 
                        ["quantity"]=> string(1) "1" 
                    } 
                } 
            } 
        } 
    } 
    ["links"]=> array(3) { 
        [0]=> object(stdClass)#13 (3) { 
            ["href"]=> string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-66D616332R6551639KJLSMVQ" 
            ["rel"]=> string(4) "self" 
            ["method"]=> string(3) "GET" 
        } 
        [1]=> object(stdClass)#14 (3) { 
            ["href"]=> string(94) "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-1XB37931V5368954G" 
            ["rel"]=> string(12) "approval_url" 
            ["method"]=> string(8) "REDIRECT" 
        } 
        [2]=> object(stdClass)#15 (3) { 
            ["href"]=> string(87) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-66D616332R6551639KJLSMVQ/execute" 
            ["rel"]=> string(7) "execute" 
            ["method"]=> string(4) "POST" 
        } 
    } 
} 

I am trying to check the value of 'state' but I cant seem to figure out how to reference that value. I have tried: ( $result is the variable where the array is located)

$result['body']['state']
$result['state']
$body['state']

None of those work, so can anyone tell me how to reference the key 'state' in that mess up there? I am usually pretty good with PHP but for some reason I can not figure this one out.

Thanks for your help.

Edit

I have formatted the response so its easier to ready but I am stuck on selecting the [1] href value. If I base it on the previous example I would use

$result['body']->link but how do I get past that to the specific href in [1]?

The formatting doesn't help, but it looks like $result['body']->state will get you what you're looking for.

You were closest with your first attempt, $result['body']['state'] , however $result['body'] is an object, and so you need to use -> to access its properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM