softleaks
softleaks

Reputation: 137

file_get_contents not working for my domain

I tried to parse a website's html using file_get_contents().

I ran the code from my website.

It worked fine at first.But suddenly this error appeared:

Warning: file_get_contents(http://www.***.com/) [function.file-get-contents]: failed to open stream: HTTP request failed!

I tried other websites and it works fine.I ran the code from my other website and it works fine.

So I searched here and used the cURL code:

$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://www.***.com/');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);

But it just returns a blank page.

Looks like the website I tried to get contents blocked or blacklisted my website domain or something.

Do I have a way out of this ??

Upvotes: 1

Views: 903

Answers (2)

Ludovit Scholtz
Ludovit Scholtz

Reputation: 84

your server cannot get to the other server

possibly dns issues, misspelling or the other server has blocked your ip..

what does curl_error() says?

BTW, CURLOPT_CONNECTTIMEOUT 2 seconds is usually quite small

Upvotes: 1

Halcyon
Halcyon

Reputation: 57709

You could try to figure out why your connection is being blocked:

  • IP based: they blocked your server.
  • User Agent based: send a user agent that identifies you as a bot.
  • If they don't like your bot user agent pretend to be someone else's bot, like Google (evil)
  • If they block bots, send a user agent that looks like a browser (evil)
  • Some other parameter that identifies a browser vs a script.

Upvotes: 3

Related Questions