How to handle Php Error message and not display it on Web page

Hello,

We have a code for reading Meta data of a Web site URL. in 95% of the cases it works just fine, but in 5% of cases it produces an Error which is:s:

Notice: Trying to get property of non-object in /var/www/html/anoox.com/temp/auto_read-site.php on line 34

In these cases, we would like to capture such Error message rather than have the Web page break with this Php Error message and in these cases just tell the user that we found no data, etc.

The offending code is:

$title = $nodes->item(0)->nodeValue;

My question is simply, in cases that the code cannot get the Meta data of the provided URL how do we either not exeeccute this line and assume
Title
cannot be found or just assume that no data can be found about the given Web site and in of course produce NO error message to the users on their web browser?

Thanky,

Try this:

<?php 
ini_set('error_log', '/logs/specific_errors.php');
error_reporting( -1 ); // maximum errors, warnings, etc
ini_set( 'display_errors', false); //  Save in log BUT not Show on Screen

$title = $nodes -> item(0) -> nodeValue;
$title = empty( $title ) 
       ? 'Display Not found Message'
       :  $title; 
if($nodes->item(0)->nodeValue != NULL and !empty($nodes->item(0)->nodeValue)){
    $title = $nodes->item(0)->nodeValue;
}else{
    $title = "No title";
}

Does that do what you need?

schwim,

with your code added, I am still getting same type error message. Here:

Notice: Trying to get property of non-object in /var/www/html/anoox.com/temp/auto_read-site.php on line 32 Title: No title

Hey John,

This code works fine.

Just one question: in cases that our Meta Indexing code fails to get a proper result from the Web site it wants to Index, we would like our code to wait a maximum of 10 seconds and give up after that. So what do we need to add to this code to do this?

Thanky

1 Like

You could use:

sleep(10);

which would “pause” the execution of the script for 10 seconds then have it try again

1 Like

Hi,

That is not what we are looking for. Since that will Sleep for 10 seconds in every case, and we want the code to try to get the answer in 10 Secs and if it cannot then give up. Keeping in mind that in 99% of the cases, domains, the code does get the answer in under 10 seconds, actually gets the answer in like 1 sec, but in 1% of the cases of bad designed Web sites, then anything can happen in which case we want the code to give up after 10 secs.

Maybe have a $start_time and $current_time ?
if ( ($current_time - $start_time) > 10) { return; }

I dont think that will work.
Since Php will wait until process X has finished running before it will run the line, which can be the line that you have proposed. So for this to work the timer function needs to be wrapped around the process that is being run. So something like:

time_out(get_url_values($url), 10);

where get_url_values is the function that we are calling and we are telling Php stop trying to execute this function and return null if no reply has come back in 10 secs.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.