Laravel API Guzzle recording data in the database with array -> HOW TO DO?

Asked

Viewed 56 times

0

Hello, I have an API that consumes external information and needs to record the resulting data in the database, but is returning data in an array. I need a help to record the data, I appreciate the help!

public function getRemoteData(){
        $client = new Client([
            'headers' => [
                'content-type' => 'application/json',
                'Accept' => 'application',
                'authorization' => 'chavecriptografada'
            ]
        ]);

        $response = $client->request('GET', 'https://api.linkfornecedor.com/events/1.0/transactions'
        );

             return $response; // AQUI QUE EU PRECISO DE AJUDA DE COMO FAZER

Example of the structure of the returned data:

[
{
etransaction_id: "iddatransacao",
advertiser_id: 42000,
sid: 367AB76,
order_id: "v31PPP9473wrpl-01",
offer_id: "58700007",
sku_number: "POIU80AK",
sale_amount: 3499,
quantity: 1,
commissions: 10.98,
process_date: "Mon Mar 16 2020 21:49:21 GMT+0000 (UTC)",
transaction_date: "Mon Mar 16 2020 21:49:18 GMT+0000 (UTC)",
transaction_type: "realtime",
product_name: "detalhes do produto",
u1: "1e1KxLxKYGFjXxmvVQFonqSdLy1DCdA3cjqVJ8pu",
currency: "BRL",
is_event: "Y"
},
...
]

And this is the structure to record the above data:

$products = new Product();

        $products->name  = $data('name');
        $products->price  = $data('price');
        $products->description  = $data('description');
        $products->quantity  = $data('quantity');
        $products->totalCommission  = $data('totalCommission');
        $products->advertiserName  = $data('advertiserName');
        $products->mid  = $data('mid');
        $products->linkId  = $data('linkId');
        $products->consumerCity  = $data('consumerCity');
        $products->offerId  = $data('offerId');
        $products->offerName  = $data('offerName');
        $products->transactionDate  = $data('transactionDate');
        $products->clickIPAddress  = $data('clickIPAddress');
        $products->landingUrl  = $data('landingUrl');
        $products->memberU1  = $data('memberU1');
        $products->referrerUrl  = $data('referrerUrl');
        $products->transactionId  = $data('transactionId');

        $products->save();
        return response()->json($products);

1 answer

0

my brother helped me with the solution, follows below:

 $data = $response->getBody();
        $data = json_decode($data);

        for($ix = 0; $ix < sizeof($data) ; $ix++) {


            $products = new Product();

            $products->etransaction_id  = $data[$ix]->etransaction_id;

Browser other questions tagged

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