1

Any one knows how to call restful web service from Magento 2?

Please guide me with a format how it can be done.

Any links to guide me with examples of POST or GET etc.

I want to Call below URL in Magento, Please suggest me structure of module

http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo

This URL results with following Data:

{
status: {
message: "the daily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.",
value: 18,
    }
}

Do I have to create a module? if not where I need to Call this URL?

Thanks in Advance!

1

You can create a custom module and use curl in your controller action to call this URL and Fetch response body

Curl Example (Sharing the required fragments of the file)

use Magento\Framework\HTTP\Client\Curl;
class Curlex extends AbstractHelper {
        /**
        * @var \Magento\Framework\HTTP\Client\Curl
        */
        protected $_curl;

      public function __construct(Curl $curl) 
        {

            $this->_curl =$curl;
        }
         /**
         * Send SMS
         * @param type $mobile_no
         * @param type $body
         */
        public function getResponse($url)
        {
          $url = urlencode($url);  
          $this->_curl->get($url);
          $response = $this->_curl->getBody();

         return $response
        }

}

the above example is for GET you can use for similar way (Guide for Curl with post data)

  • I have followed your example, but it says page is not found. can u check this question – JB Pakalapati May 27 '18 at 6:06
1

Here we have 2 action item here.

  1. You are using "demo" username to call the webservice which is already crossed the maximum limit. Either you need to subscribe for that webserice ( as a paid serice ) or get another sample username which you can use it.
  2. To call the webservice, you can create custom module in magento 2 and integrate the same.

app/code/Devi/CustomService/registration.php

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

app/code/Devi/CustomService/etc/module.xml

<?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="Devi_CustomService" setup_version="1.0.0" schema_version="1.0.0">
    </module>
</config>

app/code/Devi/CustomService/etc/frontend/routes.xml

app/code/Devi/CustomService/Helper/Data.php

<?php

namespace Devi\CustomService\Helper;

use Magento\Framework\HTTP\Client\Curl;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

/**
 * @param \Magento\Framework\App\Helper\Context $context
 */
public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    Curl $curl
) {
    parent::__construct($context);
    $this->curl = $curl;
}


/**
* Get order details ( order status, total, items, total invoiced )
* return Json Param
**/
public function getCitiesDetails()
{
    $uri = 'http://api.geonames.org/citiesJSON?north=44.1&south=9.9&east=22.4&west=55.2&lang=de&username=demo';
    $result = $this->curl->get($uri);
    echo '<pre>';
    print_r($result);

}
}

app/code/Devi/CustomService/Controller/Search/Index.php

<?php
namespace Devi\CustomService\Controller\Search;

use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
{
/** @var  \Magento\Framework\View\Result\Page */
protected $resultPageFactory;

/*** @param \Magento\Framework\App\Action\Context $context      */
public function __construct(
    Context $context, 
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \Devi\CustomService\Helper\Data $data
)
{
    $this->resultPageFactory = $resultPageFactory;
    $this->helper = $data;
    parent::__construct($context);
}

/**
 * Get City Details.
 *
 * @return JSON City details
 */
public function execute()
{
    // Call Helper function to get order details //
    $this->helper->getCitiesDetails();
    die;
}
}

I hope, It will be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.