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): 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;
      } 

3 Answers 3

2

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);
    }
}
1
  • I got the same issue while Persistent Shopping Cart enabled from admin. I fixed issue using above code Jan 4, 2019 at 9:01
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;
}
3
  • 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, 2017 at 12:52
  • Update ans pls check now
    – Abdul
    Dec 16, 2017 at 13:02
  • Its not working , same issues
    – inrsaurabh
    Jan 7, 2018 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;
            }
        }

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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