Home > Backend Development > PHP Problem > How to set request header information in php

How to set request header information in php

藏色散人
Release: 2023-03-10 16:44:01
Original
5548 people have browsed it

How to set request header information in php: 1. Use the header function to set the request header information; 2. Set the request header information through the fsockopen function; 3. Set the request header information by using the curl component.

How to set request header information in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How to set request header information in php?

php sets http request header information and response header information

You can use fsockopen and curl components to set the header information of the request server. The header function can only be used to set the header information of the client response. , cannot set the header information of the server.

1. Usage of header function

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

Multiple headers need to be written directly and cannot be connected together

2.fsockopen Function usage

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);
}
?>
Copy after login

2.php

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

3.Usage of curl component

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);
 
?>
Copy after login

2.php

print_r(getallheaders());//Will Return the header information of your own set request

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to set request header information in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template