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): 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;
      } 
Was it helpful?

Solution 3

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

    }

OTHER TIPS

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

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top