json_en code给递归误差 [英] json_encode giving recursion error

查看:134
本文介绍了json_en code给递归误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是什么错误,我似乎无法来解决这个问题。它的每产生500错误遇到什么大不了的日志。大小1.33个字节。它的垃圾邮件日志到存储器中最大的。

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

?>

我想从一个HTML文件中提取数据,其他的提取工作正常,除了上述情况,给予很多问题。我甚至分开的这一套code专为处理与同一问题的一个PHP文件。

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.

你知道吗?我使用jQuery阿贾克斯在家里页面 $多种功能。AJAX({
我是新来的Ajax,这是正常的有多个阿贾克斯在一个页面吧?

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?

推荐答案

问题显然出在你试图调用 json_en code 的东西不适合它

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

什么,我们可能会问,是 $ 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];

所以很明显这是某种形式的对象。我不熟悉的 simple_html_dom 库,但是presumably这是属于该库并重新presents HTML元素的对象。也许这是一个原生 DOMElement 对象;我不知道。

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.

什么是明确的,但问题在于,它是某种递归结构。这就是说,在某种意义上它包含本身。这是完全可能在PHP,但不可能重新present在JSON串。例如,在PHP中:

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-&GT;自是相同的对象 $ FOO 。事实上,你可以做 $ foo-&GT;自&GT;自&GT;自键,它会正常工作。这是一个非常简单的递归结构。你可能是一个有点复杂,但在原则上没有什么不同。这不可能是psented在JSON重新$ P $。 json_en code 当它遇到递归会报错。

$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.

我想你可能想存储的称号,而不是标题元素本身的文本内容。简而言之阅读 API文档库,看来你想要的明文属性。我不太清楚这是如何工作(的API是,我们应当说,稀疏的),但我的猜测是以下内容:

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.

这篇关于json_en code给递归误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆