We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Help me json_decode doesn't work from phalcon site

I was in the middle of spending all day trying to fix it and it all failed, I wanted json_decode from my phalcon website. My source code in Controller:

class JsonController extends Controller
{  
    public function indexAction()
    {
      @header('Content-Type: application/json');
        $response["status"] = "true";  
        $response["code"] = 'AAAAA'; //["product"]
        $response["name"] = 'BBBB';   //["product"] 
        echo json_encode($response,JSON_UNESCAPED_UNICODE); 
        exit;
    }
}

When I retrieved data from the phacon website (https://example.com/json/) , it all failed. Source get data json:

    <?php
        $json = file_get_contents('https://example.com/json/');  
        $read_json = json_decode($json,true); 
        echo $read_json['status'].'<br>';
        echo $read_json['name'].'<br>';
        echo $read_json['name'].'<br>';
    ?>

however, when I created a static php page outside of the controller (in public/json.php => https://example.com/json.php) , it worked

json.php file:

<?php 
   @header('Content-Type: application/json');
    $response["status"] = "true";  
    $response["code"] = 'AAAAA'; //["product"]
    $response["name"] = 'BBBB';   //["product"] 
    echo json_encode($response,JSON_UNESCAPED_UNICODE); 
    exit;
?>

Get data json in public:

    <?php
        $json = file_get_contents('https://example.com/json.php');  
        $read_json = json_decode($json,true); 
        echo $read_json['status'].'<br>';
        echo $read_json['name'].'<br>';
        echo $read_json['name'].'<br>';
    ?>

Look forward to the help

Thank you



8.4k
edited Apr '20

it would help if you provided the output from file_get_contents('https://example.com/json.php')

anyway it could be a view problem and the output is not pure JSON

i would suggest a couple of adjustments and you test it and provide the output if that didn't help

also don't forget to enable php errors it could shed some light error_reporting(E_ALL);

class JsonController extends \Phalcon\Mvc\Controller
{  
    public function indexAction()
    {
        $this->view->setRenderLevel(
            \Phalcon\Mvc\View::LEVEL_NO_RENDER
        );

        $response = [
            'status' => 'true',
            'code' => 'AAAAA',
            'name' => 'BBBB'
        ];

        $this->response->setJsonContent($response, \JSON_UNESCAPED_UNICODE);

        $this->response->send();
    }
}


3.0k

Read this documentation.

https://docs.phalcon.io/4.0/en/response

I also tried according to the document, upload website server code:

        $response = new Response();
        $contents = [
            'invoice' => [
                'id'    => 12345,
                'name'  => 'invoice.pdf',
                'date'  => '2019-01-01 01:02:03',
                'owner' => 'admin',
            ]   
        ];
        $response->setJsonContent($contents)->send();

json_decode:

    $data = file_get_contents('https://example.com/json/');  
    $read_json = json_decode($data,true);  
    var_dump($read_json); 

result =>NULL

is still not working, while at localhost it works



3.0k

it would help if you provided the output from file_get_contents('https://example.com/json.php')

anyway it could be a view problem and the output is not pure JSON

i would suggest a couple of adjustments and you test it and provide the output if that didn't help

also don't forget to enable php errors it could shed some light error_reporting(E_ALL);

class JsonController extends \Phalcon\Mvc\Controller
{  
   public function indexAction()
   {
       $this->view->setRenderLevel(
           \Phalcon\Mvc\View::LEVEL_NO_RENDER
       );

       $response = [
           'status' => 'true',
           'code' => 'AAAAA',
           'name' => 'BBBB'
       ];

       $this->response->setJsonContent($response, \JSON_UNESCAPED_UNICODE);

       $this->response->send();
   }
}

I copy the code and posted it on the website phalcon json_decode:

  $data = file_get_contents('https://example.com/json/');  //(web server phalcon)
  $read_json = json_decode($data,true);  
  var_dump($read_json); 

result =>NULL

json_decode:

  $data = file_get_contents('https://localhost/json/');  //(web server phalcon in local)
  $read_json = json_decode($data,true);  
  var_dump($read_json); 

result => array(3) { ["status"]=> string(4) "true" ["code"]=> string(5) "AAAAA" ["name"]=> string(4) "BBBB" } OK

I am definitely not configuring the server, because I put the pure php file in public, it works fine. just doesn't work in phalcon controller



8.4k
edited Apr '20

we need to know whats the actual output not the outcome of json_decode()

you need to check what is the content of $data

so change the code to:

$data = file_get_contents('https://example.com/json/');  //(web server phalcon)
var_dump($data);

and turn on the error report in your phalcon dir /phalcon_dir/public/index.php to error_reporting(E_ALL);

if there is a problem in your code on the way to your controller this would point you to the problem

also you can use Fetch or jQuery to quickly verify http response code was sent from the server

use the developer tools in your browser and paste one of these in the console and check the network tab ( in chrome )

fetch('https://example.com/json/');

// or

$.get('https://example.com/json/');


3.0k

I figured out how it works

The above code is not an error

I found the solution to make it work is crazy.

I remove the config.php file (Copy the code), created a new config.php file and Paste the old code.

So it works!

Thanks all

I figured out how it works

The above code is not an error

I found the solution to make it work is crazy.

I remove the config.php file (Copy the code), created a new config.php file and Paste the old code.

So it works!

Thanks all

Might be permissions issue.



3.0k

I figured out how it works

The above code is not an error

I found the solution to make it work is crazy.

I remove the config.php file (Copy the code), created a new config.php file and Paste the old code.

So it works!

Thanks all

Might be permissions issue.

I still don't know exactly what, but it is not permissions. Because get json with jquery still works



71

Fun factor is, in another controller, I don't have any issues with binding and executing queries through the modelsManager. Only variations are column count number and (a long way) much less variable paintings. But still you can try to make a project to run the wood cutter tool with AI technology like chainsaw https://forum.phalcon.io/discussion/20567/query-tells-field-is-required-but-is-set