PHP Simple HTML Dom Parser使用php5+编写,支持以一种超级简单的方式操纵HTML。
* 需要php5以上版本
* 支持html标签纠错
* 支持jQuery风格选择器
* 非常简洁的从HTML中抽取内容 加载整个页面,然后按HTML标记拆成树,然后再通过查找节点,最后读取需要节点的内容文本即可
index.php iframe包含采集页防止采集页js,css等与本站冲突
- <div class="maincon">
- <iframe id="mainiframe" frameborder=0 width="100%" height="960"
- marginheight=0 marginwidth=0 scrolling=no src="http://localhost/appreciate/mobile/recharge"></iframe>
- </div>
iframe中修改采集页的form提交action,提交到本站进行处理,将提交的数据在模拟采集页提交。(一个表单做2次提交
http://hudeyong926.iteye.com/blog/903490
- public function rechargeAction()
- {
- include_once('lib/simple_html_dom.php');
- $html = file_get_html(self::RECHARGE_URL);
- $post_url = $html->find('form', 0)->action; //form action
- $html->getElementById('mobileNo')->outertext = $html->find('input[id=mobileNo]', 0)->outertext . "<input type='hidden' name='post_url' value='$post_url'>"; //form中传递采集post_url
- //$html->find('div.more-com',0)->outertext = '';
- $html->find('form', 0)->setAttribute('action', 'http://localhost/appreciate/mobile/rechargePost'); //修改form action
- $html->find('td a', 1)->setAttribute('href', 'http://localhost/appreciate/mobile/history'); //修改 订单查询链接
- $ereg_rule_src_script = '/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/bank\/ddl\/js\/utility\/utility.index.js?/i';
- foreach ($html->find('script') as $script) { //正则替换js
- $script->src = preg_replace($ereg_rule_src_script, 'http://www.test.com/js/lib/dianyin_utility_index.js', $script->src);
- }
- $content = $html->save();
- $html->clear();
- unset($html);
- die($content);
- }
采集提交后页面
simple_html_dom修改后
- public function rechargeComfirmPage($post_url, $pdata){
- include_once("lib/Snoopy.class.php");
- $snoopy = new Snoopy; //Snoopy post提交
- $snoopy->fetch(self::RECHARGE_URL); //获取所有内容
- $snoopy->setcookies();
- $cookies = $snoopy->cookies;
- $snoopy->cookies = $cookies;
- $snoopy->submit($post_url, $pdata);//$pdata为提交的数组,$post_url采集提交url
- return $snoopy->results;
- }
- public function rechargePostAction(){
- $pdata['tranCode'] = $this->getRequest()->getPost('tranCode');
- $pdata['userCode'] = $this->getRequest()->getPost('userCode');
- $pdata['runName'] = $this->getRequest()->getPost('runName');
- $post_url = $this->getRequest()->getPost('post_url');
- $this->save2db();
- $results = $this->rechargeComfirmPage($post_url, $pdata);
- include_once('lib/simple_html_dom.php');
- $html = str_get_html($results);//采集提交后页面
- //修改UI
- $rate = Mage::helper('recharge/data')->getCurrencyToMoney();
- $ttt= $this->getLayout()->createBlock('virtualcurrency/customer_accounts_detail')->getCurrentMoney();
- $html->find('div[id=testPoints]',0)->innertext = $html->find('div[id=testPoints]',0)->innertext*$rate."<input type='button'>";
- $html->find('div[id=test]',0)->innertext='<table><tr>
- <td width="30%" align="right"><label class="form-label">您的数:</label></td>
- <td align="left">
- <p class="B Font14 ml10 ">
- '.$ttt.'
- </p>
- </td>
- </tr>
- <tr>
- <td width="30%" align="right"><label class="form-label">绑定手机号:</label></td>
- <td align="left">
- <p class="B Font14 ml10 ">
- '.$this->_getCustomer()->getMobile().'
- </p>
- </td>
- </tr></table>';
- die($html);
- }
时间: 2024-09-12 10:13:31