Zend_Json_Exception Decoding failed: Syntax error some time in Magento (All versions)












0















1 exception(s):
Exception #0 (Zend_Json_Exception): Decoding failed: Syntax error
Exception #0 (Zend_Json_Exception): Decoding failed: Syntax error#0 /var/www/html/magento2/vendor/magento/framework/Json/Decoder.php(20): Zend_Json::decode('') #1 var/www/html/magento2/vendor/magento/framework/Json/Helper/Data.php(58): MagentoFrameworkJsonDecoder->decode(false)




In my custom mdoule I am creating a slider by fetching data form API.




IN /var/www/html/magento2/app/code/Custommodule/ReviewRating/Block/HomehorizontalWidget.php




<?php
namespace CustommoduleReviewRatingBlock;

class HomehorizontalWidget extends MagentoFrameworkViewElementTemplate
{
protected $_helper;
protected $jsonHelper;


public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
array $data = ,
CustommoduleReviewRatingHelperData $helper,
MagentoFrameworkJsonHelperData $jsonHelper
) {
parent::__construct($context, $data);

$this->_helper = $helper;
$this->jsonHelper = $jsonHelper;
}

.................

public function get_reviews_data( $url ){
$dataResponse = $this->getDataFromApi($url);
return $dataResponse['data']['reviews'];
}

public function getDataFromApi($url)
{

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$decodedData = $this->jsonHelper->jsonDecode($data);

return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;

}



IN /var/www/html/magento2/app/code/Custommodule/ReviewRating/view/frontend/templates/homehorizontalwidget.phtml




    $url = "http://website.com/api?url";

$reviewsData = $this->get_reviews_data( $url );
echo "<pre>";
print_r( $reviewsData );
echo "</pre>";
?>





Update : I chnaged few things as below ( No idea is it the perfect way or not)

Below code is from Magento 1.X version




1) Return 0 if empty




public function getDataFromApi($url)
{



        $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
$data = ( string ) $data;
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if( empty( $data )){ // retrun 0 if empty
return 0;
} else {
return ($httpcode>=200 && $httpcode<300) ? $data : false;
}


}




2) check on phtml page is array is empty or not




   $reviewsData = $this->get_reviews_data( $url );
$reviewsImages = $this->get_reviews_images( $url );
$totalRatingCount = $this->get_avg_reviews_count( $url );
$avgRating = $this->get_avg_reviews_rating( $url );


if( empty( $totalRatingCount) && empty( $avgRating ) && empty( $reviewsData ) && empty( $reviewsImages) ){
return;
}









share|improve this question





























    0















    1 exception(s):
    Exception #0 (Zend_Json_Exception): Decoding failed: Syntax error
    Exception #0 (Zend_Json_Exception): Decoding failed: Syntax error#0 /var/www/html/magento2/vendor/magento/framework/Json/Decoder.php(20): Zend_Json::decode('') #1 var/www/html/magento2/vendor/magento/framework/Json/Helper/Data.php(58): MagentoFrameworkJsonDecoder->decode(false)




    In my custom mdoule I am creating a slider by fetching data form API.




    IN /var/www/html/magento2/app/code/Custommodule/ReviewRating/Block/HomehorizontalWidget.php




    <?php
    namespace CustommoduleReviewRatingBlock;

    class HomehorizontalWidget extends MagentoFrameworkViewElementTemplate
    {
    protected $_helper;
    protected $jsonHelper;


    public function __construct(
    MagentoFrameworkViewElementTemplateContext $context,
    array $data = ,
    CustommoduleReviewRatingHelperData $helper,
    MagentoFrameworkJsonHelperData $jsonHelper
    ) {
    parent::__construct($context, $data);

    $this->_helper = $helper;
    $this->jsonHelper = $jsonHelper;
    }

    .................

    public function get_reviews_data( $url ){
    $dataResponse = $this->getDataFromApi($url);
    return $dataResponse['data']['reviews'];
    }

    public function getDataFromApi($url)
    {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $data = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    $decodedData = $this->jsonHelper->jsonDecode($data);

    return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;

    }



    IN /var/www/html/magento2/app/code/Custommodule/ReviewRating/view/frontend/templates/homehorizontalwidget.phtml




        $url = "http://website.com/api?url";

    $reviewsData = $this->get_reviews_data( $url );
    echo "<pre>";
    print_r( $reviewsData );
    echo "</pre>";
    ?>





    Update : I chnaged few things as below ( No idea is it the perfect way or not)

    Below code is from Magento 1.X version




    1) Return 0 if empty




    public function getDataFromApi($url)
    {



            $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $data = curl_exec($ch);
    $data = ( string ) $data;
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if( empty( $data )){ // retrun 0 if empty
    return 0;
    } else {
    return ($httpcode>=200 && $httpcode<300) ? $data : false;
    }


    }




    2) check on phtml page is array is empty or not




       $reviewsData = $this->get_reviews_data( $url );
    $reviewsImages = $this->get_reviews_images( $url );
    $totalRatingCount = $this->get_avg_reviews_count( $url );
    $avgRating = $this->get_avg_reviews_rating( $url );


    if( empty( $totalRatingCount) && empty( $avgRating ) && empty( $reviewsData ) && empty( $reviewsImages) ){
    return;
    }









    share|improve this question



























      0












      0








      0








      1 exception(s):
      Exception #0 (Zend_Json_Exception): Decoding failed: Syntax error
      Exception #0 (Zend_Json_Exception): Decoding failed: Syntax error#0 /var/www/html/magento2/vendor/magento/framework/Json/Decoder.php(20): Zend_Json::decode('') #1 var/www/html/magento2/vendor/magento/framework/Json/Helper/Data.php(58): MagentoFrameworkJsonDecoder->decode(false)




      In my custom mdoule I am creating a slider by fetching data form API.




      IN /var/www/html/magento2/app/code/Custommodule/ReviewRating/Block/HomehorizontalWidget.php




      <?php
      namespace CustommoduleReviewRatingBlock;

      class HomehorizontalWidget extends MagentoFrameworkViewElementTemplate
      {
      protected $_helper;
      protected $jsonHelper;


      public function __construct(
      MagentoFrameworkViewElementTemplateContext $context,
      array $data = ,
      CustommoduleReviewRatingHelperData $helper,
      MagentoFrameworkJsonHelperData $jsonHelper
      ) {
      parent::__construct($context, $data);

      $this->_helper = $helper;
      $this->jsonHelper = $jsonHelper;
      }

      .................

      public function get_reviews_data( $url ){
      $dataResponse = $this->getDataFromApi($url);
      return $dataResponse['data']['reviews'];
      }

      public function getDataFromApi($url)
      {

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
      curl_setopt($ch, CURLOPT_TIMEOUT, 5);
      $data = curl_exec($ch);
      $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      curl_close($ch);
      $decodedData = $this->jsonHelper->jsonDecode($data);

      return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;

      }



      IN /var/www/html/magento2/app/code/Custommodule/ReviewRating/view/frontend/templates/homehorizontalwidget.phtml




          $url = "http://website.com/api?url";

      $reviewsData = $this->get_reviews_data( $url );
      echo "<pre>";
      print_r( $reviewsData );
      echo "</pre>";
      ?>





      Update : I chnaged few things as below ( No idea is it the perfect way or not)

      Below code is from Magento 1.X version




      1) Return 0 if empty




      public function getDataFromApi($url)
      {



              $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
      curl_setopt($ch, CURLOPT_TIMEOUT, 5);
      $data = curl_exec($ch);
      $data = ( string ) $data;
      $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      curl_close($ch);

      if( empty( $data )){ // retrun 0 if empty
      return 0;
      } else {
      return ($httpcode>=200 && $httpcode<300) ? $data : false;
      }


      }




      2) check on phtml page is array is empty or not




         $reviewsData = $this->get_reviews_data( $url );
      $reviewsImages = $this->get_reviews_images( $url );
      $totalRatingCount = $this->get_avg_reviews_count( $url );
      $avgRating = $this->get_avg_reviews_rating( $url );


      if( empty( $totalRatingCount) && empty( $avgRating ) && empty( $reviewsData ) && empty( $reviewsImages) ){
      return;
      }









      share|improve this question
















      1 exception(s):
      Exception #0 (Zend_Json_Exception): Decoding failed: Syntax error
      Exception #0 (Zend_Json_Exception): Decoding failed: Syntax error#0 /var/www/html/magento2/vendor/magento/framework/Json/Decoder.php(20): Zend_Json::decode('') #1 var/www/html/magento2/vendor/magento/framework/Json/Helper/Data.php(58): MagentoFrameworkJsonDecoder->decode(false)




      In my custom mdoule I am creating a slider by fetching data form API.




      IN /var/www/html/magento2/app/code/Custommodule/ReviewRating/Block/HomehorizontalWidget.php




      <?php
      namespace CustommoduleReviewRatingBlock;

      class HomehorizontalWidget extends MagentoFrameworkViewElementTemplate
      {
      protected $_helper;
      protected $jsonHelper;


      public function __construct(
      MagentoFrameworkViewElementTemplateContext $context,
      array $data = ,
      CustommoduleReviewRatingHelperData $helper,
      MagentoFrameworkJsonHelperData $jsonHelper
      ) {
      parent::__construct($context, $data);

      $this->_helper = $helper;
      $this->jsonHelper = $jsonHelper;
      }

      .................

      public function get_reviews_data( $url ){
      $dataResponse = $this->getDataFromApi($url);
      return $dataResponse['data']['reviews'];
      }

      public function getDataFromApi($url)
      {

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
      curl_setopt($ch, CURLOPT_TIMEOUT, 5);
      $data = curl_exec($ch);
      $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      curl_close($ch);
      $decodedData = $this->jsonHelper->jsonDecode($data);

      return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;

      }



      IN /var/www/html/magento2/app/code/Custommodule/ReviewRating/view/frontend/templates/homehorizontalwidget.phtml




          $url = "http://website.com/api?url";

      $reviewsData = $this->get_reviews_data( $url );
      echo "<pre>";
      print_r( $reviewsData );
      echo "</pre>";
      ?>





      Update : I chnaged few things as below ( No idea is it the perfect way or not)

      Below code is from Magento 1.X version




      1) Return 0 if empty




      public function getDataFromApi($url)
      {



              $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
      curl_setopt($ch, CURLOPT_TIMEOUT, 5);
      $data = curl_exec($ch);
      $data = ( string ) $data;
      $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      curl_close($ch);

      if( empty( $data )){ // retrun 0 if empty
      return 0;
      } else {
      return ($httpcode>=200 && $httpcode<300) ? $data : false;
      }


      }




      2) check on phtml page is array is empty or not




         $reviewsData = $this->get_reviews_data( $url );
      $reviewsImages = $this->get_reviews_images( $url );
      $totalRatingCount = $this->get_avg_reviews_count( $url );
      $avgRating = $this->get_avg_reviews_rating( $url );


      if( empty( $totalRatingCount) && empty( $avgRating ) && empty( $reviewsData ) && empty( $reviewsImages) ){
      return;
      }






      module custom magento-2.0 exception rest-api






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 28 '18 at 16:24









      sv3n

      9,64062352




      9,64062352










      asked Dec 16 '17 at 12:39









      inrsaurabh

      1,080829




      1,080829






















          3 Answers
          3






          active

          oldest

          votes


















          1














          Please try below code.




          appcodecoreMageCoreHelperData.php line number 657




          replace



          public function jsonDecode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
          {
          return Zend_Json::decode($encodedValue, $objectDecodeType);
          }


          with



          public function jsonDecode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
          {
          if (empty($encodedValue) || $encodedValue == NULL ) {
          return Zend_Json::decode('{}', $objectDecodeType);
          } else {
          return Zend_Json::decode($encodedValue, $objectDecodeType);
          }
          }





          share|improve this answer





















          • I got the same issue while Persistent Shopping Cart enabled from admin. I fixed issue using above code
            – Nimesh Patel
            yesterday





















          0














          Try below code in your function:



          public function getDataFromApi($url)
          {
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
          curl_setopt($ch, CURLOPT_TIMEOUT, 5);
          $data = curl_exec($ch);
          $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
          curl_close($ch);
          $data = (string) $data;
          $decodedData = $this->jsonHelper->jsonDecode($data, true);
          return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;
          }





          share|improve this answer























          • 1) Zend_Json_Exception it comes some time not always. 2 ) your code gives me 1 exception(s): Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76 Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76
            – inrsaurabh
            Dec 16 '17 at 12:52










          • Update ans pls check now
            – Abdul
            Dec 16 '17 at 13:02










          • Its not working , same issues
            – inrsaurabh
            Jan 7 '18 at 13:31



















          0














          I am using below code
          In Magento V 2.x



          /**
          * fetch data from api
          * @param string $api_url pass API url
          * @return array return decoded API data to view to render on templates
          */
          public function getDataFromApi($psy_api_url)
          {
          sleep(1);
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $api_url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
          curl_setopt($ch, CURLOPT_TIMEOUT, 5);
          $data = curl_exec($ch);
          $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
          curl_close($ch);
          $data = (string) $data;

          if ($httpcode == 200) {
          try {
          if ($data) {
          $decodedData = $this->jsonHelper->jsonDecode($data, true);
          return ($httpcode >= 200 && $httpcode < 300) ? $decodedData : false;
          }
          } catch (Exception $e) {
          // do nothing
          return false;
          }
          }

          }


          In Magento 1.x



          /**
          * fetch data from api
          * @param string $api_url pass API url
          * @return array return decoded API data to view to render on templates
          */

          public function getDataFromApi($api_url)
          {
          sleep(1);
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
          curl_setopt($ch, CURLOPT_TIMEOUT, 5);
          $data = curl_exec($ch);
          $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
          curl_close($ch);
          $data = (string) $data;

          if ($httpcode == 200) {
          try {
          if ($data) {
          return ($httpcode>=200 && $httpcode<300) ? $data : false;
          }
          } catch (Exception $e) {
          // do nothing
          return false;
          }
          }

          }





          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f206099%2fzend-json-exception-decoding-failed-syntax-error-some-time-in-magento-all-vers%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Please try below code.




            appcodecoreMageCoreHelperData.php line number 657




            replace



            public function jsonDecode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
            {
            return Zend_Json::decode($encodedValue, $objectDecodeType);
            }


            with



            public function jsonDecode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
            {
            if (empty($encodedValue) || $encodedValue == NULL ) {
            return Zend_Json::decode('{}', $objectDecodeType);
            } else {
            return Zend_Json::decode($encodedValue, $objectDecodeType);
            }
            }





            share|improve this answer





















            • I got the same issue while Persistent Shopping Cart enabled from admin. I fixed issue using above code
              – Nimesh Patel
              yesterday


















            1














            Please try below code.




            appcodecoreMageCoreHelperData.php line number 657




            replace



            public function jsonDecode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
            {
            return Zend_Json::decode($encodedValue, $objectDecodeType);
            }


            with



            public function jsonDecode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
            {
            if (empty($encodedValue) || $encodedValue == NULL ) {
            return Zend_Json::decode('{}', $objectDecodeType);
            } else {
            return Zend_Json::decode($encodedValue, $objectDecodeType);
            }
            }





            share|improve this answer





















            • I got the same issue while Persistent Shopping Cart enabled from admin. I fixed issue using above code
              – Nimesh Patel
              yesterday
















            1












            1








            1






            Please try below code.




            appcodecoreMageCoreHelperData.php line number 657




            replace



            public function jsonDecode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
            {
            return Zend_Json::decode($encodedValue, $objectDecodeType);
            }


            with



            public function jsonDecode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
            {
            if (empty($encodedValue) || $encodedValue == NULL ) {
            return Zend_Json::decode('{}', $objectDecodeType);
            } else {
            return Zend_Json::decode($encodedValue, $objectDecodeType);
            }
            }





            share|improve this answer












            Please try below code.




            appcodecoreMageCoreHelperData.php line number 657




            replace



            public function jsonDecode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
            {
            return Zend_Json::decode($encodedValue, $objectDecodeType);
            }


            with



            public function jsonDecode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
            {
            if (empty($encodedValue) || $encodedValue == NULL ) {
            return Zend_Json::decode('{}', $objectDecodeType);
            } else {
            return Zend_Json::decode($encodedValue, $objectDecodeType);
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            Nimesh Patel

            316112




            316112












            • I got the same issue while Persistent Shopping Cart enabled from admin. I fixed issue using above code
              – Nimesh Patel
              yesterday




















            • I got the same issue while Persistent Shopping Cart enabled from admin. I fixed issue using above code
              – Nimesh Patel
              yesterday


















            I got the same issue while Persistent Shopping Cart enabled from admin. I fixed issue using above code
            – Nimesh Patel
            yesterday






            I got the same issue while Persistent Shopping Cart enabled from admin. I fixed issue using above code
            – Nimesh Patel
            yesterday















            0














            Try below code in your function:



            public function getDataFromApi($url)
            {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            $data = curl_exec($ch);
            $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            $data = (string) $data;
            $decodedData = $this->jsonHelper->jsonDecode($data, true);
            return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;
            }





            share|improve this answer























            • 1) Zend_Json_Exception it comes some time not always. 2 ) your code gives me 1 exception(s): Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76 Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76
              – inrsaurabh
              Dec 16 '17 at 12:52










            • Update ans pls check now
              – Abdul
              Dec 16 '17 at 13:02










            • Its not working , same issues
              – inrsaurabh
              Jan 7 '18 at 13:31
















            0














            Try below code in your function:



            public function getDataFromApi($url)
            {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            $data = curl_exec($ch);
            $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            $data = (string) $data;
            $decodedData = $this->jsonHelper->jsonDecode($data, true);
            return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;
            }





            share|improve this answer























            • 1) Zend_Json_Exception it comes some time not always. 2 ) your code gives me 1 exception(s): Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76 Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76
              – inrsaurabh
              Dec 16 '17 at 12:52










            • Update ans pls check now
              – Abdul
              Dec 16 '17 at 13:02










            • Its not working , same issues
              – inrsaurabh
              Jan 7 '18 at 13:31














            0












            0








            0






            Try below code in your function:



            public function getDataFromApi($url)
            {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            $data = curl_exec($ch);
            $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            $data = (string) $data;
            $decodedData = $this->jsonHelper->jsonDecode($data, true);
            return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;
            }





            share|improve this answer














            Try below code in your function:



            public function getDataFromApi($url)
            {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            $data = curl_exec($ch);
            $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            $data = (string) $data;
            $decodedData = $this->jsonHelper->jsonDecode($data, true);
            return ($httpcode>=200 && $httpcode<300) ? $decodedData : false;
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 16 '17 at 13:10

























            answered Dec 16 '17 at 12:49









            Abdul

            7,97511135




            7,97511135












            • 1) Zend_Json_Exception it comes some time not always. 2 ) your code gives me 1 exception(s): Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76 Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76
              – inrsaurabh
              Dec 16 '17 at 12:52










            • Update ans pls check now
              – Abdul
              Dec 16 '17 at 13:02










            • Its not working , same issues
              – inrsaurabh
              Jan 7 '18 at 13:31


















            • 1) Zend_Json_Exception it comes some time not always. 2 ) your code gives me 1 exception(s): Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76 Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76
              – inrsaurabh
              Dec 16 '17 at 12:52










            • Update ans pls check now
              – Abdul
              Dec 16 '17 at 13:02










            • Its not working , same issues
              – inrsaurabh
              Jan 7 '18 at 13:31
















            1) Zend_Json_Exception it comes some time not always. 2 ) your code gives me 1 exception(s): Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76 Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76
            – inrsaurabh
            Dec 16 '17 at 12:52




            1) Zend_Json_Exception it comes some time not always. 2 ) your code gives me 1 exception(s): Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76 Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento2/vendor/magento/zendframework1/library/Zend/Json.php on line 76
            – inrsaurabh
            Dec 16 '17 at 12:52












            Update ans pls check now
            – Abdul
            Dec 16 '17 at 13:02




            Update ans pls check now
            – Abdul
            Dec 16 '17 at 13:02












            Its not working , same issues
            – inrsaurabh
            Jan 7 '18 at 13:31




            Its not working , same issues
            – inrsaurabh
            Jan 7 '18 at 13:31











            0














            I am using below code
            In Magento V 2.x



            /**
            * fetch data from api
            * @param string $api_url pass API url
            * @return array return decoded API data to view to render on templates
            */
            public function getDataFromApi($psy_api_url)
            {
            sleep(1);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $api_url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            $data = curl_exec($ch);
            $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            $data = (string) $data;

            if ($httpcode == 200) {
            try {
            if ($data) {
            $decodedData = $this->jsonHelper->jsonDecode($data, true);
            return ($httpcode >= 200 && $httpcode < 300) ? $decodedData : false;
            }
            } catch (Exception $e) {
            // do nothing
            return false;
            }
            }

            }


            In Magento 1.x



            /**
            * fetch data from api
            * @param string $api_url pass API url
            * @return array return decoded API data to view to render on templates
            */

            public function getDataFromApi($api_url)
            {
            sleep(1);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            $data = curl_exec($ch);
            $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            $data = (string) $data;

            if ($httpcode == 200) {
            try {
            if ($data) {
            return ($httpcode>=200 && $httpcode<300) ? $data : false;
            }
            } catch (Exception $e) {
            // do nothing
            return false;
            }
            }

            }





            share|improve this answer


























              0














              I am using below code
              In Magento V 2.x



              /**
              * fetch data from api
              * @param string $api_url pass API url
              * @return array return decoded API data to view to render on templates
              */
              public function getDataFromApi($psy_api_url)
              {
              sleep(1);
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_URL, $api_url);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
              curl_setopt($ch, CURLOPT_TIMEOUT, 5);
              $data = curl_exec($ch);
              $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
              curl_close($ch);
              $data = (string) $data;

              if ($httpcode == 200) {
              try {
              if ($data) {
              $decodedData = $this->jsonHelper->jsonDecode($data, true);
              return ($httpcode >= 200 && $httpcode < 300) ? $decodedData : false;
              }
              } catch (Exception $e) {
              // do nothing
              return false;
              }
              }

              }


              In Magento 1.x



              /**
              * fetch data from api
              * @param string $api_url pass API url
              * @return array return decoded API data to view to render on templates
              */

              public function getDataFromApi($api_url)
              {
              sleep(1);
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
              curl_setopt($ch, CURLOPT_TIMEOUT, 5);
              $data = curl_exec($ch);
              $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
              curl_close($ch);
              $data = (string) $data;

              if ($httpcode == 200) {
              try {
              if ($data) {
              return ($httpcode>=200 && $httpcode<300) ? $data : false;
              }
              } catch (Exception $e) {
              // do nothing
              return false;
              }
              }

              }





              share|improve this answer
























                0












                0








                0






                I am using below code
                In Magento V 2.x



                /**
                * fetch data from api
                * @param string $api_url pass API url
                * @return array return decoded API data to view to render on templates
                */
                public function getDataFromApi($psy_api_url)
                {
                sleep(1);
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $api_url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
                curl_setopt($ch, CURLOPT_TIMEOUT, 5);
                $data = curl_exec($ch);
                $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                curl_close($ch);
                $data = (string) $data;

                if ($httpcode == 200) {
                try {
                if ($data) {
                $decodedData = $this->jsonHelper->jsonDecode($data, true);
                return ($httpcode >= 200 && $httpcode < 300) ? $decodedData : false;
                }
                } catch (Exception $e) {
                // do nothing
                return false;
                }
                }

                }


                In Magento 1.x



                /**
                * fetch data from api
                * @param string $api_url pass API url
                * @return array return decoded API data to view to render on templates
                */

                public function getDataFromApi($api_url)
                {
                sleep(1);
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
                curl_setopt($ch, CURLOPT_TIMEOUT, 5);
                $data = curl_exec($ch);
                $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                curl_close($ch);
                $data = (string) $data;

                if ($httpcode == 200) {
                try {
                if ($data) {
                return ($httpcode>=200 && $httpcode<300) ? $data : false;
                }
                } catch (Exception $e) {
                // do nothing
                return false;
                }
                }

                }





                share|improve this answer












                I am using below code
                In Magento V 2.x



                /**
                * fetch data from api
                * @param string $api_url pass API url
                * @return array return decoded API data to view to render on templates
                */
                public function getDataFromApi($psy_api_url)
                {
                sleep(1);
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $api_url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
                curl_setopt($ch, CURLOPT_TIMEOUT, 5);
                $data = curl_exec($ch);
                $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                curl_close($ch);
                $data = (string) $data;

                if ($httpcode == 200) {
                try {
                if ($data) {
                $decodedData = $this->jsonHelper->jsonDecode($data, true);
                return ($httpcode >= 200 && $httpcode < 300) ? $decodedData : false;
                }
                } catch (Exception $e) {
                // do nothing
                return false;
                }
                }

                }


                In Magento 1.x



                /**
                * fetch data from api
                * @param string $api_url pass API url
                * @return array return decoded API data to view to render on templates
                */

                public function getDataFromApi($api_url)
                {
                sleep(1);
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
                curl_setopt($ch, CURLOPT_TIMEOUT, 5);
                $data = curl_exec($ch);
                $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                curl_close($ch);
                $data = (string) $data;

                if ($httpcode == 200) {
                try {
                if ($data) {
                return ($httpcode>=200 && $httpcode<300) ? $data : false;
                }
                } catch (Exception $e) {
                // do nothing
                return false;
                }
                }

                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 12 '18 at 8:01









                inrsaurabh

                1,080829




                1,080829






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f206099%2fzend-json-exception-decoding-failed-syntax-error-some-time-in-magento-all-vers%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    An IMO inspired problem

                    Management

                    Has there ever been an instance of an active nuclear power plant within or near a war zone?