PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)


Posted in PHP onAugust 31, 2012

看点:
1、file_get_contents超时控制。
2、页面编码判断。
3、键盘Enter键捕捉响应。
4、键盘event兼容处理。//event = event || window.event;
5、XMLHttpRequest 和 jQuery 两种实现方案。
6、页面及源码同时展示。
XMLHttpRequest版本 get_web.php

<?php 
header("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:32 
font-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 onkeydown="get_key(event)"> 
<div class="div_box"> 
<input id="input_text" class="input_box" type="text" value="" onclick="check_(true)" onblur="check_(false)"></input> 
<input type="button" class="button_box" onclick="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

jQuery 版本 get_web.php
<?php 
header("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:32 
font-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

作者:Zjmainstay
PHP 相关文章推荐
Windows下PHP的任意文件执行漏洞
Oct 09 PHP
php循环输出数据库内容的代码
May 24 PHP
PHP clearstatcache()函数详解
Mar 02 PHP
PHPWind与Discuz截取字符函数substrs与cutstr性能比较
Dec 05 PHP
PHP命名空间(Namespace)的使用详解
May 04 PHP
php计算当前程序执行时间示例
Apr 24 PHP
PHP生成自适应大小的缩略图类及使用方法分享
May 06 PHP
CI框架无限级分类+递归的实现代码
Nov 01 PHP
PHP实现可精确验证身份证号码的工具类示例
May 31 PHP
PHP如何通过表单直接提交大文件详解
Jan 08 PHP
Laravel find in set排序实例
Oct 09 PHP
laravel框架路由分组,中间件,命名空间,子域名,路由前缀实例分析
Feb 18 PHP
PHP的简易冒泡法代码分享
Aug 28 #PHP
php 解决旧系统 查出所有数据分页的类
Aug 27 #PHP
PHP实现手机归属地查询API接口实现代码
Aug 27 #PHP
PHP 图片水印类代码
Aug 27 #PHP
PHP setTime 设置当前时间的代码
Aug 27 #PHP
PHP 透明水印生成代码
Aug 27 #PHP
无JS,完全php面向过程数据分页实现代码
Aug 27 #PHP
You might like
第九节--绑定
2006/11/16 PHP
PHP 循环列出目录内容的函数代码
2010/05/26 PHP
在WAMP环境下搭建ZendDebugger php调试工具的方法
2011/07/18 PHP
根据分辩率调用不同的CSS.
2007/01/08 Javascript
基于JavaScript实现继承机制之原型链(prototype chaining)的详解
2013/05/07 Javascript
使用js解决由border属性引起的div宽度问题
2013/11/26 Javascript
jQuery+css3实现文字跟随鼠标的上下抖动
2015/07/31 Javascript
在JavaScript中模拟类(class)及类的继承关系
2016/05/20 Javascript
深入剖析JavaScript面向对象编程
2016/07/12 Javascript
在web中js实现类似excel的表格控件
2016/09/01 Javascript
Javascript 引擎工作机制详解
2016/11/30 Javascript
react实现pure render时bind(this)隐患需注意!
2017/03/09 Javascript
JS插件clipboard.js实现一键复制粘贴功能
2020/12/04 Javascript
微信小程序利用云函数获取手机号码
2019/12/17 Javascript
[02:47]2018年度DOTA2最佳辅助位选手4号位-完美盛典
2018/12/17 DOTA
Python常见加密模块用法分析【MD5,sha,crypt模块】
2017/05/24 Python
python生成词云的实现方法(推荐)
2017/06/13 Python
python机器学习之贝叶斯分类
2018/03/26 Python
python如何创建TCP服务端和客户端
2018/08/26 Python
python 批量添加的button 使用同一点击事件的方法
2019/07/17 Python
PyCharm汉化安装及永久激活详细教程(靠谱)
2020/01/16 Python
Python socket连接中的粘包、精确传输问题实例分析
2020/03/24 Python
CSS3按钮鼠标悬浮实现光圈效果源码
2016/09/11 HTML / CSS
美国蔬菜和植物种子公司:Burpee
2017/02/01 全球购物
英国奢侈品概念店:Base Blu
2019/05/16 全球购物
bonprix荷兰网上商店:便宜的服装、鞋子和家居用品
2020/07/04 全球购物
在Ajax应用中信息是如何在浏览器和服务器之间传递的
2016/05/31 面试题
计算机软件个人的自荐信范文
2013/12/01 职场文书
2016圣诞节贺卡寄语
2015/12/07 职场文书
七年级之家长会发言稿范文
2019/09/04 职场文书
PHP控制循环操作的时间
2021/04/01 PHP
golang 实现菜单树的生成方式
2021/04/28 Golang
Python面向对象之成员相关知识总结
2021/06/24 Python
python脚本框架webpy模板赋值实现
2021/11/20 Python
使用CSS定位HTML元素的实现方法
2022/07/07 HTML / CSS
在windows server 2012 r2中安装mysql的详细步骤
2022/07/23 Servers