file_get_contents + stream_context_create

I have the following script, and it works on my development server (Wampserver2), but on the production server (Linux), the cookies I’m trying to send in the HTTP header don’t seem to make the receiving script use them.

Initially, on Wampserver2, I had a problem, and it was that I had to have a User-Agent string in the header, but I have no clue what is going on for the Linux server.

I have tried to change the line endings in the header from \r
, so that isn’t going to make a difference, in case that was what you are thinking.

Please let me know if you have an answer.

The code:

<?php defined('SYSPATH') OR die('No direct access allowed.');
if(isset($_COOKIE['bwd_data']) && isset($_COOKIE['bwd'])){
	$cookie_val = $_COOKIE['bwd_data'];
	$cookie_val2 = $_COOKIE['bwd'];
	$opts = array(
		'header'=>	"Host:\\r\
" .
					"User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . " \\r\
" .
					"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \\r\
" .
					"Accept-language: en-us,en;q=0.5\\r\
" .
					//"Accept-Encoding: gzip,deflate\\r\
" .
					"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\\r\
" .
					"Keep-Alive: 300\\r\
" .
					"Connection: keep-alive\\r\
" .
" .
					"Cookie: bwd=" . $cookie_val2 . "; " . "bwd_data=" . $cookie_val . "\\r\
	$context = stream_context_create($opts);
	echo(file_get_contents(url::site('httperrors','http'), FALSE , $context));

print_r($opts); displays the value you expect?

print_r($opts) looks fine to me:

    [http] => Array
            [method] => GET
            [header] => Host:
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/2009042316 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-language: en-us,en;q=0.5
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: bwd=0580d58974469d164f062e6a15296847; bwd_data=c2Vzc2lvbl9pZHxzOjMyOiIwBTgwZDU4OTY0NDY5ZDE2NGYwNjJlNmExNTI5Njg0NyI7dG90YWxfaGl0c3xpOjgzO19rZl9mbGFzaF98YTowOnt9dXNlcl9hZ2VudHxzOjkyOiJNb3ppbGxhLzUuMCAoV2luZG93czsgVTsgV2luZG93cyBOVCA1LjE7IGVuLVVTOyBydjoxLjkuMC4xMCkgR2Vja28vMjAwOTA0MjMxNiBGaXJlZm94LzMuMC4xMCI7aXBfYWRkcmVzc3xzOjEyOiI2OC4xODMuOC4xODAiO2xhc3RfYWN0aXZpdHl8aToxMjQyMzE4ODc3O3N0eWxlc2hlZXR8czoxMDoiYmx1ZV90aGVtZSI7



If you think you can help, and can take a look at my phpinfo(), I’ve made it available here:

I’d really like to get this working.

On your server, which of the if or else blocks is being executed? (ie, is the cookie set for your server’s domain?)

IF is being executed, not else. I’ve already checked that by removing the if/else altogether, and running the code as if cookies were always going to be present.

Am I right to assume that the script/server which receives this http request is not in your control? If not, consider mocking a script to receive the request on a domain you do control. Then send then request from both of the servers, and have your receiving mock script log the entire http request. Then you can compare them.

Also, does file_get_contents() return any string at all? On that note, consider using fopen() temporarily, as then you can use stream_get_meta_data($fp) to view the http response headers.

I found my answer by browsing the bug reports for the stream_context_create() function. Apparently, if php is compiled --with-curlwrappers, then a header that is sent as an option must be in the form of a simple array, and not a string as documented.

It would seem quite painless for somebody to update the documentation with this simple fact, and I’m a little hosed that I had to spent so long looking for an answer, but I am relieved that I am done and can move on.

Thanks for your time.