Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

Fatal Error make my php quit right away

Dear experts,
For Error handling, for example, in VBA, we can have "On Error Resume Next" or "On Error Goto" to
let the program still in running even the error happen at exact line number.
In Javascript, that is also easily to understand the error handling from try and catch{}, if error happen
in try {} and then goto catch();

But for my php I am trying using try and catch{} that is NOT exactly same as method as in javascript or VBA handling.
I get  an error message from the following php code   : Fatal error: Call to a member function
getElementByTagName()
on a non-object  on line 10 ,which is exact same line at $data= $dom->getElementsByTagName('table')->item(4)->getElementsByTagName('div')->item(21)>nodeValue;
In my code, I try to access the remote-site with $c varaible to program the $url, there is 10 pages I need to
grab the data, if one of fail, I will re-do it until 5 times is fail.

I don't care the fatal error but it will halt the program running or suspend everything , Why I don't care
because I know it is remote-site server is  busy only  to handle my request of LoadHTMLFile().

Now I put try and catch {} to let it re-do again for 4 more times once fatal error happen, But
it still get the same line ans same fatal error message and quit the program right away, in other words,
it never go to catch{}.

Please advise, how to let the following code running even fatal-error because I can re-do 4 more time and then
quit the program. I know at least  one time success on LoadHTMlfile() work in 5 trial.

There is a lot of  Error handling at http://php.net/manual/en/function.set-error-handler.php,
I don't how it works becuase I know once the fatal-error happen, the program will quit so how to
do any if or condition check for the error happen location.


Duncan




<?php
For ($k=0; $k <4; ++$k)  {  //repeat loadhtmlfile() over again if one of $c is not loaded
try {
for ($c=0; $c < 10; ++$c) {
$url= 'http://www.othersite.com/ex.aspx?symbol='.$c;
$dom = new DOMDocument();
$dom->loadHTMLFile($url);
echo "start=".$c;
$data= $dom->getElementsByTagName('table')->item(4)->getElementsByTagName('div')->item(21)>nodeValue;
echo "Sucess to pass getElementsByTagname".$c;
}
$k=4;//It means no need to do re-do since no fatal error during loadHTMlfile for 10 pages
}
catch(err) {
echo "It found error at =" %c;
}
}
?>

Open in new window

0
duncanb7
Asked:
duncanb7
6 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Fatal error: Call to a member function getElementByTagName() on a non-object  

the part in bold is the important part. at least at some level of the call, there is a non-object ...
either of these is returning a non-object (aka empty or null):
$dom
$dom->getElementsByTagName('table')
$dom->getElementsByTagName('table')->item(4)
$dom->getElementsByTagName('table')->item(4)->getElementsByTagName('div')
$dom->getElementsByTagName('table')->item(4)->getElementsByTagName('div')->item(21)

so, you have 2 options:
get every level apart, and check every level, or "ignore" errors

I would go like this:
$data = "";
$d1 = $dom->getElementsByTagName('table');
if (!$d1)
{
  $d2 = $d1->item(4);
  if (!$d2)
  { 
     $d3 = $d2->getElementsByTagName('div')
     if (!$d4)
     {
        $d5 = $d4->item(21);
        if (!$d5) { $data = $d5->nodeValue; }
     }
   }
} 

Open in new window



0
 
duncanb7Author Commented:
Sorry, the $c  from 0 to 9, for example, $c=6 , is not valid that is I intended to try make fatal error to test
try and catch {} to see it works or not for try and catch{}, Actuall, there is no such page of $c=6 that I already
know it
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
i see,

please put the try{} inside the loop
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hernst42Commented:
Fatal errors in php can not be catched as they are fatal and cause an immediate aboirt of the php script.
The dom extension itself is not designed around the new features of php 5 like exceptions. So it would be cool if the dom extension would throw an exception if an element does not exists instead of reaturn a null value. So you need to code this by yourself:


$tabitems = $dom->getElementsByTagName('table');
if (! ($item = $tabitems->item(4)) instanceof DOMNode)) {
    throw new Exception();
}
$divitems = $itme->getElementsByTagName('div')
if (! ($item = $divitems->item(21)) instanceof DOMNode)) {
    throw new Exception();
}
return $item->nodeValue;

Open in new window

0
 
duncanb7Author Commented:
Thanks for your reply, I change it is still fail because I intend to
go to the wbesite that is not existied for $c=6 in oder to test
the try and catch {} so I get fatal error is Normal and Accepted

But let the fatal error  and go to catch {] so that we can re-do it
until 5 times and quit the program .
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
0
 
duncanb7Author Commented:
Yes, you are correct, once fatal error happen, script will abort, whatever you is no use, Right ?
For example, if $tabitems = $dom->getElementsByTagName('table') will happen
fatal error, the other code like if condition checking is not running, Right,
So, in other words, the only way to solve the issue , it is "don't let have fatal Error Happen ", Right ?


$tabitems = $dom->getElementsByTagName('table');
if (! ($item = $tabitems->item(4)) instanceof DOMNode)) {
    throw new Exception();
}


So, in google seach, people are talking error handling or exception that is NOT fatal error, Right,
otherwise how to handle.?
0
 
duncanb7Author Commented:
So, from the article, I don't quit understand,

If it is fatal error, whatever you do the code or shutdown  funcation that is no use because
once fata error, php script abort, Right ?

I also pyt try{} in the loop, it still fail I think because of the same reason.
So it is related to Linux Adm system issue ? Should let users to catch the error ?
0
 
Ray PaseurCommented:
This is pretty unambiguous: Call to a member function getElementByTagName() on a non-object on line 10.

You would want to know what created the data you're using on line 10.  You can test to see if the data is an object with this function:
http://us3.php.net/manual/en/function.is-object.php

Probably, either something failed to create the object (and returned FALSE) in an earlier instruction, or the data you've got does not have an object at the index level you've addressed.  In either case, the right thing to do is program so that no fatal error occurs, and that means testing the data for the correct type before using it.

Here is a microcosm example of how the error happens.  Note that this is different for non-arrays and non-objects.
<?php // RAY_expected_error.php
error_reporting(E_ALL);

// CREATE A NON-OBJECT (THIS IS A STRING)
$foo = 'bar';

// WARNING IF WE TRY TO USE WITH AN ITERATOR
foreach ($foo as $lump)
{
   /* STUFF */
}
// FATAL ERROR IF WE TRY TO USE A NON-OBJECT WITH OOP NOTATION
$thing = $foo->baz();

Open in new window

0
 
duncanb7Author Commented:
Corect what I said, The code you provide  is help and the fatal error is gone, and
I believe loadHTMLfile() is no issue for non-exist webpage because it still has header even content is
empty that won't create fatal error.

If avoid, the fatal error and re-run,I will consider using  Linux c script to control php program like bat file
in window.
 
$data = "";
$d1 = $dom->getElementsByTagName('table');
if (!$d1)
{
  $d2 = $ d1->item(4);
  if (!$d2)
  {
     $d3 = $d2->getElementsByTagName('div')
     if (!$d4)
     {
        $d5 = $d4->item(21);
        if (!$d5) { $data = $d5->nodeValue; }
     }
   }
}
0
 
duncanb7Author Commented:
Dear angelIII,

Is (!$d1) or ($d1), since it is passed as i said  a minute ago because it never go into if condition.
and I put echo statement , it never echo so it pass fatal evero but output for $data is empty
! sign should check it is null variable
Please advise
Duncan

$d1 = $dom->getElementsByTagName('table');
if (!$d1)
{

echo "Testing*****************";
  $d2 = $ d1->item(4);
  if (!$d2)
  {
     $d3 = $d2->getElementsByTagName('div')
     if (!$d4)
     {
        $d5 = $d4->item(21);
        if (!$d5) { $data = $d5->nodeValue; }
     }
   }
} Accept and Award Points Accept as Solution
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry... my fault. it must be if ($d1) and not if (!$d1) ... same for the other tests.
0
 
duncanb7Author Commented:
Thanks for all of your reply,
It might go to the new thread
for more fatal-error testing
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now