SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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\n to \n, 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:

    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(
    	  'http'=>array(
    		'method'=>"GET",
    		'header'=>	"Host: www.mysite.com\r\n" .
    					"User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . " \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" .
    					"Referer: http://www.mysite.com/\r\n" .
    					"Cookie: bwd=" . $cookie_val2 . "; " . "bwd_data=" . $cookie_val . "\r\n"
    	  )
    	);
    	$context = stream_context_create($opts);
    	echo(file_get_contents(url::site('httperrors','http'), FALSE , $context));
    }else{
    	echo(file_get_contents(url::site('httperrors','http')));
    }
    ?>

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print_r($opts); displays the value you expect?

  3. #3
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print_r($opts) looks fine to me:
    Code:
    Array
    (
        [http] => Array
            (
                [method] => GET
                [header] => Host: www.mysite.com
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) 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
    Referer: http://www.mysite.com/
    Cookie: bwd=0580d58974469d164f062e6a15296847; bwd_data=c2Vzc2lvbl9pZHxzOjMyOiIwBTgwZDU4OTY0NDY5ZDE2NGYwNjJlNmExNTI5Njg0NyI7dG90YWxfaGl0c3xpOjgzO19rZl9mbGFzaF98YTowOnt9dXNlcl9hZ2VudHxzOjkyOiJNb3ppbGxhLzUuMCAoV2luZG93czsgVTsgV2luZG93cyBOVCA1LjE7IGVuLVVTOyBydjoxLjkuMC4xMCkgR2Vja28vMjAwOTA0MjMxNiBGaXJlZm94LzMuMC4xMCI7aXBfYWRkcmVzc3xzOjEyOiI2OC4xODMuOC4xODAiO2xhc3RfYWN0aXZpdHl8aToxMjQyMzE4ODc3O3N0eWxlc2hlZXR8czoxMDoiYmx1ZV90aGVtZSI7
    
            )
    
    )

  4. #4
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you think you can help, and can take a look at my phpinfo(), I've made it available here:

    http://www.ramseys-auto-detailing.com/phpinfo.gif

    I'd really like to get this working.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    On your server, which of the if or else blocks is being executed? (ie, is the cookie set for your server's domain?)
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    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.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •