image_container remains null therefore it throws the error? [duplicate] - php

I am using this library (PHP Simple HTML DOM parser) to parse a link, here's the code:
function getSemanticRelevantKeywords($keyword){
$results = array();
$html = file_get_html("http://www.semager.de/api/keyword.php?q=". urlencode($keyword) ."&lang=de&out=html&count=2&threshold=");
foreach($html->find('span') as $e){
$results[] = $e->plaintext;
}
return $results;
}
but I am getting this error when I output the results:
Fatal error: Call to a member function find() on a non-object in
/var/www/vhosts/efamous.de/subdomains/sandbox/httpdocs/getNewTrusts.php
on line 25
(line 25 is the foreach loop), the odd thing is that it outputs everything (at least seemingly) correctly but I still get that error and can't figure out why.

The reason for this error is: the simple HTML DOM does not return the object if the size of the response from url is greater than 600000.
You can void it by changing the simple_html_dom.php file. Remove strlen($contents) > MAX_FILE_SIZE from the if condition of the file_get_html function.
This will solve your issue.

You just need to increase CONSTANT MAX_FILE_SIZE in file simple_html_dom.php.
For example:
define('MAX_FILE_SIZE', 999999999999999);

This error usually means that $html isn't an object.
It's odd that you say this seems to work. What happens if you output $html?
I'd imagine that the url isn't available and that $html is null.
Edit:
Looks like this may be an error in the parser. Someone has submitted a bug and added a check in his code as a workaround.

Before file_get_html/load_file method, you should first check if URL exists or not.
If the URL exists, you pass one step.
(Some servers, service a 404 page a valid HTML page. which has propriate HTML page structure like body, head, etc. But it has only text "This page couldn'!t find. 404 error bla bla..)
If URL is 200-OK, then you should check whether fetched thing is object and whether nodes are set.
That's the code i used in my pages.
function url_exists($url){
if ((strpos($url, "http")) === false) $url = "http://" . $url;
$headers = #get_headers($url);
// print_r($headers);
if (is_array($headers)){
if(strpos($headers[0], '404 Not Found'))
return false;
else
return true;
}
else
return false;
}
$pageAddress='http://www.google.com';
if ( url_exists($pageAddress) ) {
$htmlPage->load_file( $pageAddress );
} else {
echo 'url doesn t exist, i stop';
return;
}
if( $htmlPage && is_object($htmlPage) && isset($htmlPage->nodes) )
{
// do your work here...
} else {
echo 'fetched page is not ok, i stop';
return;
}

For those arriving here via a search engine (as I did), after reading the info (and linked bug-report) above, I started some code-prodding and ended up fixing my problems with 2 extra checks after loading the dom;
$html = file_get_html('<your url here>');
// first check if $html->find exists
if (method_exists($html,"find")) {
// then check if the html element exists to avoid trying to parse non-html
if ($html->find('html')) {
// and only then start searching (and manipulating) the dom
}
}

I'm having the same error come up in my logs and apart from the solutions mentioned above, it could also be that there is no 'span' in the document. I get the same error when searching for divs with a particular class that doesn't exist on the page, but when searching for something that I know exists on the page, the error doesn't pop up.

your script is OK.
I receive this error when it doase not find the element that i'm looking for on that page.
In your case, please check if the page that you are accessing it has 'SPAN' element

Simplest solution to this problem
if ($html = file_get_html("http://www.semager.de/api/keyword.php?q=". urlencode($keyword) ."&lang=de&out=html&count=2&threshold=") {
} else {
// do something else because couldn't find html
}

Error means, the find() function is either not defined yet or not available. Make sure you have loaded or include related function.

Related

PHP - Simple HTML DOM Parser - Getting FATAL ERROR when html is OK

I am trying to parse a table and output the a plaintext in another table. I have gotten this far:
<?php
if (url_exists($url))
{
$html = file_get_html($url);
}
else
{
echo "URL doesn't exist.";
}
if ($html && is_object($html) && isset($html->nodes))
{
// Everything checks out
$table = $html->find('table[border]');
if (!empty($table))
{
$row = $table->find('tr');
}
}
else
{
echo "Fetched page is not ok.";
}
?>
This returns an error:
Fatal error: Call to a member function find() on a non-object in /var/www/html/jsudimak/mailman/webdev-test1.php on line 78
Line 78 is this one: $row = $table->find('tr');
This means that :
the html is valid
the table I am trying to parse is also valid
Therefore, I am bewildered by the fact that the find() method is still returning this error.
I have looked into the cause of this error extensively for the past few days and I have yet to find a solution. I have also tried some other parsing tools no still no luck. Help me with this fellow debuggers!!!!
By the way, I am using the Simple HTML Dom Parser to parse the table.
Use $table = $html->find('table[border]')[0];
The documentation says that, unless you specify an index in the function find(), it will return an array

How to detect element is exist or not

Hello everyone i am fetching data by using simple html dom
This is my code of php which is fetching data from site
include('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file($this->main_url.$lin->link);
if($html){
//check if language heading h2 exist then process forward
if($html->find('h2.channel-title',0)){
fetch data from tables
}
}
This line if($html->find('h2.channel-title',0)) finding h2.channel-title in find function of simple html dom give me a fatal error when h2.channer-title is not exist
In many pages <h2 class="channel-title"> English Links</h2> exists so i have code according to them and process further in my foreach loop it's working fine and fetched all data.
But
when <h2 class="channel-title">English Links</h2> tag is not exist it give me an error
Fatal error: Call to a member function find() on a non-object in C:\xampp\apps\wordpress\htdocs\wp-content\plugins\autobot\engine\simple_html_dom.php on line 1113
Please help me i am stuck in it need help thank you. i want if h2.channel-title exist run my foreach code else run another but don't give an error its stop my whole script. :(
this might help.
$html = new simple_html_dom();
$html->load_file($this->main_url.$lin->link);
if($html) {
$var = $html->find('h2.channel-title',0);
if(isset($var)) {
fetch data from tables
} else{
//do something
}
}
var_dump($html);
Which library you are using?

SimpleHtmlDOM, PHP, Fatal Error: Call to a member function find() on a non-object in C:\xampp\htdocs [duplicate]

I am using this library (PHP Simple HTML DOM parser) to parse a link, here's the code:
function getSemanticRelevantKeywords($keyword){
$results = array();
$html = file_get_html("http://www.semager.de/api/keyword.php?q=". urlencode($keyword) ."&lang=de&out=html&count=2&threshold=");
foreach($html->find('span') as $e){
$results[] = $e->plaintext;
}
return $results;
}
but I am getting this error when I output the results:
Fatal error: Call to a member function find() on a non-object in
/var/www/vhosts/efamous.de/subdomains/sandbox/httpdocs/getNewTrusts.php
on line 25
(line 25 is the foreach loop), the odd thing is that it outputs everything (at least seemingly) correctly but I still get that error and can't figure out why.
The reason for this error is: the simple HTML DOM does not return the object if the size of the response from url is greater than 600000.
You can void it by changing the simple_html_dom.php file. Remove strlen($contents) > MAX_FILE_SIZE from the if condition of the file_get_html function.
This will solve your issue.
You just need to increase CONSTANT MAX_FILE_SIZE in file simple_html_dom.php.
For example:
define('MAX_FILE_SIZE', 999999999999999);
This error usually means that $html isn't an object.
It's odd that you say this seems to work. What happens if you output $html?
I'd imagine that the url isn't available and that $html is null.
Edit:
Looks like this may be an error in the parser. Someone has submitted a bug and added a check in his code as a workaround.
Before file_get_html/load_file method, you should first check if URL exists or not.
If the URL exists, you pass one step.
(Some servers, service a 404 page a valid HTML page. which has propriate HTML page structure like body, head, etc. But it has only text "This page couldn'!t find. 404 error bla bla..)
If URL is 200-OK, then you should check whether fetched thing is object and whether nodes are set.
That's the code i used in my pages.
function url_exists($url){
if ((strpos($url, "http")) === false) $url = "http://" . $url;
$headers = #get_headers($url);
// print_r($headers);
if (is_array($headers)){
if(strpos($headers[0], '404 Not Found'))
return false;
else
return true;
}
else
return false;
}
$pageAddress='http://www.google.com';
if ( url_exists($pageAddress) ) {
$htmlPage->load_file( $pageAddress );
} else {
echo 'url doesn t exist, i stop';
return;
}
if( $htmlPage && is_object($htmlPage) && isset($htmlPage->nodes) )
{
// do your work here...
} else {
echo 'fetched page is not ok, i stop';
return;
}
For those arriving here via a search engine (as I did), after reading the info (and linked bug-report) above, I started some code-prodding and ended up fixing my problems with 2 extra checks after loading the dom;
$html = file_get_html('<your url here>');
// first check if $html->find exists
if (method_exists($html,"find")) {
// then check if the html element exists to avoid trying to parse non-html
if ($html->find('html')) {
// and only then start searching (and manipulating) the dom
}
}
I'm having the same error come up in my logs and apart from the solutions mentioned above, it could also be that there is no 'span' in the document. I get the same error when searching for divs with a particular class that doesn't exist on the page, but when searching for something that I know exists on the page, the error doesn't pop up.
your script is OK.
I receive this error when it doase not find the element that i'm looking for on that page.
In your case, please check if the page that you are accessing it has 'SPAN' element
Simplest solution to this problem
if ($html = file_get_html("http://www.semager.de/api/keyword.php?q=". urlencode($keyword) ."&lang=de&out=html&count=2&threshold=") {
} else {
// do something else because couldn't find html
}
Error means, the find() function is either not defined yet or not available. Make sure you have loaded or include related function.

file_get_html returning empty page

I am using simple html dom parser but when I use file_get_html(), it returns empty page, but page is note empty you can check by opening in browser. Here is my code
include"11/simple_html_dom.php";
$link = "http://www.flipkart.com/transcend-storejet-25m3-2-5-inch-1-tb-external-hard-disk/p/itmd72p3y3zcsbku? pid=ACCD72ZXFC6ZRTST&srno=b_1&ref=549d7873-2897-4bd5-8451-776337341be8";
$html = file_get_html($link);
if(!empty($html)){
echo $html->find("span.fk-font-verybig") ;
}
else{
echo 'file is empty';
}
Any help would be appreciated.
Try this:
in your simple_html_dom.php
Edit this line define('MAX_FILE_SIZE',600000); to define('MAX_FILE_SIZE',900000); or even more according to the size of your file.
This sometimes occurs when your Html file size is larger then what is define so it returns empty without any errors.
I hope it works.
Instead of echo $html->find("span.fk-font-verybig");
try echo reset( $html->find("span.fk-font-verybig") );

Check element exists using PHP Selenium 2 Webdriver?

I am trying to check if an element on a page exists by CSS using Selenium 2. Anyone have any examples using PHP Selenium Webdriver Facebook wrapper?
I have tried the below code:
if($driver->findElement(WebDriverBy::xpath("image-e4e")) != 0)
{
}
But gives me this error:
Fatal error: Uncaught exception 'NoSuchElementWebDriverError' with
message 'Unable to locate element:
{"method":"xpath","selector":"image-e4e"}
By design, findElement returns the WebDriverElement if it is found but it throws an exception when it is not found.
To check whether the element is on the page without getting an exception, the trick is to use findElements. findElements returns an array of all elements found on the page. It returns an empty array if nothing is found.
if (count($driver->findElements(WebDriverBy::xpath("image-e4e"))) === 0) {
echo 'not found';
}
Use: $driver->findElements intstead of findElement
findElements will return an empty array if element not exists and will not throw an exception.
You can also use the sizeof() builtin function in PHP:
$check = $driver->findElements(WebDriverBy::xpath('image-e4e'));
if (sizeof($check) > 0) {
echo "success";
}
OR can also be used to count() function:
if (count($driver->findElements(WebDriverBy::xpath('image-e4e'))) > 0) {
echo "success";
}
Output: If XPath element is available on a page then it will give you success
If you want to use findElement then:
$checkXpath = 'image-e4e';
$checkXpath = $this->findElementByXpath($driver, $checkXpath);
$check = $driver->findElement($checkXpath);
if (count($check) > 0) {
echo "success";
}
OR
$checkXpath = WebDriverBy::xpath('image-e4e');
$check = $driver->findElement($checkXpath);
if (count($check) > 0) {
echo "success";
}
Output: If XPath element is available on a page then it will give you success
Note: The findElement method throws a NoSuchElementException exception when the element is not available on the page. Whereas, the findElements method returns an empty list when the element is not available or doesn't exist on the page.
That's because your xpath is bad.
If you want to continue using xpath, then your seletor would be
//*[#id='image-e4e']
If you have an open mind, get used to CSS Selectors. They are faster and much more readable
WebDriverBy::cssSelector("#image-e4e")
This is of course assuming that the ID is image-e4e. The reason your xpath was failing was because xpath was attempting to locate an immedate child with tag name image-e4e. you want to parse the entire pom for an element with an attribute that equals image-e4e. Whether that's ID or name, I do not know.
if($driver->findElement(WebDriverBy::id("image-e4e")) != 0)
{
}
Try the above logic I am sure definitely it will work out.

Categories