>  기사  >  백엔드 개발  >  PHP에서 요청 헤더 정보를 설정하는 방법

PHP에서 요청 헤더 정보를 설정하는 방법

藏色散人
藏色散人원래의
2021-06-17 09:24:385469검색

PHP에서 요청 헤더 정보를 설정하는 방법: 1. 헤더 함수를 사용하여 요청 헤더 정보를 설정합니다. 2. fsockopen 함수를 통해 요청 헤더 정보를 설정합니다. 3. 컬 구성요소를 사용하여 요청 헤더 정보를 설정합니다.

PHP에서 요청 헤더 정보를 설정하는 방법

이 문서의 운영 환경: Windows 7 시스템, PHP 버전 7.1, DELL G3 컴퓨터

PHP에서 요청 헤더 정보를 설정하는 방법은 무엇입니까?

php는 http 요청 헤더 정보 및 응답 헤더 정보를 설정합니다

요청 서버 설정 헤더 정보는 fsockopen 및 컬 구성 요소와 함께 사용할 수 있습니다. 헤더 기능은 클라이언트 응답의 헤더 정보를 설정하는 데만 사용할 수 있으며 서버의 헤더 정보는 설정할 수 없습니다.

1. 헤더 함수

  header('WWW-Authenticate: Negotiate');
  header('User-Agent:Mozilla/5.0);

여러 헤더를 직접 작성해야 합니다. 함께 연결할 수 없습니다

2. fsockopen 함수 사용

1.php

<?php
$fp = fsockopen("test.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET /2.php HTTP/1.1\r\n";
    $out .= "Host: test.com\r\n";
 
    $out .= "name:longqiqi\r\n";
    $out .= "Connection: Close\r\n\r\n";
 
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

2.php

print_r(getallheaders());//会返回自己设置请求的头信息

3.

1.php

<?php
 
function FormatHeader($url, $myIp = null,$xml = null)
{
 // 解析url
 $temp = parse_url($url);
 
 $query = isset($temp[&#39;query&#39;]) ? $temp[&#39;query&#39;] : &#39;&#39;;
 
 $path = isset($temp[&#39;path&#39;]) ? $temp[&#39;path&#39;] : &#39;/&#39;;
 
 $header = array (
 
 "POST {$path}?{$query} HTTP/1.1",
 
 "Host: {$temp[&#39;host&#39;]}",
 
 "Content-Type: text/xml; charset=utf-8",
 
 &#39;Accept: */*&#39;,
 
 "Referer: http://{$temp[&#39;host&#39;]}/",
 
 &#39;User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)&#39;,
 
 "X-Forwarded-For: {$myIp}",
 
 "Content-length: 380",
 
 "Connection: Close"
 
 );
 return $header;
} 
 
$interface = &#39;http://test.com/2.php&#39;;
 
$header = FormatHeader($interface,&#39;10.1.11.1&#39;);
 
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL, $interface);
 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  //设置头信息的地方
 
curl_setopt($ch, CURLOPT_HEADER, 0);    //不取得返回头信息
 
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
$result = curl_exec($ch);
 
var_dump($result);
 
?>

2 .php

print_r(getallheaders());//설정한 요청의 헤더 정보를 반환합니다

권장 학습: "

PHP 비디오 튜토리얼 "

위 내용은 PHP에서 요청 헤더 정보를 설정하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.