我正在尝试使用php解析网页并在登录后卷曲,但我没有弄错。我已经阅读了十几个主题但没有成功。
我的第一步是在登录页面上发布以获取cookie并将其保存在文件中。 我的第二步是使用此cookie访问第二页并解析它。
这是我的代码,感谢您的帮助!
<?php
$cookiefile = "/var/www/html/a-cki.txt";
$loginUrl = 'myurl/login.php';
$parseUrl = 'myurl/index.php';
$postinfo = "username=xxx&password=xxx&login=xxx";
//Login and save cookie
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_exec($ch);
curl_close($ch);
// Download another page while reusing the cookie
$parse = curl_init();
curl_setopt( $parse, CURLOPT_URL, $parseUrl);
curl_setopt( $parse, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $parse, CURLOPT_HEADER, 1);
curl_setopt( $parse, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $parse, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt( $parse, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt( $parse, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $parse, CURLOPT_SSL_VERIFYHOST, false);
$parseResponse = curl_exec( $parse );
curl_close($parse);
?>
答案 0 :(得分:0)
解决了ArminŠupuk评论!
$postinfo = "username=xxx&password=xxx&login=xxx";
//Login and save cookie
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_exec($ch);
curl_close($ch);
// Download another page while reusing the cookie
$parse = curl_init();
curl_setopt( $parse, CURLOPT_URL, $parseUrl);
curl_setopt( $parse, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $parse, CURLOPT_HEADER, 1);
curl_setopt( $parse, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $parse, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt( $parse, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $parse, CURLOPT_SSL_VERIFYHOST, false);
$parseResponse = curl_exec( $parse );
curl_close($parse);
?>