cancel
Showing results for 
Search instead for 
Did you mean: 

How to call API and save it into database ?

How to call API and save it into database ?

i am having issue that i want to call API from the website but i do not know how to save it into my database. Can  anybody help me and can give me some suggestions ? I am grateful for your help.

Below is my db_schema in order to save the API that i wanna call:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="custom_api" resource="default" engine="innodb" comment=" Custom API Table">
        <column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Entity ID"/>
        <column xsi:type="varchar" name="title" nullable="false" length="255"  comment="Title"/>
        <column xsi:type="text" name="description" nullable="true" comment="Description"/>
        <column xsi:type="int" name="view" padding="10" nullable="false" comment="View"/>
        <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP" comment="Creation Time"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>
    </table>
</schema>

I already create the Repository, Interface and Model but i don't wanna post a long question

And here is the website that i want to save the API that i called: https://webservice.rakuten.co.jp/explorer/api. I hope the community can help me. I appreciate it

 

1 REPLY 1

Re: How to call API and save it into database ?

Hello @annq3sivn4281 


You need to create model function where you need to get data by curl by using below example code :

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'<api url>');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if (!empty($buffer)){
   //Note: Save json data with database by using collection of your modle.
}

It may help you!
Thanks

Problem solved? Click Accept as Solution!