programing

PHP file_get_contents() 및 요청 헤더 설정

copysource 2023. 1. 14. 10:55
반응형

PHP file_get_contents() 및 요청 헤더 설정

PHP를 사용하여 HTTP 헤더를 전송할 수 있습니까?file_get_contents()?

사용자 에이전트를 에서 보낼 수 있다는 것을 알고 있습니다.php.ini파일입니다. 하지만, 다음과 같은 다른 정보들도 보내주실 수 있나요?HTTP_ACCEPT,HTTP_ACCEPT_LANGUAGE,그리고.HTTP_CONNECTION와 함께file_get_contents()?

아니면 이를 실현하는 다른 기능이 있습니까?

사실, 더 읽어보면file_get_contents()기능:

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

당신이 원하는 것을 달성하기 위해 당신은 이 패턴을 따를 수 있습니다.하지만 저는 이것을 개인적으로 테스트한 적이 없습니다.(그리고 만약 그것이 효과가 없다면, 다른 답변을 확인해 주세요.)

이것이 나에게 효과가 있었다(도미닉은 한 줄만 짧았다).

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

php cURL 라이브러리를 사용하는 것이 올바른 방법일 것입니다.이 라이브러리는 단순한 라이브러리보다 많은 기능을 가지고 있기 때문입니다.file_get_contents(...).

예:

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

네.

전화할 때file_get_contentsURL 에서는, 를 사용할 필요가 있습니다.stream_create_context이 함수는 php.net에 잘 설명되어 있습니다.

이는 php.net의 사용자 코멘트 섹션에 있는 다음 페이지에서 거의 정확하게 다루어집니다.http://php.net/manual/en/function.stream-context-create.php

HTTPS가 필요 없고 시스템에서 컬을 사용할 수 없는 경우 를 사용할 수 있습니다.

이 함수는 일반 파일 핸들처럼 읽고 쓸 수 있는 연결을 엽니다.

아쉽게도, 이건...file_get_contents()그 정도의 제어력을 제공합니다.보통 cURL 확장자가 가장 먼저 표시되지만 매우 단순하고 간단한HTTP 요구의 경우 PECL_HTTP 확장자(http://pecl.php.net/package/pecl_http)를 권장합니다.(cURL보다 훨씬 사용하기 쉽다)

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

반응형