1

I have used this below Controller to call Web service with Get method.

I got below response

enter image description here

I need to Parse and get the value the "Message".

my controller looks like below.

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';

            $this -> getMyCurlResponse($curl1);
            $resultArray = json_decode($result);
            print_r($resultArray[0]);

            if(($resultArray->status)&&($resultArray->status->message)){
                echo $resultArray->status->message;
            }

        }

}

I am getting Below error

{"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}}
**Error: Parse error on line 1:
...ation.","value":18}}1 exception(s):Exce
-----------------------^
Expecting 'EOF', '}', ',', ']'**
1 exception(s):
Exception #0 (Exception): Notice: Undefined variable: result in C:\xampp\htdocs\Magento-CE\app\code\Autosynch\synchProduct\Controller\Page\Service.php on line 41

Exception #0 (Exception): Notice: Undefined variable: result in C:\xampp\htdocs\Magento-CE\app\code\Autosynch\synchProduct\Controller\Page\Service.php on line 41
#0 C:\xampp\htdocs\Magento-CE\app\code\Autosynch\synchProduct\Controller\Page\Service.php(41): Magento\Framework\App\ErrorHandler->handler(8, 'Undefined varia...', 'C:\\xampp\\htdocs...', 41, Array)
#1 C:\xampp\htdocs\Magento-CE\var\generation\Autosynch\synchProduct\Controller\Page\Service\Interceptor.php(37): Autosynch\synchProduct\Controller\Page\Service->execute()
#2 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\App\Action\Action.php(102): Autosynch\synchProduct\Controller\Page\Service\Interceptor->execute()
#3 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Interceptor.php(74): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#4 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(70): Autosynch\synchProduct\Controller\Page\Service\Interceptor->___callParent('dispatch', Array)
#5 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...')
#6 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))
#7 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))
#8 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')
#9 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))
#10 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))
#11 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')
#12 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))
#13 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))
#14 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-...')
#15 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))
#16 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))
#17 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...')
#18 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))
#19 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))
#20 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)
#21 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))
#22 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Interceptor.php(74): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#23 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\Interception\Chain\Chain.php(70): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#24 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...')
#25 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))
#26 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))
#27 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')
#28 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))
#29 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))
#30 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...')
#31 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))
#32 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))
#33 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...')
#34 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))
#35 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))
#36 C:\xampp\htdocs\Magento-CE\var\generation\Magento\Framework\App\FrontController\Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#37 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\App\Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#38 C:\xampp\htdocs\Magento-CE\vendor\magento\framework\App\Bootstrap.php(258): Magento\Framework\App\Http->launch()
#39 C:\xampp\htdocs\Magento-CE\index.php(41): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#40 {main}

please help me, thanks In advance.

  • I have updated my answer. – kunj May 27 '18 at 10:10
1

You need to json_decode:

    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();

          return $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;
            }
        }
}
  • I am getting same error: Expecting 'EOF', '}', ',', '] – JB Pakalapati May 28 '18 at 6:12
  • Have you copied my code? – kunj May 28 '18 at 6:34
  • Yes, i have used your Code only. But still I am getting error I have mentioned in question. – JB Pakalapati May 28 '18 at 6:37
  • Actually, you need to update this: From : ` public function getMyCurlResponse($url) { $this->_curl->get($url); $response = $this->_curl->getBody(); print_r($response); }` To : ` public function getMyCurlResponse($url) { $this->_curl->get($url); $response = $this->_curl->getBody(); return $response; }` – kunj May 28 '18 at 6:40
  • Yeah, It works fine now. Thanks. If you have time can you check this question – JB Pakalapati May 28 '18 at 6:44

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.