-1

I'm using simple_html_dom which is a predefined webcrawler class with various methods.

I have the following:

$html = new simple_html_dom();

$arrayoflinks = //this is where I have a list of links//;

foreach($arrayoflinks as $eachlink){
    $html->load_file($eachlink);   //these are methods from the simple html_dom
    $html->find('a'); //these are methods from the simple html_dom
    //run a function I already wrote
}

The issue is that the $html in the foreach loop is not being recognized. My Netbeans IDE is telling me that the $html in the foreach loop is introducing a new variable, which implicitly means it's not being recognized as the class method.

How can I get around this?

EDIT: Turns out the error was something else. Accessing the method in the above foreach loop is valid.

Terence Chow
  • 10,755
  • 24
  • 78
  • 141
  • 2
    Forget what your IDE is telling you, did you try it? what error did you get? – Adi Aug 04 '12 at 19:41
  • This code should be valid. Are there any { } around the $html? Also, please check if $html returns a valid object by using var_dump. – ATaylor Aug 04 '12 at 19:42
  • *I have abdicated my brain to my IDE and am [not wearing pants](http://mikearauz.tumblr.com/post/247676543/something-missing-by-shel-silverstein-i-remember)*. Or something. – Jared Farrish Aug 04 '12 at 19:42
  • 1
    ah come on, there is some comfort with code completition, no need to fight for or against that here. This is either a problem with the Netbeans IDE or with the source code of `simple_html_dom`. I personally would prefer something that is based on `DOMDocument` under the hood instead. See [How to parse and process HTML with PHP?](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php) for more information. – hakre Aug 04 '12 at 19:44
  • there aren't any {} around my html, the error I get is 'Maximum execution time of 30 seconds exceeded in simple_html_dom'...I was assuming the max 30 seconds was reached cause it wasn't recognizing the $html variable – Terence Chow Aug 04 '12 at 19:45
  • Oh, you are just doing too much, so it takes too long and then you run into a time limit. That's all. Just do less so that you do not run into that limit. – hakre Aug 04 '12 at 19:46
  • And there's your error. It takes the framework too long to process the list and it aborts execution. To get around 'this', you either need to reduce the list of URLs to parse, or increase the maximum execution time in the php.ini – ATaylor Aug 04 '12 at 19:46
  • It could be, due to `$html->load_file()`. Note, execution time can at times be surprising. – Jared Farrish Aug 04 '12 at 19:46
  • OHHHHHHHHHHHH HAHAHAHA that was my problem? lol – Terence Chow Aug 04 '12 at 19:47
  • thanks all! first person to give any answer gets the checkmark? – Terence Chow Aug 04 '12 at 19:47
  • That depends entirely on you, but you should accept an answer, so people will know, that your question is answered. – ATaylor Aug 04 '12 at 19:49

2 Answers2

1

After the OP posted the error message, once more as answer:

PHP has a default execution time for scripts. Once this runtime is over, the script aborts with an error message.

Either increase the maximum runtime in php.ini (don't forget to restart your webserver), or reduce the amount of things the script has to do (for example by reducing the amount of URLs to parse).

ATaylor
  • 2,598
  • 2
  • 17
  • 25
  • 1
    Also DOMDocument works faster than simple html dom. Probably it helps already if you switch the library. – hakre Aug 04 '12 at 20:32
0

I pasted your code in PHPStorm and it's not giving any kind of errors about $html not being accessible from inside the foreach loop. I'd say it's a Netbeans bug - the code appears valid. Run it and it should work.

Chris Clower
  • 5,036
  • 2
  • 18
  • 29