Magento – Zend_Json_Exception Decoding Failed: Syntax Error

customexceptionmagento-2.0modulerest api

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): Magento\Framework\Json\Decoder->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 Custommodule\ReviewRating\Block;

class HomehorizontalWidget extends \Magento\Framework\View\Element\Template
{
protected $_helper;
protected $jsonHelper;


public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    array $data = [],
    \Custommodule\ReviewRating\Helper\Data $helper,
    \Magento\Framework\Json\Helper\Data $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;
      } 

Best Answer

Please try below code.

app\code\core\Mage\Core\Helper\Data.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);
    }
}
Related Topic