0

I want source code of user's entered URL page. We can get it using file_get_contents.

But while using file_get_contents it gives:

Warning: file_get_contents(http://www.google.com): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /var/www/html/scrap/yelp/simple_html_dom.php on line 75

If I open same url from browser, it opens correctly. How google people get that I have made query using file_get_content or from browser?

Update: I tried with curl also, but still same error

user123
  • 5,269
  • 16
  • 73
  • 121

3 Answers3

1

My suggestion is the site you want to parse has blocked unwanted requests, try to use the cUrl library.

biolarnative
  • 188
  • 12
1

try CURL with useragent

function get_web_page( $url ){

    $options = array(
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_HEADER          => false,
            CURLOPT_FOLLOWLOCATION  => true,
            CURLOPT_USERAGENT       => "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0",
            CURLOPT_SSL_VERIFYPEER  => false,

        );

        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $content = curl_exec( $ch );
        curl_close( $ch );

        return $content;
}

echo get_web_page("http://www.google.com");
  • it gives me this error : `Sorry, you're not allowed to access this page. Your IP address is: x.x.x.x` (*my server IP) – user123 May 05 '14 at 10:55
  • I think google somehow know you are doing it by php - maybe you need to tweek headers sent by cURL so they wouldnt look so php like. – Seti May 05 '14 at 11:00
  • I think you have recently sent too many automated requests. Google might have temporarily blocked your server ip. try later. – alreadycoded.com May 05 '14 at 11:01
0

Hey it will work fine problem is that your code is wrong.

file_get_contents('http://www.google.com');

Use this it will work. If any other help you want just let me know.

Shivam
  • 702
  • 2
  • 10
  • 25
  • Shivam: I used same code, no issue with code. it's regarding bypass the blocking – user123 May 05 '14 at 10:52
  • run this code in this website you will come to know http://www.compileonline.com/execute_php_online.php – Shivam May 05 '14 at 10:54
  • dude, file_get_contents works fine. Issue is my server IP is blocked by google. So now do you have any solution – user123 May 05 '14 at 10:57