1

I am trying to get call get web service sample get link

<?php
namespace Autosynch\synchProduct\Controller\Page;
use Magento\Framework\HTTP\Client\Curl;

class Service extends \Magento\Framework\App\Action\Action {
        /**
        * @param \Magento\Framework\App\Action\Context $context
        * @param \Magento\Framework\HTTP\Client\Curl $curl
        */
        protected $_curl;

        public function __construct(
            \Magento\Framework\App\Action\Context $context,
            Curl $curl
        ) 
        {

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

         print_r($response);
        }

        public function execute()
        {
            echo "Welcome to Service Call";

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

            $this -> getMyCurlResponse($curl1);

        }

}

but I am getting below error.

Welcome to Service Call1 exception(s):
Exception #0 (Exception): Could not resolve host: http%3A%2F%2Fapi.geonames.org%2FcitiesJSON%3Fnorth%3D44.1%26south%3D-9.9%26east%3D-22.4%26west%3D55.2%26lang%3Dde%26username%3Ddemo

Exception #0 (Exception): Could not resolve host: http%3A%2F%2Fapi.geonames.org%2FcitiesJSON%3Fnorth%3D44.1%26south%3D-9.9%26east%3D-22.4%26west%3D55.2%26lang%3Dde%26username%3Ddemo
#0 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\HTTP\Client\Curl.php(389): Magento\Framework\HTTP\Client\Curl->doError('Could not resol...')
#1 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\HTTP\Client\Curl.php(219): Magento\Framework\HTTP\Client\Curl->makeRequest('GET', 'http%3A%2F%2Fap...')
#2 C:\xampp\htdocs\Magento-CE\app\code\Autosynch\synchProduct\Controller\Page\Service.php(29): Magento\Framework\HTTP\Client\Curl->get('http%3A%2F%2Fap...')
#3 C:\xampp\htdocs\Magento-CE\var\generation\Autosynch\synchProduct\Controller\Page\Service\Interceptor.php(24): Autosynch\synchProduct\Controller\Page\Service->getMyCurlResponse('http://api.geon...')
#4 C:\xampp\htdocs\Magento-CE\app\code\Autosynch\synchProduct\Controller\Page\Service.php(39): Autosynch\synchProduct\Controller\Page\Service\Interceptor->getMyCurlResponse('http://api.geon...')
#5 C:\xampp\htdocs\Magento-CE\var\generation\Autosynch\synchProduct\Controller\Page\Service\Interceptor.php(37): Autosynch\synchProduct\Controller\Page\Service->execute()
#6 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\App\Action\Action.php(102): Autosynch\synchProduct\Controller\Page\Service\Interceptor->execute()
#7 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Interceptor.php(74): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#8 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(70): Autosynch\synchProduct\Controller\Page\Service\Interceptor->___callParent('dispatch', Array)
#9 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Autosynch\\synch...', 'dispatch', Object(Autosynch\synchProduct\Controller\Page\Service\Interceptor), Array, 'customer-app-ac...')
#10 C:\xampp\htdocs\Magento-CE\vendor\magento\module-customer\Model\App\Action\ContextPlugin.php(61): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#11 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(67): Magento\Customer\Model\App\Action\ContextPlugin->aroundDispatch(Object(Autosynch\synchProduct\Controller\Page\Service\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#12 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Autosynch\\synch...', 'dispatch', Object(Autosynch\synchProduct\Controller\Page\Service\Interceptor), Array, 'contextPlugin')
#13 C:\xampp\htdocs\Magento-CE\vendor\magento\module-store\App\Action\Plugin\Context.php(106): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#14 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(67): Magento\Store\App\Action\Plugin\Context->aroundDispatch(Object(Autosynch\synchProduct\Controller\Page\Service\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#15 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Autosynch\\synch...', 'dispatch', Object(Autosynch\synchProduct\Controller\Page\Service\Interceptor), Array, 'storeCheck')
#16 C:\xampp\htdocs\Magento-CE\vendor\magento\module-store\App\Action\Plugin\StoreCheck.php(44): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#17 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(67): Magento\Store\App\Action\Plugin\StoreCheck->aroundDispatch(Object(Autosynch\synchProduct\Controller\Page\Service\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#18 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Autosynch\\synch...', 'dispatch', Object(Autosynch\synchProduct\Controller\Page\Service\Interceptor), Array, 'tax-app-action-...')
#19 C:\xampp\htdocs\Magento-CE\vendor\magento\module-tax\Model\App\Action\ContextPlugin.php(91): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#20 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(67): Magento\Tax\Model\App\Action\ContextPlugin->aroundDispatch(Object(Autosynch\synchProduct\Controller\Page\Service\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#21 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Autosynch\\synch...', 'dispatch', Object(Autosynch\synchProduct\Controller\Page\Service\Interceptor), Array, 'weee-app-action...')
#22 C:\xampp\htdocs\Magento-CE\vendor\magento\module-weee\Model\App\Action\ContextPlugin.php(112): Autosynch\synchProduct\Controller\Page\Service\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#23 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Interceptor.php(142): Magento\Weee\Model\App\Action\ContextPlugin->aroundDispatch(Object(Autosynch\synchProduct\Controller\Page\Service\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#24 C:\xampp\htdocs\Magento-CE\var\generation\Autosynch\synchProduct\Controller\Page\Service\Interceptor.php(52): Autosynch\synchProduct\Controller\Page\Service\Interceptor->___callPlugins('dispatch', Array, Array)
#25 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\App\FrontController.php(55): Autosynch\synchProduct\Controller\Page\Service\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#26 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Interceptor.php(74): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#27 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(70): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#28 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'requestPreproce...')
#29 C:\xampp\htdocs\Magento-CE\vendor\magento\module-store\App\FrontController\Plugin\RequestPreprocessor.php(94): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#30 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(67): Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#31 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'install')
#32 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Module\Plugin\DbStatusValidator.php(69): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#33 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(67): Magento\Framework\Module\Plugin\DbStatusValidator->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#34 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'front-controlle...')
#35 C:\xampp\htdocs\Magento-CE\vendor\magento\module-page-cache\Model\App\FrontController\BuiltinPlugin.php(73): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#36 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(67): Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#37 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'front-controlle...')
#38 C:\xampp\htdocs\Magento-CE\vendor\magento\module-page-cache\Model\App\FrontController\VarnishPlugin.php(55): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#39 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Interceptor.php(142): Magento\PageCache\Model\App\FrontController\VarnishPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#40 C:\xampp\htdocs\Magento-CE\var\generation\Magento\Framework\App\FrontController\Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#41 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\App\Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#42 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\App\Bootstrap.php(258): Magento\Framework\App\Http->launch()
#43 C:\xampp\htdocs\Magento-CE\index.php(41): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#44 {main}
1

Try this:

<?php
namespace Autosynch\synchProduct\Controller\Page;
use Magento\Framework\HTTP\Client\Curl;

class Service extends \Magento\Framework\App\Action\Action {
        /**
        * @param \Magento\Framework\App\Action\Context $context
        * @param \Magento\Framework\HTTP\Client\Curl $curl
        */
        protected $_curl;

        public function __construct(
            \Magento\Framework\App\Action\Context $context,
            Curl $curl
        ) 
        {

            parent::__construct($context);
            $this->_curl =$curl;
        }
         /**
         * Send SMS
         * @param type $mobile_no
         * @param type $body
         */
        public function getMyCurlResponse($url)
        {
          $this->_curl->get($url);
          $response = $this->_curl->getBody();

         print_r($response);
        }

                public function execute()
    {
        echo "Welcome to Service Call";

        $curl1 = 'http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo';
        $result = $this->getMyCurlResponse($curl1);
        $resultArray = json_decode($result);
        if(($resultArray->status)&&($resultArray->status->message)){
            echo $resultArray->status->message;
        }
    }


}
  • to print response you need to do this echo $this->getMyCurlResponse($curl1);. – kunj May 27 '18 at 8:18
  • Works perfectly, how can I parse the result and read "Message" – JB Pakalapati May 27 '18 at 8:24
  • try This: ` public function execute() { echo "Welcome to Service Call"; $curl1 = 'api.geonames.org/…'; $result = $this->getMyCurlResponse($curl1); $resultArray = json_decode($result); if(($resultArray->status)&&($resultArray->status->message)){ echo $resultArray->status->message; } } ` – kunj May 27 '18 at 8:35
  • Also updated answer. – kunj May 27 '18 at 8:36

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.