FlyingAtom FlyingAtom - 3 years ago 164
Ajax Question

json_encode giving recursion error

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.


if(isset($_REQUEST['type']) && $_REQUEST['type'] = "getmoredetails"){

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

I'm new to Ajax, it's alright to have multiple Ajax in one page right?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download