PHP file_get_contents() and setting request headers

后端 未结 7 961
庸人自扰
庸人自扰 2020-11-22 14:29

With PHP, is it possible to send HTTP headers with file_get_contents() ?

I know you can send the user agent from your php.ini file. However

相关标签:
7条回答
  • 2020-11-22 14:32

    Here is what worked for me (Dominic was just one line short).

    $url = "";
    
    $options = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n" .
                  "Cookie: foo=bar\r\n" .  // check function.stream-context-create on php.net
                  "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad 
      )
    );
    
    $context = stream_context_create($options);
    $file = file_get_contents($url, false, $context);
    
    0 讨论(0)
  • 2020-11-22 14:32

    Yes.

    When calling file_get_contents on a URL, one should use the stream_create_context function, which is fairly well documented on php.net.

    This is more or less exactly covered on the following page at php.net in the user comments section: http://php.net/manual/en/function.stream-context-create.php

    0 讨论(0)
  • 2020-11-22 14:32

    If you don't need HTTPS and curl is not available on your system you could use fsockopen

    This function opens a connection from which you can both read and write like you would do with a normal file handle.

    0 讨论(0)
  • 2020-11-22 14:36

    Using the php cURL libraries will probably be the right way to go, as this library has more features than the simple file_get_contents(...).

    An example:

    <?php
    $ch = curl_init();
    $headers = array('HTTP_ACCEPT: Something', 'HTTP_ACCEPT_LANGUAGE: fr, en, da, nl', 'HTTP_CONNECTION: Something');
    
    curl_setopt($ch, CURLOPT_URL, "http://localhost"); # URL to post to
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); # return into a variable
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header ); # custom headers, see above
    $result = curl_exec( $ch ); # run!
    curl_close($ch);
    ?>
    
    0 讨论(0)
  • 2020-11-22 14:40

    Actually, upon further reading on the file_get_contents() function:

    // Create a stream
    $opts = [
        "http" => [
            "method" => "GET",
            "header" => "Accept-language: en\r\n" .
                "Cookie: foo=bar\r\n"
        ]
    ];
    
    // DOCS: https://www.php.net/manual/en/function.stream-context-create.php
    $context = stream_context_create($opts);
    
    // Open the file using the HTTP headers set above
    // DOCS: https://www.php.net/manual/en/function.file-get-contents.php
    $file = file_get_contents('http://www.example.com/', false, $context);
    

    You may be able to follow this pattern to achieve what you are seeking to, I haven't personally tested this though. (and if it doesn't work, feel free to check out my other answer)

    0 讨论(0)
  • 2020-11-22 14:51

    You can use this variable to retrieve response headers after file_get_contents() function.

    Code:

      file_get_contents("http://example.com");
      var_dump($http_response_header);
    

    Output:

    array(9) {
      [0]=>
      string(15) "HTTP/1.1 200 OK"
      [1]=>
      string(35) "Date: Sat, 12 Apr 2008 17:30:38 GMT"
      [2]=>
      string(29) "Server: Apache/2.2.3 (CentOS)"
      [3]=>
      string(44) "Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT"
      [4]=>
      string(27) "ETag: "280100-1b6-80bfd280""
      [5]=>
      string(20) "Accept-Ranges: bytes"
      [6]=>
      string(19) "Content-Length: 438"
      [7]=>
      string(17) "Connection: close"
      [8]=>
      string(38) "Content-Type: text/html; charset=UTF-8"
    }
    
    0 讨论(0)
提交回复
热议问题