结合使用CURL和Simple HTML DOM来删除数据

时间:2013-08-01 11:50:25

标签: php curl simple-html-dom

我已经分配了一个从密码保护的站点废弃数据的任务,我通过CURL完成了,但现在我想在CURL返回的html中获取链接,并转到该链接并从那里获取数据。我将CURL的回复传递给file_get_contents(),但没有效果。这是我的CURL代码。

$ckfile = tempnam("/tmp", "CURLCOOKIE");
$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML,    like Gecko) Chrome/5.0.342.3 Safari/533.2';

$username = "XXXXXX";
$password = "XXXXXX";


$f = fopen('log.txt', 'w'); // file to write request header for debug purpose


$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

 $html = curl_exec($ch);

 curl_close($ch);

preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $html, $viewstate);
preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION"   value="(.*?)" />~', $html, $eventValidation);

$viewstate = $viewstate[1];
$eventValidation = $eventValidation[1];




$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

// Collecting all POST fields
$postfields = array();
$postfields['__EVENTTARGET'] = "";
$postfields['__EVENTARGUMENT'] = "";
$postfields['__VIEWSTATE'] = $viewstate;
$postfields['__EVENTVALIDATION'] = $eventValidation;
$postfields['ctl00$LoginPopup1$Login1$UserName'] = $username;
$postfields['ctl00$LoginPopup1$Login1$Password'] = $password;
$postfields['ctl00$LoginPopup1$Login1$LoginButton'] = 'Log In';

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch); // Get result after login page.

这是简单的html dom代码

$html = file_get_contents($ret);

这是我得到的错误

Warning: file_get_contents(1): failed to open stream: No such file or directory

任何其他建议如何做到这一点将不胜感激。感谢

2 个答案:

答案 0 :(得分:0)

如果您想要发送请求的页面的HTML输出,请尝试将CURLOPT_RETURNTRANSFER设置为true,然后$ret应该包含您的页面的HTML CURL'd了一个。

我不会使用DOMDocument来解析回复,因为页面中的HTML可能格式不正确,DOMDocument会抱怨。

如果您只是在寻找链接,可以在HTML上使用preg_match_all

答案 1 :(得分:0)

与MajorCaiger一样,您需要将CURLOPT_RETURNTRANSFER设置为true,然后将其加载str_get_html

$html = curl_exec($ch);
$doc = str_get_html($html);

即便如此,我认为你没有太大的成功机会,这些asp形式非常棘手。