PHP 利用AJAX获取网页并输出(原创自Zjmainstay)
看点:
1、file_get_contents超时控制。
2、页面编码判断。
3、键盘Enter键捕捉响应。
4、键盘event兼容处理。//event = event || window.event;
5、XMLHttpRequest 和 jQuery 两种实现方案。
6、页面及源码同时展示。
<?phpheader("Content-type: text/html; charset=utf-8");if(!empty($_POST['input_text'])) {ini_set('default_socket_timeout', 10);if(!$data = file_get_contents($_POST['input_text'])) {echo "Time out!";return ;}$charset_pos = stripos($data,'charset');if($charset_pos) {if(stripos($data,'utf-8',$charset_pos)) {echo iconv('utf-8','utf-8',$data);}else if(stripos($data,'gb2312',$charset_pos)) {echo iconv('gb2312','utf-8',$data);}else if(stripos($data,'gbk',$charset_pos)) {echo iconv('gbk','utf-8',$data);}return;}echo $data;}else { ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Get Web Page</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-CN" /><script type="text/javascript">function createXMLHTTP(){try{var request = new XMLHttpRequest();}catch(e1){var arrVersions = ["Microsoft.XMLHTTP","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp.5.0"];for(var i=0;i < arrVersions.length;i++){try{request = new ActiveXObject(arrVersions[i]);}catch(e2){request = false;}}}return request;}function ajax_post(url, params, target_id){request = new createXMLHTTP();request.onreadystatechange = function() {if (this.readyState == 4)if (this.status == 200)if (this.responseText != null)document.getElementById(target_id).innerHTML = this.responseText;}request.open("POST", url, true);request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");request.setRequestHeader("Content-length", params.length);request.setRequestHeader("Connection", "close");request.send(params);}var checked = false;function check_(value) {checked = value;}function get_key(event) {event = event || window.event;if(event.keyCode==13 && checked != false){var url = document.getElementById('input_text').value;if(url != '') {get_page();}else {document.getElementById('input_text').onfocus();return false;}}}function get_page() {var url = document.getElementById('input_text').value;if(!url) {return false;}else {if(document.getElementById('output_page').innerHTML != '') {document.getElementById('output_page').innerHTML = '';}}if(url.indexOf('http://') == -1) {url = 'http://'+url;}ajax_post('<?php echo $_SERVER['PHP_SELF']; ?>','input_text='+url,'output_page');document.getElementById('click_show').style.display = 'block';document.getElementById('back_a').href = document.location.href;document.getElementById('origin_website').href = url;}</script><style>.div_box{margin-top:10px;}.input_box{border:1px solid;margin-left:10px;margin-top:2px;height:15px;float:left;size:32font-size: 14px;}.button_box{float:left;height:23px;padding-bottom:3px;}.hide_box{display:none; }.a_box{margin-left:10px;margin-top:3px;height:15px;float:left;font-size: 14px;}.clear_box{height:50px;}</style></head><body οnkeydοwn="get_key(event)"><div class="div_box"><input id="input_text" class="input_box" type="text" value="" οnclick="check_(true)" οnblur="check_(false)"></input><input type="button" class="button_box" οnclick="get_page()" value="Get it!" ></input><div id="click_show" class="hide_box"><a id="origin_website" class="a_box" href="#" target="_black">访问原站</a><a id="back_a" class="a_box" href="#">后退</a></div></div><div class="clear_box"></div><div id="output_page"></div></body></html> <?php}//End_php
<?phpheader("Content-type: text/html; charset=utf-8");if(!empty($_POST['input_text'])) {ini_set('default_socket_timeout', 10);if(!$data = file_get_contents($_POST['input_text'])) {echo "Time out!";return ;}$charset_pos = stripos($data,'charset');if($charset_pos) {if(stripos($data,'utf-8',$charset_pos)) {echo iconv('utf-8','utf-8',$data);}else if(stripos($data,'gb2312',$charset_pos)) {echo iconv('gb2312','utf-8',$data);}else if(stripos($data,'gbk',$charset_pos)) {echo iconv('gbk','utf-8',$data);}return;}echo $data;}else { ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Get Web Page</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-CN" /><script type="text/javascript" src="http://files.cnblogs.com/Zjmainstay/jquery-1.6.2.min.js"></script><script type="text/javascript">$(document).ready(function(){$(document).keyup(function(e){e = e || window.event; if(e.keyCode == 13 && $("#input_text").val() != '') {$(".button_box").click();}});$(".button_box").click(function(){if($("#input_text").val() == '') {$("#input_text").addClass('errorTips').focus();return false;}else {$("#input_text").removeClass('errorTips');}$.ajax({url: '<?php echo $_SERVER['PHP_SELF'] ?>',data: 'input_text='+$("#input_text").val(),type:'POST',success:function(msg){$(".html_tips").show();$("#origin_website").attr('href',$("#input_text").val());$("#back_a").attr('href',document.location.href);$("#click_show").show();$("#output_page_html").empty().val(msg).css({height:parseInt($(document).height()-100)}).show();$("#output_page").empty().html(msg).show();}});});});</script><style>.div_box{margin-top:10px;}.input_box{border:1px solid;margin-left:10px;margin-top:2px;height:15px;float:left;size:32font-size: 14px;}.button_box{float:left;height:23px;padding-bottom:3px;}.hide_box{display:none; }.a_box{margin-left:10px;margin-top:3px;height:15px;float:left;font-size: 14px;}.clear_box{height:50px;}.error_tips{border:1px solid red;}#output_page_html{width:960px;margin:0 auto;}.html_tips{float: left;margin: 0 21px;font-size:1.8em;}</style></head><body><div class="div_box"><input id="input_text" class="input_box" type="text" value=""></input><input type="button" class="button_box" value="Get it!" ></input><div id="click_show" class="hide_box"><a id="origin_website" class="a_box" href="#" target="_black">访问原站</a><a id="back_a" class="a_box" href="#">后退</a></div></div><div class="clear_box"></div><div class="html_tips hide_box">站点</div><div id="output_page"></div><div class="html_tips hide_box">站点源码</div><textarea id="output_page_html" class="hide_box"></textarea> </body></html> <?php} //End_php
转载于:https://www.cnblogs.com/Zjmainstay/archive/2012/08/06/Ajax_getPage.html
PHP 利用AJAX获取网页并输出(原创自Zjmainstay)相关推荐
- php获取网页输出,PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)_PHP教程
看点: 1.file_get_contents超时控制. 2.页面编码判断. 3.键盘Enter键捕捉响应. 4.键盘event兼容处理.//event = event || window.event ...
- 利用ajax获取数据对学生表进行简单的增删改查
前言 本次练习没有使用前端框架,默认以表格方式拼接渲染数据,后端数据返回没有采用规范的数据形式返回 默认你能用springboot-mybatis从后端获取到数据 spring boot 静态资源处理 ...
- ajax获取网页新闻,基于Ajax的新闻网页动态数据的抓取方法及系统
主权项: 1.基于Ajax的新闻网页动态数据的抓取方法,其特征是,包括如下步骤:步骤(101):建立新闻网页爬取内容数据库,设置新闻网页爬取内容数据库的编码方式:获得待抓取新闻网页的新闻列表页面的UR ...
- Obsidian之利用MaoXian获取网页信息
Using extension file to install MaoXian Web Clipper 起因:正在看一篇文章,认为写的相当不错: 定制自己的CentOS,制作ISO镜像文件_evglo ...
- 利用HttpClient 获取网页数据java代码模版
2019独角兽企业重金招聘Python工程师标准>>> HttpClientBuilder httpClientBuilder=HttpClients.custom();HttpCl ...
- C# 网络编程之webBrowser获取网页url和下载网页中图片
该文章主要是通过C#网络编程的webBrowser获取网页中的url并简单的尝试下载网页中的图片,主要是为以后网络开发的基础学习.其中主要的通过应用程序结合网页知识.正则表达式实现浏览.获取url.下 ...
- c#正则表达式取出数据库中带html标签的内容,C#用正则表达式 获取网页源代码标签的属性或值...
1.有url获取到网页源代码: using System.Web; using System.IO; using System.Net; private void GetHtmlinfo(string ...
- python urlopen 乱码_Python 2.7.3 urllib2.urlopen 获取网页出现乱码解决方案
出现乱码的原因是,网页服务端有bug,它硬性使用使用某种特定的编码方案,而并没有按照客户端的请求头的编码要求来发送编码. 解决方案:使用chardet来猜测网页编码. 1.去chardet官网下载ch ...
- python获取网页源码不完整_python和Ajax在一起了?真的???
Ajax动态网页加载爬取新浪微博某关键词下的信息 前言 有些时候我们使用浏览器查看页面正常显示的数据与使用requests抓取页面html得到的数据不一致,这是因为requests获取的是原始的HTM ...
最新文章
- 浅谈RPA 在银行领域的十个场景应用
- python代码教程-【Python】Python3纯代码极简教程
- 双系统XP和ubuntu,升级ubuntu出现no such device grub rescue
- USTC English Club Note20211208
- 第十届蓝桥杯省赛JavaC组真题——详细答案对照(完整版-包含打扫机器人的视频全过程讲解与编码内容对照)
- runtime无法执行grep_如何使管道使用Runtime.exec()?
- Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)
- KMP算法之NEXT数组代码原理分析 - 数据结构和算法38
- HDU - 6464 免费送气球(线段树二分)
- MySQL注释(转)
- 英语数字听力训练精灵
- android netd firewall 分析,第2章 深入理解Netd
- 淘宝模块中宝贝跳转链接
- 与卿共赴鸿蒙是什么意思,《山河令》看来周子舒是真的很爱温客行,君心似我心,此生无憾...
- Red Gate 破解
- 硅光电子器件模拟:“RSoft光电器件设计仿真技术与应用”
- 疾病研究:重症肌无力医师指南
- 【附源码】计算机毕业设计SSM泰兴市公交信息系统
- 人工智能之数学基础篇—线性代数基础(上)
- 从PDF文件中提取嵌入的MP4视频文件
热门文章
- Kubectl 常用命令, 开发人员常用k8s命令
- 使用JPA进行Update操作 @Query注解的用法,JPL
- Python 生成器总结
- win10 4步快速安装vue
- SpringBoot (三) :SpringBoot使用Freemarker模板引擎渲染web视图
- flask_模拟请求post,get
- LeetCode简单题之通过翻转子数组使两个数组相等
- AIoT开放平台及应用
- HiLink LiteOS IoT芯片 让IoT开发简单高效
- 激光雷达Lidar Architecture and Lidar Design(下)