1

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) "[email protected]"
        ["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!

2
  • Post your foreach loop & the json to object conversion code. Also I will suggest you to convert your json object to an array rather than an object. Commented Dec 19, 2013 at 13:09
  • @Rikesh I've updated the questions with examples that i tried Commented Dec 19, 2013 at 22:48

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Please refer to this link first how-to-answer Second Link
1

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...

1 Comment

Can you please explain a bit more? where did you skipped that lines? I am also trying to convert the data to json but still no success.
1

You can also use

$response->toJSON();

then you can use

$result = json_decode($response);
echo ($result->state);

Comments

0

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

1 Comment

I tried to json_decode the response but it returns NULL. Is it because this is not a proper JSON format?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.