View Full Version : File get contents is blocked - how can I get around this?

04-21-2011, 08:27 PM

I'm running scripts on some sites and until now everything is fine. I tried to perform a file_get_contents on this page http://www.superpages.com/bp/New-York-NY/Martin-A-Kron-Esq-L0002110663.htm
and it returns nothing

other pages on this site do work (but only at higher levels) - such as http://www.superpages.com/bp/New-York-NY/Martin-A-Kron-Esq-L0002110663.htm.

I think this website is blocking the entry to the business information page - how can I get around this?


04-21-2011, 08:31 PM
Try using cURL instead. file_get_contents() is ok for very basic calls but i don't think it supports headers and things which the site may be inspecting. cURL lets you set all sets of options.

04-22-2011, 11:15 AM
Thanks - it works.

One more questions - if I have a loop on numerous pages - do I do the curl init for each one or on time per scripts?

04-22-2011, 03:27 PM
You only need to use curl_init to initialise a curl instance. You can set the URL in a loop ad call curl_exec as many times as you need.

This is from php.net:

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser

// close cURL resource, and free up system resources

Note how you can use curl_setopt() to set the url - which you can do in your loop. Just use curl_init once before the loop and curl_close after the loop.