How do I read and store response data from an API?

Asked

Viewed 484 times

0

I got the answer from API of CIELO below, now I would like to understand with do to read and store the values.

If anyone can help me with what key information should I store in the database?

object(stdClass)#2 (3) { ["MerchantOrderId"]=> string(16) "2014113245231706" ["Customer"]=> object(stdClass)#3 (1) { ["Name"]=> string(24) "Comprador rec programada" } ["Payment"]=> object(stdClass)#4 (15) { ["ServiceTaxAmount"]=> int(0) ["Installments"]=> int(1) ["Interest"]=> int(0) ["Capture"]=> bool(false) ["Authenticate"]=> bool(false) ["Recurrent"]=> bool(false) ["CreditCard"]=> object(stdClass)#5 (5) { ["CardNumber"]=> string(16) "123412******1231" ["Holder"]=> string(12) "Teste Holder" ["ExpirationDate"]=> string(7) "03/2019" ["SaveCard"]=> bool(false) ["Brand"]=> string(4) "Visa" } ["SoftDescriptor"]=> string(13) "123456789ABCD" ["Provider"]=> string(8) "Simulado" ["Type"]=> string(10) "CreditCard" ["Amount"]=> int(1500) ["Currency"]=> string(3) "BRL" ["Country"]=> string(3) "BRA" ["Status"]=> int(20) ["RecurrentPayment"]=> object(stdClass)#6 (9) { ["RecurrentPaymentId"]=> string(36) "85717927-b438-461b-908f-764772289cb5" ["ReasonCode"]=> int(0) ["ReasonMessage"]=> string(10) "Successful" ["NextRecurrency"]=> string(10) "2015-06-01" ["StartDate"]=> string(10) "2015-06-01" ["EndDate"]=> string(10) "2019-12-01" ["Interval"]=> int(6) ["Link"]=> object(stdClass)#7 (3) { ["Method"]=> string(3) "GET" ["Rel"]=> string(16) "recurrentPayment" ["Href"]=> string(107) "https://apiquerysandbox.cieloecommerce.cielo.com.br/1/RecurrentPayment/8 5717927-b438-461b-908f-764772289cb5" } ["AuthorizeNow"]=> bool(false) } } }

1 answer

0

They returned a JSON to you, it seems. You read the data as a normal object. For example:

<?php echo $objeto->MerchantOrderId; // output: 2014113245231706

To save, it depends on what you are using for persistence and what you will do with this information. But in general you can save as normal as any other information. Examples:

  • Save to a relational database by mapping the properties of this object to columns in some table or saving the entire json (using json_encode($objeto), that returns a string;
  • Save to a non-relational bank, like Mongo or couchdb;
  • Save to `file_put_contents(".json file", json_encode($object));

It is up to you to define how you want to save this data.

  • Thank you very much! And if I want the information mapped below: ["Payment"]["Recurrentpayment"]["Startdate"]=> string(10) "2015-06-01", how do I do ?

  • Could paste the JSON as it appears in the API?

  • Something like that would work ? $xmlString = "my.xml"; $xmlObject = simplexml_load_file($xmlString); $xmltovar = json_decode(json_encode($xmlObject), true); foreach($xmltovar ["Payment"] as $test) { echo $test["Recurrentpayment"]["Startdate"]." <br>"; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.