0
votes

I'm working on a project which is simply scrapping the comments of youtube but the condition is not to use cURL. So i started using simple html dom.php. I almost fought with many problems and now that are fixed. But i have a query that how to set an error for file_get_html() method. For example: I'm allowing user to enter the url of a youtube video, i have almost done a lot of validating of youtube URL, but still if someone will enter a wrong URL then the script will report this error: *Warning: file_get_contents(http://www.youtube.com/watch?v=VR--anQO?????222cV--clS8): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\xampp\htdocs\utube\simple_html_dom.php on line 39*

and i can't set error reporting to false as it's not allowed. So can you please say me how to display this error: Invalid URL instead of the error shown above. And yes please don't say about the youtube API as it's not allowed :(

1

1 Answers

0
votes

The easiest way would be to use an @. Just use it like this:

if (@file_get_html(...)) {
    //do whatever you want, if it suceeds
} else {
    echo "Invalid URL";
}

You can find more information here.

As simple_html_dom is a class, that should be the reason why the @ has no affect, as the class don't seem to regard it.

A thing worth a try: First try using file_get_contents, and only use simle_html_dom, if that works - like this:

if (@file_get_contents(...)) {
    file_get_html(...);
    //now anything else you want to do
} else {
    echo "Invalid URL";
}