file_get_contents() do not work on web with sub-domain

Hello there, i am sorry for wasting your time, but your assistance could help me a lot.

I am working on php script which takes information from webpages. When i trying to get html source into string with this:
[php]$url = ‘http://cs.sportsdirect.com/mens/mens-indoor-and-court-trainers’;
$str = file_get_contents($url, NULL, NULL, 10, 1250);[/php]
It writes me this error message:Warning: file_get_contents(http://cs.sportsdirect.com/mens/mens-indoor-and-court-trainers) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request - on line which starting with $str.

But when i try to get string like here:
[php]$url = ‘http://www.sportsdirect.com/mens/mens-indoor-and-court-trainers’;
$str = file_get_contents($url, NULL, NULL, 10, 1250);[/php]
It works fine. (this 2 sites are very simmilar, but one of them is in czech and second in english)

Can you please help me? I need to have information in my language.

Thank you for help!

I am not a big fan of file_get_contents for retrieving from a remote site. It can easily be blocked at the server level and doesn’t have many features or options. I have no idea why it is working with the www and not with the cs, but it may have to do with how the server is handling the subdomain, or a different php.ini configuration between the two.

If curl is available to you, the following is working for me…[php]$url = ‘http://cs.sportsdirect.com/mens/mens-indoor-and-court-trainers’;

function curl_contents($url){
$crl = curl_init();
$timeout = 15;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($crl, CURLOPT_AUTOREFERER, 1);
curl_setopt ($crl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($crl, CURLOPT_USERAGENT, ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:15.0) Gecko/20100101 Firefox/15.0.1’);
curl_setopt ($crl, CURLOPT_HTTPHEADER, array(‘DNT: 1’,‘REQUEST: GET /mens/mens-indoor-and-court-trainers HTTP/1.1’,‘Host: cs.sportsdirect.com’,‘Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8’,‘Accept-Language: en-us,en;q=0.5’));
$ret = curl_exec($crl);
$info = curl_getinfo($crl);
curl_close($crl);
return $ret;
}

echo curl_contents($url);
[/php]

I should note that I had to play with the headers in order to get it to work. Curl also failed initially - the benefit is that you can do things like this (and cookies). You can also use it to POST, etc.

Let me know if it doesn’t work for you, but I don’t know that you are going to be able to get file_get_contents to work for you with the cs subdomain.

Not all hosts support curl though

I do not know how it works, but it works and that is important! Thank you very much, maybey one day my skills will be so big that i understant this :slight_smile:

Anyway thank you very much!

Draq,

Glad it worked for you! Normally you do not need to set nearly this many options to make curl work! As I hinted to …

If curl is available to you
and as richei also pointed out, it isn’t built into every install of php; but when it is (and in my experience it usually is) - it is much more powerful.

jay

Yeah, i must download dll to my wamp localhost server… I hope it will work on my online server :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service