php 获取session id,php用curl获取sessionid

http://www.111cn.net/phper/21/cfc64649f56d7b599cbb3cf15ade8cee.htm

$cookie_jar = tempnam(''./tmp'',''cookie'');

$ch = curl_init(); curl_setopt($ch,CURLOPT_URL,''http://******'');

curl_setopt($ch, CURLOPT_POST, 1);

$request = ''email_address=&password=&action='';

curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

//把返回来的cookie信息保存在$cookie_jar文件中

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);

//设定返回的数据是否自动显示

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//设定是否显示头信息

curl_setopt($ch, CURLOPT_HEADER, false);

//设定是否输出页面内容

curl_setopt($ch, CURLOPT_NOBODY, false);

curl_exec($ch);

curl_close($ch); //get data after login

$ch2 = curl_init();

curl_setopt($ch2, CURLOPT_URL, ''http://*****'');

curl_setopt($ch2, CURLOPT_HEADER, false);

curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar);

$orders = curl_exec($ch2);

echo '''';

echo strip_tags($orders);

echo '''';

curl_close($ch2);

?>

方法2用fsockopen:

function GetWebContent($host, $method, $str, $sessid = '''')

{

$ip = gethostbyname($host);

//echo "ip=$ip
";

[email=$fp=@fsockopen($ip,80]$fp=@fsockopen($ip,80[/email]);

if (!$fp) return;

fputs($fp, "$method ");

fputs($fp, "Host: $host ");

if (!empty($sessid))

{

fputs($fp, "Cookie: PHPSESSID=$sessid; path=/; ");

}

if ( substr(trim($method),0, 4) == "POST")

{

fputs($fp, "Content-Length: ". strlen($str) . " "); // 别忘了指定长度

}

//fputs($fp, "Content-Type: application/x-www-form-urlencoded ");

fputs($fp, "Content-Type: application/x-www-form-urlencoded ");

fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1) )");//add by Ew 071012

fputs($fp, "Connection: Keep-Alive ");

if ( substr(trim($method),0, 4) == "POST")

{

fputs($fp, $str." ");

}

while(!feof($fp))

{

$response .= fgets($fp);

}

$hlen = strpos($response," "); // LINUX下是 " "

$header = substr($response, 0, $hlen);

//echo "header=$header

";
$entity = substr($response, $hlen + 4);

if ( preg_match(''/PHPSESSID=([0-9a-z]+);/i'', $header, $matches))

{

$a[''sessid''] = $matches[1];

}

if ( preg_match(''/Location: ([0-9a-z_?=.]+)/i'', $header, $matches))

{

$a[''location''] = $matches[1];

}

$a[''content''] = $entity;

fclose($fp);

return $a;

}

$response = GetWebContent("$host","POST /$login_page HTTP/1.0", $str);//登入得到新的session_id

//...可以在这里先保存session_id

$response = GetWebContent("$host","GET /$somepage HTTP/1.0", '''', $response[''sessid'']);//使用session_id访问页面

echo $response[''location''].$response[''content'']."
";

?>

参考 https://blog.csdn.net/weixin_30171859/article/details/115230849

您可能还喜欢...

发表回复