{"id":4370,"date":"2019-07-22T04:18:59","date_gmt":"2019-07-22T04:18:59","guid":{"rendered":"http:\/\/myprojects.advchaweb.com\/?p=4370"},"modified":"2019-08-16T03:15:52","modified_gmt":"2019-08-16T03:15:52","slug":"magento-2-create-module-api","status":"publish","type":"post","link":"https:\/\/myprojects.advchaweb.com\/index.php\/2019\/07\/22\/magento-2-create-module-api\/","title":{"rendered":"Magento 2 &#8211; Create Module API"},"content":{"rendered":"<p>CREATE A MODULE TO USE API TO PULL PRODUCTS DATA FROM SINOGOODIES (SG) WEBSITE<br \/>\nI PUT THE CODE IN app\/code\/advcha\/SGApi DIRECTORY<br \/>\nCreate registration.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\nuse Magento\\Framework\\Component\\ComponentRegistrar;\r\nComponentRegistrar::register(\r\n    ComponentRegistrar::MODULE, \r\n    'Advcha_SGApi', \r\n    __DIR__\r\n);\r\n<\/pre>\n<p>Create etc\/module.xml<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\"&gt;\r\n \u00a0\u00a0\u00a0&lt;module name=\"Advcha_SGApi\" setup_version=\"1.0.0\" \/&gt;\r\n&lt;\/config&gt;\r\n<\/pre>\n<p>ENABLE THE MODULE<\/p>\n<pre class=\"lang:default decode:true \">php bin\/magento module:enable Advcha_SGApi<\/pre>\n<p>Create etc\/webapi.xml<\/p>\n<pre class=\"lang:default decode:true\">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;routes xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Webapi:etc\/webapi.xsd\"&gt;\r\n    &lt;route url=\"\/V1\/sgapi\/getproduct\" method=\"GET\"&gt;\r\n        &lt;service class=\"Advcha\\SGApi\\Api\\SGInterface\" method=\"getproduct\"\/&gt;\r\n        &lt;resources&gt;\r\n            &lt;resource ref=\"anonymous\"\/&gt;\r\n        &lt;\/resources&gt;\r\n    &lt;\/route&gt;\r\n&lt;\/routes&gt;\r\n<\/pre>\n<p>Create the API interface Api\/SGInterface.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Advcha\\SGApi\\Api;\r\n\r\n\/**\r\n * Interface SGInterface\r\n * @package Advcha\\SGApi\\Api\r\n *\/\r\ninterface SGInterface\r\n{\r\n    \/**\r\n     * Get the SG product \r\n     * @return string JSON\r\n     *\/\r\n    public function getproduct();\r\n\r\n}\r\n<\/pre>\n<p>Create a dependency injection di.xml for the SGInterface to the implementation in Model directory.<br \/>\netc\/di.xml<\/p>\n<pre class=\"lang:default decode:true\">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\"&gt;\r\n    &lt;preference for=\"Advcha\\SGApi\\Api\\SGInterface\" type=\"Advcha\\SGApi\\Model\\SG\"\/&gt;\r\n&lt;\/config&gt;\r\n<\/pre>\n<p>Create the implementation in Model\/SG.php (FOR EXAMPLE)<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Advcha\\SGApi\\Model;\r\n\r\nuse Advcha\\SGApi\\Api\\SGInterface;\r\n\r\n\/**\r\n * Class SG\r\n * @package Advcha\\SGApi\\Model\r\n *\/\r\nclass SG implements SGInterface\r\n{\r\n    protected $_objectManager;\r\n\r\n    public function __construct(\r\n        \\Magento\\Framework\\ObjectManagerInterface $objectManager\r\n    ) {\r\n        $this-&gt;_objectManager = $objectManager;\r\n    }\r\n\r\n    public function getproduct()\r\n    {\r\n        $resource = $this-&gt;_objectManager-&gt;create('Magento\\Framework\\App\\ResourceConnection');\r\n        $connection = $resource-&gt;getConnection();\r\n\r\n        $querySGProduct = \"SELECT * FROM sinogoodies_products LIMIT 10\";\r\n        $SGProduct = $connection-&gt;query($querySGProduct)-&gt;fetchAll();\r\n\r\n        return json_encode($SGProduct);\r\n    }\r\n}\r\n<\/pre>\n<p>SETUP UPGRADE AND COMPILE DI<\/p>\n<pre class=\"lang:default decode:true \">php bin\/magento setup:upgrade --keep-generated &amp;&amp; php bin\/magento setup:di:compile &amp;&amp; php bin\/magento setup:static-content:deploy -f &amp;&amp; php bin\/magento indexer:reindex &amp;&amp; php bin\/magento cache:clean &amp;&amp; php bin\/magento cache:flush<\/pre>\n<p>TEST THE API CALL: http:\/\/ayoimport-dev.test\/rest\/V1\/sgapi\/getproduct<br \/>\nTHE CORRECT RESPONSE WILL BE LIKE THIS:<\/p>\n<pre class=\"lang:default decode:true \">This XML file does not appear to have any style information associated with it. The document tree is shown below.\r\n&lt;response&gt;\r\n[{\"sku\":\"A00000000011201\",\"listingId\":\"51216\",\"description\":\"&lt;br \\\/&gt;Product Name:&lt;br \\\/&gt;Xiaomi Mi Waterproof Travel Backpack Urban Casual Life Style City Bag Office&lt;br\\\/&gt;Feat ...,\"adviceTitle\":\"Italian Version Backlit 2.4GHz Wireless Keyboard Air Mouse Black Black Italian\",\"categoryId\":\"A\",\"categoryName\":\"Computers &amp; 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\"}]\r\n&lt;\/response&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &lt;?php use Magento\\Framework\\Component\\ComponentRegistrar; ComponentRegistrar::register( ComponentRegistrar::MODULE, &#8216;Advcha_SGApi&#8217;, __DIR__ ); Create etc\/module.xml &lt;?xml version=&#8221;1.0&#8243;?&gt; &lt;config xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=&#8221;urn:magento:framework:Module\/etc\/module.xsd&#8221;&gt; \u00a0\u00a0\u00a0&lt;module name=&#8221;Advcha_SGApi&#8221; setup_version=&#8221;1.0.0&#8243; \/&gt; &lt;\/config&gt; ENABLE THE MODULE php bin\/magento module:enable Advcha_SGApi Create etc\/webapi.xml &lt;?xml version=&#8221;1.0&#8243;?&gt; &lt;routes &hellip; <a href=\"https:\/\/myprojects.advchaweb.com\/index.php\/2019\/07\/22\/magento-2-create-module-api\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Magento 2 &#8211; Create Module API&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71,98,13],"tags":[],"class_list":["post-4370","post","type-post","status-publish","format-standard","hentry","category-magento","category-magento-tutorial","category-tutorial"],"_links":{"self":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/4370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/comments?post=4370"}],"version-history":[{"count":6,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/4370\/revisions"}],"predecessor-version":[{"id":4550,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/4370\/revisions\/4550"}],"wp:attachment":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/media?parent=4370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/categories?post=4370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/tags?post=4370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}