简体   繁体   English

CURL PHP 试图获取非对象的属性

[英]CURL PHP Trying to get property of non-object

I'm trying to parse an html output that i get through CURL using simple html dom, but I get this error:我正在尝试使用简单的 html dom 解析通过 CURL 获得的 html 输出,但出现此错误:

Trying to get property 'innertext' of non-object试图获取非对象的属性“内部文本”

This is the relevant code:这是相关代码:

$output = curl_exec($ch);

if($output === FALSE) {
    echo "cURL Error: " . curl_error($ch);
}

curl_close($ch);

$html = str_get_html($output);
$kungtext_class = $html->find('div[class=kungtext]');
$kungtext = $kungtext_class->innertext;

echo $kungtext;

Output variable is the gathered HTML in text-form that I get from CURL.输出变量是我从 CURL 获得的以文本形式收集的 HTML。

Updated answer更新答案

$kungtext_class is giving you an array , you can't access the property because you get a bunch of elements, not only one. $kungtext_class为您提供了一个array ,您无法访问该属性,因为您获得了一堆元素,而不仅仅是一个。

See the docs http://simplehtmldom.sourceforge.net/manual_api.htm请参阅文档http://simplehtmldom.sourceforge.net/manual_api.htm

mixed find ( string $selector [, int $index] )

Find elements by the CSS selector.通过 CSS 选择器查找元素。 Returns the Nth element object if index is set, otherwise return an array of object.如果设置了索引,则返回第 N 个元素对象,否则返回对象数组

So your code should be like所以你的代码应该像

foreach ($html->find('div[class=kungtext]') as $kungtext_class) {
    echo $kungtext_class->innertext;
}

Or, access index 0 (first element):或者,访问索引0 (第一个元素):

$kungtext_class = $html->find('div[class=kungtext]', 0);
$kungtext = $kungtext_class->innertext;

Old answer旧答案

curl_exec() by default returns a boolean . curl_exec()默认返回一个boolean

You need to set CURLOPT_RETURNTRANSFER in the curlopts, it then returns the expected string (on success).您需要在 curlopts 中设置CURLOPT_RETURNTRANSFER ,然后返回预期的字符串(成功时)。

// Before the curl_exec():
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

http://php.net/manual/en/function.curl-exec.php http://php.net/manual/en/function.curl-exec.php

Returns true on success or false on failure.成功时返回true ,失败时返回false However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, false on failure.但是,如果设置了CURLOPT_RETURNTRANSFER选项,它将在成功时返回结果,在失败时返回false

From the documentation :从文档:

mixed find ( string $selector [, int $index] )

Find children by the CSS selector.通过 CSS 选择器查找子项。 Returns the Nth element object if index is set, otherwise, return an array of object.如果设置了索引,则返回第 N 个元素对象,否则返回对象数组。

so in your case , the returned value is saved in $kungtext_class, the error you have here can be :所以在你的情况下,返回的值保存在 $kungtext_class 中,你在这里的错误可能是:

1 - the find call return an array , so you have to do that : 1 - find 调用返回一个数组,所以你必须这样做:

foreach($kungtext_class as $element){
     echo $element->innertext;
}

2- the find call don't find any element that have the class kungtext so it return null and it's normal that the innertext throw error . 2- find 调用找不到任何具有kungtext类的元素,因此它返回 null 并且 innertext 抛出 error 是正常的。

Your code will work correctly onyl if the find method return only one element from the dom that have the class kungtext .如果 find 方法仅从具有kungtext类的 dom 返回一个元素,则您的代码将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM