CREATE A MODULE TO USE API TO PULL PRODUCTS DATA FROM SINOGOODIES (SG) WEBSITE
I PUT THE CODE IN app/code/advcha/SGApi DIRECTORY
Create registration.php
|
1 2 3 4 5 6 7 |
<?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register( ComponentRegistrar::MODULE, 'Advcha_SGApi', __DIR__ ); |
Create etc/module.xml
|
1 2 3 4 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Advcha_SGApi" setup_version="1.0.0" /> </config> |
ENABLE THE MODULE
|
1 |
php bin/magento module:enable Advcha_SGApi |
Create etc/webapi.xml
|
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0"?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd"> <route url="/V1/sgapi/getproduct" method="GET"> <service class="Advcha\SGApi\Api\SGInterface" method="getproduct"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes> |
Create the API interface Api/SGInterface.php
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace Advcha\SGApi\Api; /** * Interface SGInterface * @package Advcha\SGApi\Api */ interface SGInterface { /** * Get the SG product * @return string JSON */ public function getproduct(); } |
Create a dependency injection di.xml for the SGInterface to the implementation in Model directory.
etc/di.xml
|
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Advcha\SGApi\Api\SGInterface" type="Advcha\SGApi\Model\SG"/> </config> |
Create the implementation in Model/SG.php (FOR EXAMPLE)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php namespace Advcha\SGApi\Model; use Advcha\SGApi\Api\SGInterface; /** * Class SG * @package Advcha\SGApi\Model */ class SG implements SGInterface { protected $_objectManager; public function __construct( \Magento\Framework\ObjectManagerInterface $objectManager ) { $this->_objectManager = $objectManager; } public function getproduct() { $resource = $this->_objectManager->create('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); $querySGProduct = "SELECT * FROM sinogoodies_products LIMIT 10"; $SGProduct = $connection->query($querySGProduct)->fetchAll(); return json_encode($SGProduct); } } |
SETUP UPGRADE AND COMPILE DI
|
1 |
php bin/magento setup:upgrade --keep-generated && php bin/magento setup:di:compile && php bin/magento setup:static-content:deploy -f && php bin/magento indexer:reindex && php bin/magento cache:clean && php bin/magento cache:flush |
TEST THE API CALL: http://ayoimport-dev.test/rest/V1/sgapi/getproduct
THE CORRECT RESPONSE WILL BE LIKE THIS:
|
1 2 3 4 |
This XML file does not appear to have any style information associated with it. The document tree is shown below. <response> [{"sku":"A00000000011201","listingId":"51216","description":"<br \/>Product Name:<br \/>Xiaomi Mi Waterproof Travel Backpack Urban Casual Life Style City Bag Office<br\/>Feat ...,"adviceTitle":"Italian Version Backlit 2.4GHz Wireless Keyboard Air Mouse Black Black Italian","categoryId":"A","categoryName":"Computers & Internet","weight":"114","stock":"300","mainImage":"http:\/\/img.sinogoodies.com\/jfl_img\/newShopImg\/\/A\/A00000009830001\/%E5%8E%9F%E5%9B%BE\/A00000009830001_1_L.jpg","packageHeight":"2","packageLength":"18","packageWidth":"11","packageWeight":"131","price":"116","purchasePrice":"39","taxPrice":"46","attributes":"[{\"attributeId\":6,\"attributeName\":\"Color\",\"attributeValueId\":9,\"attributeValueName\":\"Black\"},{\"attributeId\":114,\"attributeName\":\"Language\",\"attributeValueId\":4338,\"attributeValueName\":\"Italian\"}]","brand_id":"1407","status":"0"}] </response> |