菁葱~岁月~ 不饶人。

file_get_contents模拟POST、GET传值,同时发送和获取COOKIE

如何使用file_get_contents函数,模拟浏览器通过POST和GET方式提交信息到指定页面,以及模拟浏览器获取远程网站的cookie,如何判断这些cookie是否过期,如何请求头中加入远程网站需要的cookie。
获取HTTP响应头:
$http_response_header — HTTP 响应头
说明:
$http_response_header 数组与 get_headers() 函数类似。当使用HTTP 包装器时,$http_response_header 将会被 HTTP 响应头信息填充。$http_response_header 将被创建于局部作用域中。
 
获取cookie
保存cookie的信息以Set-Cookie开头,格式为Set-Cookie: cookie变量名; expires=英文字符串表示的格林威治时间的有效期; paht=生效的url路径。。。(和setcookie顺序一样)
 
 
(修改php.ini 文件 设置 expose_php = Off可以去掉响应头中的X-Powered-By: PHP/5.2.5)
 
例子:
 
xc_form.php表单页面,设置cookie
<?php
setcookie("GuestId", 'guest111111');
setcookie('Del' , '删除/无效的cookie', time() - 3600 , '/');
setcookie('CSESSIONID' , '20111216', time() + 3600 , '/');
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html>
<form action="xc.php?getname=张三" method="post">
<input type="text" name="postname" />
<input type="submit" value="submit" />
</form>
</html>
 
xc.php接收传值,删除cookie并设置另一个cookie
<?php
setcookie("GuestId", '', time() - 3600);
setcookie("UserId", 'd364d43s2425c6f64d63223452243324');
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
var_dump($_COOKIE);
var_dump($_POST);
var_dump($_GET);
var_dump($_SERVER);
?>
 
test.php
<?php
//从表单页或任意页面获取cookie
$form_url= 'https://test135.info/test/xc_form.php';
$setting_array= array(
'http' => array(
'timeout' => 5,
'method' => "GET",
'protocol_version' => "1.1",
'header' =>
"User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1\r\n" .
"Host: test135.info\r\n" .
"Accept-Language: zh-cn,zh;q=0.5\r\n" .
"Accept-Encoding: gzip, deflate\r\n" .
"Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n" .
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" .
"Connection: keep-alive\r\n\r\n"
)
 
);
 
file_get_contents($form_url, FALSE, stream_context_create($setting_array));
var_dump($http_response_header);
$cookie_arr= get_cookies($http_response_header, array());
var_dump($cookie_arr);
$cookiedata= build_cookiedata($cookie_arr);
$postdata= http_build_query(array('postname'=>'post数据'));//结构与url传值?后面的部分相同,http_build_query还有其他参数
$getdata= http_build_query(array('getname'=>'张三'));
 
 
$url= 'https://test135.info/test/xc.php?'.$getdata;//这个页面是用来接收post、get和cookie的
$setting_array= array(
'http' => array(
'timeout' => 5,
'method' => "POST",
'protocol_version' => "1.1",
'header' =>
"User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1\r\n" .
"Referer: ".$form_url."\r\n".//表单页面url
"Host: test135.info\r\n" .
"Accept-Language: zh-cn,zh;q=0.5\r\n" .
"Accept-Encoding: gzip, deflate\r\n" .
"Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n" .
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" .
"Connection: keep-alive\r\n".
"Content-length: ".strlen($postdata)."\r\n".//数据字符串长度
"Content-type: application/x-www-form-urlencoded\r\n".//类型:表单
"Cookie: ".$cookiedata."\r\n\r\n",
'content'=> $postdata,//格式:a=1&b=2&c=3
)
 
);
 
 
echo file_get_contents($url, FALSE, stream_context_create($setting_array));
$cookie_arr= get_cookies($http_response_header, $cookie_arr);
var_dump($cookie_arr);
 
//根据返回头“$http_response_header”处理保存cookie的数组“$cookie_arr”
//$cookie_arr= get_cookies($http_response_header, $cookie_arr);
function get_cookies($get_header, $cookie_arr){
preg_match_all( "/Set-Cookie: (.*?)(?:\r\n|; expires=(.*?)(?:\r\n|; ))/" , implode( "\r\n" , $get_header ), $cookies );
if(count($cookies[1])<= 0){
return $cookie_arr;
}
foreach($cookies[2] as $k=>$expire_time){//$cookies[1]:cookie的name和值,$cookies[2]有效期字符串,一一对应。
$header_cookie_arr= explode('=', $cookies[1][$k]);//不需要trim,因为两边有空格的cookie名一定会定义失败
if(strlen($expire_time)> 5&& strtotime($expire_time,time())< time()){
if(isset($cookie_arr[$header_cookie_arr[0]])){
unset ($cookie_arr[$header_cookie_arr[0]]);//没有考虑作用域名和目录
}
}else{
$cookie_arr[$header_cookie_arr[0]]= $header_cookie_arr[1];
}
}
return $cookie_arr;
}
//组织cookie传值字符串
function build_cookiedata($cookie_arr){
$cookiedata= '';
foreach($cookie_arr as $k=> $v){
$cookiedata.= $k.'='.$v.'; ';//浏览器传cookie时;后有空格
}
if(strlen($cookiedata)>0){
$cookiedata= substr($cookiedata, 0, -2);
}
return $cookiedata;
}
?>
原文地址:https://hi.baidu.com/119991/blog/item/108ed45029f3bb06377abec4.html
通过本地文件test.php和远程文件xc_form.php访问,xc_form.php文件有完全相同的输出:
array(2) {
["GuestId"]=>
string(11) "guest111111"
["CSESSIONID"]=>
string(8) "20111216"
}
array(1) {
["postname"]=>
string(10) "post数据"
}
array(1) {
["getname"]=>
string(6) "张三"
}
array(45) {
["ALL_HTTP"]=>
string(526) "HTTP_CONNECTION:keep-alive
HTTP_CONTENT_LENGTH:31
HTTP_CONTENT_TYPE:application/x-www-form-urlencoded
HTTP_ACCEPT:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
HTTP_ACCEPT_CHARSET:GB2312,utf-8;q=0.7,*;q=0.7
HTTP_ACCEPT_ENCODING:gzip, deflate
HTTP_ACCEPT_LANGUAGE:zh-cn,zh;q=0.5
HTTP_COOKIE:GuestId=guest111111; CSESSIONID=20111216
HTTP_HOST:test135.info
HTTP_REFERER:https://test135.info/test/xc_form.php
HTTP_USER_AGENT:Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
"
["HTTPS"]=>
string(3) "off"
["SCRIPT_NAME"]=>
string(12) "/test/xc.php"
["HTTP_COOKIE"]=>
string(40) "GuestId=guest111111; CSESSIONID=20111216"
["AUTH_PASSWORD"]=>
string(0) ""
["AUTH_TYPE"]=>
string(0) ""
["AUTH_USER"]=>
string(0) ""
["CONTENT_LENGTH"]=>
string(2) "31"
["CONTENT_TYPE"]=>
string(33) "application/x-www-form-urlencoded"
["PATH_TRANSLATED"]=>
string(26) "e:\wwwroot\test135\wwwroot"
["QUERY_STRING"]=>
string(26) "getname=%E5%BC%A0%E4%B8%89"
["REMOTE_ADDR"]=>
string(12) "1.1.1.1"
["REMOTE_HOST"]=>
string(12) "1.1.1.1"
["REMOTE_USER"]=>
string(0) ""
["REQUEST_METHOD"]=>
string(4) "POST"
["SERVER_NAME"]=>
string(22) "test135.info"
["SERVER_PORT"]=>
string(2) "80"
["SERVER_PROTOCOL"]=>
string(8) "HTTP/1.1"
["SERVER_SOFTWARE"]=>
string(17) "Microsoft-IIS/6.0"
["APPL_MD_PATH"]=>
string(18) "/LM/w3svc/161/ROOT"
["APPL_PHYSICAL_PATH"]=>
string(27) "e:\wwwroot\test135\wwwroot\"
["INSTANCE_ID"]=>
string(3) "161"
["INSTANCE_META_PATH"]=>
string(13) "/LM/W3SVC/161"
["LOGON_USER"]=>
string(0) ""
["REQUEST_URI"]=>
string(39) "/test/xc.php?getname=%E5%BC%A0%E4%B8%89"
["URL"]=>
string(39) "/test/xc.php?getname=%E5%BC%A0%E4%B8%89"
["SCRIPT_FILENAME"]=>
string(38) "e:\wwwroot\test135\wwwroot\test\xc.php"
["ORIG_PATH_INFO"]=>
string(12) "/test/xc.php"
["PATH_INFO"]=>
string(0) ""
["ORIG_PATH_TRANSLATED"]=>
string(38) "e:\wwwroot\test135\wwwroot\test\xc.php"
["DOCUMENT_ROOT"]=>
string(26) "e:\wwwroot\test135\wwwroot"
["PHP_SELF"]=>
string(12) "/test/xc.php"
["HTTP_CONNECTION"]=>
string(10) "keep-alive"
["HTTP_CONTENT_LENGTH"]=>
string(2) "31"
["HTTP_CONTENT_TYPE"]=>
string(33) "application/x-www-form-urlencoded"
["HTTP_ACCEPT"]=>
string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
["HTTP_ACCEPT_CHARSET"]=>
string(26) "GB2312,utf-8;q=0.7,*;q=0.7"
["HTTP_ACCEPT_ENCODING"]=>
string(13) "gzip, deflate"
["HTTP_ACCEPT_LANGUAGE"]=>
string(14) "zh-cn,zh;q=0.5"
["HTTP_HOST"]=>
string(22) "test135.info"
["HTTP_REFERER"]=>
string(46) "https://test135.info/test/xc_form.php"
["HTTP_USER_AGENT"]=>
string(67) "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"
["REQUEST_TIME"]=>
int(1324007337)
["argv"]=>
array(1) {
[0]=>
string(26) "getname=%E5%BC%A0%E4%B8%89"
}
["argc"]=>
int(1)
}
array(2) {
["CSESSIONID"]=>
string(8) "20111216"
["UserId"]=>
string(32) "d364d43s2425c6f64d63223452243324"
}
zhuan: https://hi.baidu.com/timepage/item/0410cad4562b3c1d20e25045

评论
热度(1)

© 菁葱岁月 | Powered by LOFTER