development

PHP file_get_contents () 및 설정 요청 헤더

big-blog 2020. 7. 21. 07:29
반응형

PHP file_get_contents () 및 설정 요청 헤더


PHP를 사용하면 file_get_contents()? 로 HTTP 헤더를 보낼 수 있습니까?

php.ini파일 에서 사용자 에이전트를 보낼 수 있다는 것을 알고 있습니다 . 그러나, 당신은 또한 같은 다른 정보를 보낼 수 있습니다 HTTP_ACCEPT, HTTP_ACCEPT_LANGUAGEHTTP_CONNECTIONfile_get_contents()?

아니면 이것을 달성 할 다른 기능이 있습니까?


실제로 file_get_contents()함수 에 대한 추가 정보는 다음과 같습니다.

// Create a stream
$opts = [
    "http" => [
        "method" => "GET",
        "header" => "Accept-language: en\r\n" .
            "Cookie: foo=bar\r\n"
    ]
];

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);

당신이 찾고있는 것을 달성하기 위해이 패턴을 따를 수 있습니다. 나는 개인적으로 이것을 테스트하지 않았습니다. (그리고 작동하지 않으면 다른 답변을 확인하십시오.)


여기 나를 위해 일한 것이 있습니다 (Dominic은 한 줄로 짧았습니다).

$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);

이 변수를 사용하여 file_get_contents()함수 후 응답 헤더를 검색 할 수 있습니다 .

암호:

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

산출:

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"
}

예.

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


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);
?>

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.


Unfortunately, it doesn't look like file_get_contents() really offers that degree of control. The cURL extension is usually the first to come up, but I would highly recommend the PECL_HTTP extension (http://pecl.php.net/package/pecl_http) for very simple and straightforward HTTP requests. (it's much easier to work with than cURL)

참고URL : https://stackoverflow.com/questions/2107759/php-file-get-contents-and-setting-request-headers

반응형