weird html parser error Call to a member function find() - php

I have this error when parsing html
my code is like this
$html = new \simple_html_dom();
$html->load_file($url);
$type = gettype($html);
$this->output->writeln("check point1:" . $type); // it says 'object'
$class = get_class($html);
$this->output->writeln("check point2:" . $class); // it says 'simple_html_dom'
foreach($html->find('a') as $element){
//do something here.
}
then this code shows this error sometims.
PHP Fatal error: Call to a member function find() on a non-object
error
even when error happens $html is correctly fetched (I check in two check points)
This error is similar to this article though, no slution is revealed.
Please help me

Related

Fatal error: Call to undefined function drupal_get_path()

After updating our server to PHP 5.4 and a Drupal site to Drupal 6.37 I keep getting this error message and I am not able to get past it
Fatal error: Call to undefined function drupal_get_path() in /home/mysite/public_html/includes/module.inc on line 285
this is the related function call
function module_load_include($type, $module, $name = NULL) {
if (empty($name)) {
$name = $module;
}
$file = './'. drupal_get_path('module', $module) ."/$name.$type";
if (is_file($file)) {
require_once $file;
}
else {
return FALSE;
}
}
Even when I try to run update.php I still get the same error
I am new to Drupal so any help will be greatly appreciated
Please let me know what additional info you may need since it is my first post here and as I said I don't know much about Drupal

Error : Fatal error: Call to a member function escape_string() on a non-object

I am getting the error:
"Fatal error: Call to a member function escape_string() on a non-object" It says this error appears on line 214 of my code. Below you can see that code.
// Check description, more than 0 characters
if (isset($_POST['description'])) {
if (strlen($_POST['description']) > 0) {
$description = $conn->escape_string($_POST['description']);
} else {
$errors .= "desc|";
}
} else {
$errors .= "desc|";
}
The line of code below is the specific offending code according to the error message
$description = $conn->escape_string($_POST['description']);
Any idea what may be causing this error?
Thanks
Yeah, you're either looking for mysqli_real_escape_string or add_slashes or html_entity_decode, depending on what you want to achieve next.

How to avoid 'on a non-object in ***' error

I am using simple_html_dom like this
$html = new \simple_html_dom();
$html->load_file($url);
$html->find('a')
then sometims this error happens
Fatal error: Call to a member function find() on a non-object in /src/Acme/TopBundle/Command/simple_html_dom.php on line 1146
its OK. I think,it might happen that load_file fails to get contents of url;
but,I want to pass throw this error and continue process.
so I changed the script like this .
$html = new \simple_html_dom();
$html->load_file($url);
if (!$html){
return null;
}
$html->find('a')
but it still returns error and stop.
how can I pass throw this error?
Use is_object:
$html = new \simple_html_dom();
$html->load_file($url);
if (!is_object($html){
return null;
}
$html->find('a')
You can also do a defined check and use the gettype.
<?php
require('simple_html_dom.php');
$html = new \simple_html_dom();
$html->load_file('http://www.bensoft.com/');
if (defined($html) && gettype($html) == 'object') {
$html->find('a');
}
?>

Why am I getting errors thrown on PHP code wrapped in a try-catch?

<?php
try {
$attrs = $xml->attributes();
$code = $attrs['_Code'];
}
catch (Exception $e)
{
$code = '';
}
?>
Gets me:
Fatal error: Call to a member function attributes() on a non-object on
line 6
Why am I getting errors thrown on code wrapped in a try-catch??
NOTE: It is possible to avoid this error by using the following code. (The question is not about avoiding the error, but why it's not being caught- still I thought I'd share the non-erroring code anyway)
if (is_object($xml) && method_exists($xml,'attributes')) {
$attrs = $xml->attributes();
$code = !empty($attrs['_Code'])?$attrs['_Code']:'';
}
else {
$code = '';
}
PHP fatal errors cannot be caught. I don't know the specifics of what you're doing, but you'll have to figure out some other way to test whether $xml->attributes() will work or not.
Also, swallowing every error and not logging it anywhere is bad practice because when stuff starts breaking you won't have any idea of why.
try/catch only works for exceptions, not parse errors. You have to test to make sure that $xml has an attributes method to avoid such an error (could be called a null pointer, but not exactly).

PHP: Error in Error Checking

if($xml->getElementsByTagName($elmnt) && $xml->getElementsByTagName($elmnt)->length > 0)
This line is intended to check for errors. All I want is to, instead of breaking the entire page, make a legible error message. It is included in a function designed to stop all related processes on failure and continue displaying the rest of the page if it doesn't work, since the page layout does not depend on whether or not this function succeeds.
Fatal error: Call to a member function getElementsByTagName() on a non-object in file.php on line 100
How do I actually check to make sure that the DOMDocument in question has the element without it throwing the error above? I've tried using just the first condition or the second condition.
var_dump($xml);
object(DOMDocument)#3 (1) {
["preserveWhitespace"]=>
bool(false)
}
If you use simplexml_load_file to load the file, it will either return with a successfull load or it will return false on failure. The error you are getting signifies that the variable $xml is not an object, which means that when you tried to load the xml file onto the variable, it didn't do it successfully.
If before you do the getElementsByTagName($elmnt) you make sure $xml is not false, you shouldn't get the error Fatal error: Call to a member function getElementsByTagName() on a non-object in file.php on line 100
if (file_exists('test.xml')) {
$xml = simplexml_load_file('test.xml');
if($xml){
print_r($xml);
}
} else {
exit('Failed to open test.xml.');
}
Your problem is that $xml is not an object, you have to check that first:
if ($xml && ($obj = $xml->getElementsByTagName($elmnt)) && $obj->length > 0) {
// you should be good as this point
}
You can also use !empty($xml) instead of just $xml if there is a possibility that $xml is undefined.

Categories