首页 > 编程 > PHP > 正文

PHP设置头信息及取得返回头信息的方法

2020-03-22 17:30:21
字体:
来源:转载
供稿:网友
本文实例讲述了PHP设置头信息及取得返回头信息的方法。分享给大家供大家参考,具体如下:设置请求的头信息,我们可以用header函数,可以用fsockopen,可以用curl等,本文主要讲的是用curl来设置头信息,并取得返回后的头信息。一、请求方设置自己的头信息,header.phpfunction FormatHeader($url, $myIp = null,$xml = null) // 解悉url $temp = parse_url($url); $query = isset($temp['query']) $temp['query'] : ''; $path = isset($temp['path']) $temp['path'] : '/'; $header = array ( "POST {$path} {$query} HTTP/1.1", "Host: {$temp['host']}", "Content-Type: text/xml; charset=utf-8", 'Accept: */*', "Referer: http://{$temp['host']}/", 'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)', "X-Forwarded-For: {$myIp}", "Content-length: 380", "Connection: Close" return $header;$interface = 'http://localhost/test/header2.php';$header = FormatHeader($interface,'10.1.11.1');$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);
[HTTP_REFERER] = http://localhost/[HTTP_USER_AGENT] = Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)[HTTP_X_FORWARDED_FOR] = 10.1.11.1[CONTENT_LENGTH] = 380[PATH] = /usr/local/bin:/usr/bin:/bin[SERVER_SIGNATURE] = address Apache/2.2.16 (Ubuntu) Server at localhost Port 80 /address 。。。。。。。。。。。。。。。。。。。。。。。。。。。。上面那几个,我们可以明显看到,是我设置的头信息。四、取得返回的头信息
PHP基本语法入门教程》、《phphtml' target='_blank'>面向对象程序设计入门教程》及《php curl用法总结》希望本文所述对大家PHP程序设计有所帮助。PHP教程

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表