Php 如何从这段代码中获取sessionId和viewstate变量?

Php 如何从这段代码中获取sessionId和viewstate变量?,php,html,regex,libcurl,Php,Html,Regex,Libcurl,可能重复: 如何从该代码中提取sessionId和viewstate变量 代码如下: $url=”http://www.atb.bergamo.it/ITA/Default.aspx?SEZ=2&PAG=38&MOD=LINTRV"; $ckfile=tempnam(“/tmp”,“CURLCOOKIE”); $ch=curl\u init($url); curl_setopt($ch,CURLOPT_COOKIEJAR,$ckfile); curl_setopt($ch,CURLOPT_FO

可能重复:

如何从该代码中提取
sessionId
viewstate
变量

代码如下:

$url=”http://www.atb.bergamo.it/ITA/Default.aspx?SEZ=2&PAG=38&MOD=LINTRV";
$ckfile=tempnam(“/tmp”,“CURLCOOKIE”);
$ch=curl\u init($url);
curl_setopt($ch,CURLOPT_COOKIEJAR,$ckfile);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$html=curl\u exec($ch);
卷曲关闭($ch);
预匹配(“~~”,$html,$viewstate);

变量转储(文件获取内容($ckfile))// 不使用正则表达式,您可以非常简单地执行此操作:

$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;
$url=”http://www.atb.bergamo.it/ITA/Default.aspx?SEZ=2&PAG=38&MOD=LINTRV";    
$ckfile=tempnam(“/tmp”,“CURLCOOKIE”);
$ch=curl\u init($url);
curl_setopt($ch,CURLOPT_COOKIEJAR,$ckfile);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$html=curl\u exec($ch);
卷曲关闭($ch);
$viewstate=explode('id=“\u viewstate”value=“”,$html);
$viewstate=分解(“,$viewstate[1]);
$viewstate=$viewstate[0];
$sesid=explode(“会话ID”,文件获取内容($ckfile));
$sesid=explode(“\n”,$sesid[1]);
$sesid=trim($sesid[0]);
echo$viewstate。“
”; echo$sesid;

适用于我的测试设置。

不使用正则表达式,您可以非常简单地执行此操作:

$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;
$url=”http://www.atb.bergamo.it/ITA/Default.aspx?SEZ=2&PAG=38&MOD=LINTRV";    
$ckfile=tempnam(“/tmp”,“CURLCOOKIE”);
$ch=curl\u init($url);
curl_setopt($ch,CURLOPT_COOKIEJAR,$ckfile);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$html=curl\u exec($ch);
卷曲关闭($ch);
$viewstate=explode('id=“\u viewstate”value=“”,$html);
$viewstate=分解(“,$viewstate[1]);
$viewstate=$viewstate[0];
$sesid=explode(“会话ID”,文件获取内容($ckfile));
$sesid=explode(“\n”,$sesid[1]);
$sesid=trim($sesid[0]);
echo$viewstate。“
”; echo$sesid;

适用于我的测试设置。

使用HTML解析器提取此类值比使用正则表达式更容易。然而,对于正则表达式,这一点以前已经被问过并回答过了。使用HTML解析器提取这样的值比正则表达式更容易。然而,对于正则表达式,这一点以前已经被问过并回答过了。