0

I am using some data in my web site like social links, contact address, contact phone, slider banners, I can use them as html in blocks or contact pages. But I am facing a problem, How to call them as REST API. I Already uses Magento2 API:

/V1/cmsBlock/:blockId 
/V1/cmsPage/:pageId

But the respobse is html and it is so bad. any help?

Jsparo30
  • 405
  • 4
  • 10
  • 30
  • I think if you don't want to get HTML from that you will have to write a custom module that allows you to input the fields you need as plain text organized in a way that you can use them as an API response. If the data you are trying to get is already available separately (like the phone number) you might just need to create an extension that adds the api endpoint you need. – oirad Apr 10 '17 at 06:13
  • can you help more? I am new in magento 2 – Jsparo30 Apr 10 '17 at 08:51
  • What data are you trying to fetch from API exactly? – oirad Apr 10 '17 at 12:46
  • External data which have no configs in backend like, slider banners, social links, contact numbers and address – Jsparo30 Apr 10 '17 at 13:12

1 Answers1

1

For the data like social links, contact numbers etc I suggest you to add this as text in the configuration, to do so you can create a module that has the following structure:

app/code/Jsparo/Customapi/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Jsparo_Customapi',
    __DIR__
);

app/code/Jsparo/Customapi/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Jsparo_Customapi" setup_version="1.0.0">
    </module>
</config>

app/code/Jsparo/Customapi/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
    <tab id="jsparo" translate="label" sortOrder="1100">
        <label>Jsparo</label>
    </tab>
    <section id="jsparo_social" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
        <label>Social</label>
        <tab>jsparo</tab>
        <resource>Jsparo_Social::config</resource>
        <group id="facebook" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
            <label>Facebook</label>
            <field id="url" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Facebook Url</label>
            </field>
        </group>
    </section>
</system>
</config>

app/code/Jsparo/Customapi/etc/webapi.xml

<?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/jsparo/facebook" method="GET">
        <service class="Jsparo\Customapi\Api\FacebookInterface" method="getUrl"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

app/code/Jsparo/Customapi/etc/di.xml

<?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="Jsparo\Customapi\Api\FacebookInterface" type="Jsparo\Customapi\Model\Facebook"/>
</config>

app/code/Jsparo/Customapi/Api/FacebookInterface.php

<?php
namespace Jsparo\Customapi\Api;
interface FacebookInterface {
    /**
     * @return string $url
     */
    public function getUrl();
}

app/code/Jsparo/Customapi/Helper/Data.php

<?php
namespace Jsparo\Customapi\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper {

    const prefix = 'jsparo_social/';
    private function moduleConfig($key) {
        return $this->scopeConfig->getValue(self::prefix . $key);
    }

    public function getFacebookUrl() {
        return $this->moduleConfig('facebook/url');
    }
}

app/code/Jsparo/Customapi/Model/Facebook.php

<?php
namespace Jsparo\Customapi\Model;
use Jsparo\Customapi\Helper\Data;
class Facebook implements FacebookInterface {

    private $helper;

    public function __construct(
        Data $helper
    ) {
        $this->helper = $helper;
    }

    public function getUrl() {
        return $this->helper->getFacebookUrl();
    }
}

You might have to do some adjustments and add all the fields / api endpoints that you need to it.

You can add also Caching to your API by using the Magento\Framework\App\CacheInterface to avoid having to perform certain computations.

Notice that I created the endpoint with the anonymous role, so that it is not protected.

EDIT: I created a github repository where you can see the full source and edited the couple typos that were above. I assume that this module source code gets added in app/code/Jsparo/Customapi.

oirad
  • 587
  • 5
  • 7
  • Thank you. But I got this error in backend->stores->configuration `"Notice: Undefined index: id in /var/www/html/magento2/app/code/Magento/Config/Model/Config/Structure/Element/Iterator.php on line 59";i:1;s:4888:"#0 /var/www/html/magento2/app/code/Magento/Config/Model/Config/Structure/Element/Iterator.php(59): Magento\Framework\App\ErrorHandler->handler(8, 'Undefined index...', '/var/www/html/m...', 59, Array) ` – Jsparo30 Apr 10 '17 at 16:41
  • What version of magento 2 are you using? – oirad Apr 11 '17 at 00:13
  • I am using magento V 2.1.4 – Jsparo30 Apr 11 '17 at 14:05
  • I edited my answer to fix the typos and created a github repo where you can have a look at the full code easier. – oirad Apr 11 '17 at 17:39
  • Hi bro, Thank you very much. If I can ask, what is the best steps and tutorials to learn magento 2? – Jsparo30 Apr 13 '17 at 14:04
  • They had some free online courses in the past months, not sure if those are still available (on the official magento website). But I believe that the best way to learn is to check how these things have been implemented in the core by the magento team and read the documentation. – oirad Apr 14 '17 at 03:24
  • Sorry, It is not or topic, but can you have a look on this question: `http://stackoverflow.com/questions/43448327/invalid-request-field-order-avsdetails-billtofirstname-was-not-in-charset-is` – Jsparo30 Apr 17 '17 at 12:07