Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_encode giving recursion error

Tags:

json

ajax

php

Warning:  json_encode(): recursion detected in [Directory] 

What is this error, I can't seem to solve the issue. It's generating a BIG BIG log per error 500 faced. 133,000,000 bytes in size. It's spamming the log till memory max out.

<?php
include('simple_html_dom.php');

if(isset($_REQUEST['type']) && $_REQUEST['type'] = "getmoredetails"){ 
retrievemore($_REQUEST['htmlsource']);
}

function retrievemore($htmlcode){
$retrievetitle = retrievechTitle($htmlcode);
$retrievermb = retrievechRMB($htmlcode);
echo json_encode(array("error"=>0,"rmb"=>$retrievermb,"title"=>$retrievetitle));
}
function retrievechTitle($htmlcode){
$html = str_get_html($htmlcode);
$title = $html->find('div[class=tb-detail-hd]h3');
return $title[0];
}
function retrievechRMB($htmlcode){
$html = str_get_html($htmlcode);
$rmb = $html->find('[class=tb-rmb-num]');
return $rmb[0];
}

?>

I'm trying to extract data from a HTML file, the other extraction works fine, except for the above, giving lots of issue. I even separated this set of code specifically to one PHP file for processing and same issue.

Any idea? I use jQuery Ajax with multiple functions at home page with $.ajax({
I'm new to Ajax, it's alright to have multiple Ajax in one page right?

like image 959
CodeGuru Avatar asked Jun 21 '13 12:06

CodeGuru


People also ask

What does json_encode return?

Syntax. The json_encode() function can return a string containing the JSON representation of supplied value. The encoding is affected by supplied options, and additionally, the encoding of float values depends on the value of serialize_precision.

What is JSON_ encode in php?

The json_encode() function is used to encode a value to JSON format.

How can I get JSON encoded data in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

What is JSON encoded string?

The method JSON. stringify(student) takes the object and converts it into a string. The resulting json string is called a JSON-encoded or serialized or stringified or marshalled object.


1 Answers

The problem clearly lies in your attempting to call json_encode on something not suited for it:

echo json_encode(array("error"=>0,"rmb"=>$retrievermb,"title"=>$retrievetitle));

What, we may ask, is $retrievetitle? What kind of value is it? Well, we find it in the function definition:

$html = str_get_html($htmlcode);
$title = $html->find('div[class=tb-detail-hd]h3');
return $title[0];

So clearly it is some kind of object. I'm not familiar with the simple_html_dom library, but presumably it's an object that belongs to that library and represents an HTML element. Perhaps it is a native DOMElement object; I don't know.

What is clear, however, is that it is some kind of recursive structure. That is to say, in some sense it contains itself. This is perfectly possible in PHP, but it is impossible to represent in a JSON string. For instance, in PHP:

class Foo {
    public $self;

    public function __construct() {
        $this->self = $this;
    }
}
$foo = new Foo;

$foo->self is the same object as $foo. Indeed, you could do $foo->self->self->self and it would work fine. This is a very simple recursive structure. Yours is probably a bit more complex, but not dissimilar in principle. This can't be represented in JSON. json_encode will error when it encounters recursion.

I imagine you probably wanted to store the text content of the title, rather than the title element itself. Briefly reading the API documentation for the library, it seems you want the plaintext property. I'm not quite sure how this works (the APi is, shall we say, sparse) but my guess would be the following:

return $title[0]->plaintext;

But that is only an educated guess.

like image 143
lonesomeday Avatar answered Oct 02 '22 01:10

lonesomeday