Possible Duplicate:
Grabbing the href attribute of an A element
如何提取sessionId
和viewstate
这段代码中的变量?
代码是这样的:
$url = "http://www.atb.bergamo.it/ITA/Default.aspx?SEZ=2&PAG=38&MOD=LINTRV";
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec ($ch);
curl_close($ch);
preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~',$html,$viewstate);
var_dump(file_get_contents($ckfile)); //<--- There's the sessionId variable in it
var_dump($viewstate[1]); //<--- View State
你能帮我吗?
最佳答案
您可以非常简单地完成此操作,无需正则表达式:
$viewstate = explode('id="__VIEWSTATE" value="', $html);
$viewstate = explode('"', $viewstate[1]);
$viewstate = $viewstate[0];
cookie 也一样:
$sesid = explode('SessionId', file_get_contents($ckfile));
$sesid = explode('\n', $sesid[1]);
$sesid = trim($sesid[0]);
把它们放在一起,你就得到了
$url = "http://www.atb.bergamo.it/ITA/Default.aspx?SEZ=2&PAG=38&MOD=LINTRV";
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec ($ch);
curl_close($ch);
$viewstate = explode('id="__VIEWSTATE" value="', $html);
$viewstate = explode('"', $viewstate[1]);
$viewstate = $viewstate[0];
$sesid = explode('_SessionId', file_get_contents($ckfile));
$sesid = explode("\n", $sesid[1]);
$sesid = trim($sesid[0]);
echo $viewstate."<br/>";
echo $sesid;
适用于我的测试设置。
关于php - 如何从此代码中获取 sessionId 和 viewstate 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13662499/