PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)
程序员文章站
2022-05-14 12:54:20
看点: 1、file_get_contents超时控制。 2、页面编码判断。 3、键盘enter键捕捉响应。 4、键盘event兼容处理。//event = event |...
看点:
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
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面向对象 字段的声明与使用
推荐阅读
-
用php实现的获取网页中的图片并保存到本地的代码
-
PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)
-
用php实现的获取网页中的图片并保存到本地的代码_PHP
-
0x00000050电脑蓝屏代码 用php实现的获取网页中的图片并保存到本地的代码
-
用php实现的获取网页中的图片并保存到本地的代码
-
PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)_PHP
-
PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)_PHP
-
PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)_php技巧
-
用php实现的获取网页中的图片并保存到本地的代码_PHP教程
-
PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)_php技巧