2017-02-17 85 views
-3

如何訪問square up api返回對象中的值。訪問數組中的對象與其他對象及其值Values.Squareup返回對象api

通常我會做:$結果持有對象數組

echo $result->id; 

,但這似乎並沒有被工作:當使用的print_r()

SquareConnect\Model\ChargeResponse Object 
(
    [errors:protected] => 
    [transaction:protected] => SquareConnect\Model\Transaction Object 
     (
      [id:protected] => 1111111-15f3-5b37-6940-ceac7a6b54b8 //modified for stackoverflow 
      [location_id:protected] => 111111111111111111111111111111 //modified 
      [created_at:protected] => 2017-02-17T22:48:15Z 
      [tenders:protected] => Array 
       (
        [0] => SquareConnect\Model\Tender Object 
         (
          [id:protected] => 111111-3e5f-1111111-710c-4a4f11111d7be //modified for stack overflow 
          [location_id:protected] => 111111Nz8yXzf7rZHFYx0mbIKEgAQ //modified for stack overflow 
          [transaction_id:protected] => 111111111-15f3-5b37-6940-11111 //modified for stack overflow 
          [created_at:protected] => 2017-02-17T22:48:15Z 
          [note:protected] => Online Transaction 
          [amount_money:protected] => SquareConnect\Model\Money Object 
           (
            [amount:protected] => 402 
            [currency:protected] => USD 
           ) 

          [processing_fee_money:protected] => 
          [customer_id:protected] => 
          [type:protected] => CARD 
          [card_details:protected] => SquareConnect\Model\TenderCardDetails Object 
           (
            [status:protected] => CAPTURED 
            [card:protected] => SquareConnect\Model\Card Object 
             (
              [id:protected] => 
              [card_brand:protected] => VISA 
              [last_4:protected] => 5858 
              [exp_month:protected] => 
              [exp_year:protected] => 
              [cardholder_name:protected] => 
              [billing_address:protected] => 
             ) 

            [entry_method:protected] => KEYED 
           ) 

          [cash_details:protected] => 
         ) 

       ) 

      [refunds:protected] => 
      [reference_id:protected] => 
      [product:protected] => EXTERNAL_API 
      [client_id:protected] => 
      [order:protected] => 
      [shipping_address:protected] => 
     ) 

) 

對象返回當使用print($ result);

{ 
    "transaction": { 
     "id": "9b2f3df1-cdf3-5399-6c54-07a67af8d8f2", 
     "location_id": "CBASEGjNz8yXzf7rZHFYx0mbIKEgAQ", 
     "created_at": "2017-02-17T23:37:28Z", 
     "tenders": [ 
      { 
       "id": "c424e20e-dac0-5144-518a-114fe24cb2e5", 
       "location_id": "CBASEGjNz8yXzf7rZHFYx0mbIKEgAQ", 
       "transaction_id": "9b2f3df1-cdf3-5399-6c54-07a67af8d8f2", 
       "created_at": "2017-02-17T23:37:28Z", 
       "note": "Online Transaction", 
       "amount_money": { 
        "amount": 402, 
        "currency": "USD" 
       }, 
       "type": "CARD", 
       "card_details": { 
        "status": "CAPTURED", 
        "card": { 
         "card_brand": "VISA", 
         "last_4": "5858" 
        }, 
        "entry_method": "KEYED" 
       } 
      } 
     ], 
     "product": "EXTERNAL_API" 
    } 
} 

如何訪問square中的不同數組和對象,以便我可以使用這些信息。因此,爲了保持答案的簡短,我將如何訪問Tenders對象中的id或對象事務ID?

+0

從這裏開始。 https://docs.connect.squareup.com/ – Augwa

+0

@Augwa已經在開發者網站上。我得到了需要爲卡充電的所有代碼,但所有文檔都說它將返回一個數組,但不提供任何有關如何讀取上面粘貼的數組的代碼示例。因此上面的問題。 – DEVPROCB

+1

https://github.com/square/connect-php-sdk/blob/master/docs/Api/TransactionApi.md#charge – Augwa

回答

0

該API沒有談論訪問返回的值,所以我做了一些試驗和錯誤。最後發現,這是一個多文件,所以做了這樣的事情:$result['transaction']['tenders'][0]['type'] 允許我訪問返回數組的值。