php script to read and save web page contents not working on some sites -


i have simple script works on sites not main site want work - code below accesses sample site perfectly. when use on site want access http://www.livescore.com error

this works.

<?php $url = "http://www.cambodia.me.uk"; $page = file_get_contents($url); $outfile = "contents.html"; file_put_contents($outfile, $page); ?> 

this not work.....

<?php $url = "http://www.livescore.com"; $page = file_get_contents($url); $outfile = "contents.html"; file_put_contents($outfile, $page); ?> 

and gives following error

warning: file_get_contents(http://www.livescore.com) [function.file-get-contents]: failed open stream: http request failed! http/1.0 404 not found in c:\program files (x86)\easyphp-5.3.8.1\www\livescore\attempt-1-read-page.php on line 3

thanks assistance

in common case can file_get_contents follow redirects:

$context = stream_context_create(     array(         'http' => array(             'follow_location' => true         )     ) );  $html = file_get_contents('http://www.example.com/', false, $context); 

this site tries analyze user-agent http header, , fails if it's not found. try add user-agent header:

<?php  $context = stream_context_create(     array(         'http' => array(             'header' => "user-agent: chrome",             'ignore_errors' => true,             'follow_location' => true         )     ) );  $html = file_get_contents('http://www.livescore.com/', false, $context);  echo substr($html, 0, 200)."\n"; 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -