views:

2980

answers:

6

Hi, i'm trying to get the contents from another file with file_get_contents (don't ask why).

I have two files: test1.php and test2.php. Test1.php returns a string, bases on the user that is logged in.

Test2.php tries to get the contents of test1.php and is being executed by the browser, thus getting the cookies.

To send the cookies with file_get_contents, i create a streaming context:

$opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n"))`;

I'm retreiving the contents with:

$contents = file_get_contents("http://www.domain.com/test1.php", false, $opts);

But now I get the error:

Warning: file_get_contents(http://www.domain.com/test1.php) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

Does somebody knows what i'm doing wroing here?

edit: forgot to mention: Without the streaming_context, the page just loads. But withouth the cookies I don't get the info I need.

A: 

Make sure that file1.php exists on the server. Try opening it in your own browser to make sure!

Sebastian Hoitz
+4  A: 

First, this is probably just a typo in your question, but the third arguments to file_get_contents() needs to be your streaming context, NOT the array of options. I ran a quick test with something like this, and everything worked as expected

$opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n"));
$context = stream_context_create($opts);
$contents = file_get_contents('http://example.com/test1.txt', false, $context);
echo $contents;

The error indicates the server is returning a 404. Try fetching the URL from the machine PHP is running on and not from your workstation/desktop/laptop. It may be that your web server is having trouble reaching the site, your local machine has a cached copy, or some other network screwiness.

Be sure you repeat your exact request when running this test, including the cookie you're sending (command line curl is good for this). It's entirely possible that the page in question may load fine in a browser without the cookie, but when you send the cookie the site actually is returning a 404.

Make sure that $_SERVER['HTTP_COOKIE'] has the raw cookie you think it does.

If you're screen scraping, download Firefox and a copy of the LiveHTTPHeaders extension. Perform all the necessary steps to reach whatever page it is you want in Firefox. Then, using the output from LiveHTTPHeaders, recreate the exact same request requence. Include every header, not just the cookies.

Finally, PHP Curl exists for a reason. If at all possible, (I'm not asking!) use it instead. :)

Alan Storm
A: 

I am trying to read from a remote url, it requires sending cookies. I used the "-Live http headers-addon for firefox" to get the header information.

$url = "the url I am trying to read from";

$streamcontext_options = array(
 'http'=> array(
  'method'=> 'GET',  
  'header'=>'Host: www.example.com'.'\r\n'.
       'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10'.'\r\n'.
          'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'.'\r\n'.
          'Accept-Language: en-us,en;q=0.5'.'\r\n'.
          'Accept-Encoding: gzip,deflate'.'\r\n'.
          'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'.'\r\n'.
          'Keep-Alive: 300'.'\r\n'.
          'Connection: keep-alive'.'\r\n'.
          'Cookie: SESSIONSCOPETESTED=1; HASSESSIONSCOPE=1; CFID=17293435; CFTOKEN=cee1d949e204d3d9-11306833-9220-CB78-78019E5B4D41E902; LASTVISIT=%7Bts%20%272009%2D05%2D05%2012%3A02%3A33%27%7D; __utma=217479050.3269404674142338000.1241533938.1241533938.1241533938.1; __utmb=217479050.64.10.1241533938; __utmc=217479050; __utmz=217479050.1241533938.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); USERID=458824; PASSWORD=1D444A8D2653BF4360ED48EEF4DB1510; NEXTPAGE=%3Ffa%3D; __utmv=217479050.member; STATUS=LoggedIn'.'\r\n'
         ) );

So there goes all the required header info from the -live http headers- I guess.

$streamcontext = stream_context_create($streamcontext_options);
$fcontent_1 = file_get_contents($url, false, $streamcontext);

It throws a warning... [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in ... on line ...

The url I am trying to read is of the form... example.com/foo/?ctf=soccer&start=1&key=get%20firstset

The value of the last argument(get firstset) is urlencoded I guess, could the problem have something to do with that ?. Using urldecode(...) on the url throws the same warning!.

any suggestions? thanks

A: 

I tried php curl too, but no joy.

$url = "the url that asks to login";
$username = "myusername";
$password = "mypassword";
$postargs = "Username=".urlencode($username)."&Password=".urlencode($password);

$cr = curl_init($url);
curl_setopt ($cr, CURLOPT_POST, true);
curl_setopt ($cr, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($cr, CURLOPT_HEADER, true);
curl_setopt($cr, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cr, CURLOPT_COOKIEJAR, 'cookie1.txt');
$content_1 = curl_exec($cr);
curl_close($cr);

$url = "here goes the url I am trying to read from";
$cr = curl_init($url);
curl_setopt($cr, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cr, CURLOPT_COOKIEFILE, 'cookie1.txt');
$content_2 = curl_exec($cr);
curl_close($cr);

curl does not write the header information to the cookie1.txt I have used. I read elsewhere this seems to be a incompatilibity issue with php 5.

$content_1 shows.. HTTP/1.1 200 OK Date: Tue, 05 May 2009 18:22:12 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: Servlet 2.4; JBoss-4.2.2.GA (build: SVNTag=JBoss_4_2_2_GA date=200710221139)/Tomcat-5.5 Set-Cookie: SESSIONSCOPETESTED=0; Path=/ Set-Cookie: HASSESSIONSCOPE=0; Path=/ Set-Cookie: SESSIONSCOPETESTED=1; Path=/ Set-Cookie: HASSESSIONSCOPE=0; Path=/ Transfer-Encoding: chunked Content-Type: text/html;charset=UTF-8 1
, this info seems way too far from what -live http header addon- shows.

$content_2 shows ... The requested scope session has not been enabled. Before session variables can be used, the session state management system must be enabled ...
, among a bunch of other messages.

Would anyone shed some light on this?. thanks

A: 

Just out of curiosity, are you attempting a file_get_contents on a page that has a space in it? I remember trying to use fgc on a URL that had a space in the name and while my web browser parsed it just fine, fgc didn't. I ended up having to use a str_replace to replace ' ' with '%20'. I would think that this should have been relatively easy to spot that though as it would report only half of the filename. Also, I noticed in one of these posts, someone used \r\n while defining the headers. Keep in mind that PHP doesn't like these to be in single quotes, but they work fine in double.

Hope this helped, Jason

Jason
A: 

FYI- if you are using A2hosting you have to request fopen access to be granted (they shut it down by default for security reasons)

Rob