• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-26) 35次浏览 已收录 0个评论

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

 <BR><?php <BR>header("Content-type: text/html; charset=utf-8"); <BR>if(!empty($_POST['input_text'])) { <BR>ini_set('default_socket_timeout', 10); <BR>if(!$data = file_get_contents($_POST['input_text'])) { <BR>echo "Time out!"; <BR>return ; <BR>} <BR>$charset_pos = stripos($data,'charset'); <BR>if($charset_pos) { <BR>if(stripos($data,'utf-8',$charset_pos)) { <BR>echo iconv('utf-8','utf-8',$data); <BR>}else if(stripos($data,'gb2312',$charset_pos)) { <BR>echo iconv('gb2312','utf-8',$data); <BR>}else if(stripos($data,'gbk',$charset_pos)) { <BR>echo iconv('gbk','utf-8',$data); <BR>} <BR>return; <BR>} <BR>echo $data; <BR>}else { <BR>?> <BR> <BR> <BR> <BR><title>Get Web Page</title> <BR><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <BR><meta http-equiv="Content-Language" content="zh-CN" /> <BR><script type="text/javascript"> <BR>function createXMLHTTP() <BR>{ <BR>try <BR>{ <BR>var request = new XMLHttpRequest(); <BR>} <BR>catch(e1) <BR>{ <BR>var arrVersions = ["Microsoft.XMLHTTP","MSXML2.XMLHttp.4.0", <BR>"MSXML2.XMLHttp.3.0","MSXML2.XMLHttp.5.0"]; <BR>for(var i=0;i < arrVersions.length;i++){ <BR>try{ <BR>request = new ActiveXObject(arrVersions[i]); <BR>}catch(e2){ <BR>request = false; <BR>} <BR>} <BR>} <BR>return request; <BR>} <BR>function ajax_post(url, params, target_id) <BR>{ <BR>request = new createXMLHTTP(); <BR>request.onreadystatechange = function() { <BR>if (this.readyState == 4) <BR>if (this.status == 200) <BR>if (this.responseText != null) <BR>document.getElementById(target_id).innerHTML = this.responseText; <BR>} <BR>request.open("POST", url, true); <BR>request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); <BR>request.setRequestHeader("Content-length", params.length); <BR>request.setRequestHeader("Connection", "close"); <BR>request.send(params); <BR>} <BR>var checked = false; <BR>function check_(value) { <BR>checked = value; <BR>} <BR>function get_key(event) { <BR>event = event || window.event; <BR>if(event.keyCode==13 && checked != false) <BR>{ <BR>var url = document.getElementById('input_text').value; <BR>if(url != '') { <BR>get_page(); <BR>}else { <BR>document.getElementById('input_text').onfocus(); <BR>return false; <BR>} <BR>} <BR>} <BR>function get_page() { <BR>var url = document.getElementById('input_text').value; <BR>if(!url) { <BR>return false; <BR>}else { <BR>if(document.getElementById('output_page').innerHTML != '') { <BR>document.getElementById('output_page').innerHTML = ''; <BR>} <BR>} <BR>if(url.indexOf('http://') == -1) { <BR>url = 'http://'+url; <BR>} <BR>ajax_post( <BR>'<?php echo $_SERVER['PHP_SELF']; ?>', <BR>'input_text='+url, <BR>'output_page' <BR>); <BR>document.getElementById('click_show').style.display = 'block'; <BR>document.getElementById('back_a').href = document.location.href; <BR>document.getElementById('origin_website').href = url; <BR>} <BR></script> <BR><style> <BR>.div_box{ <BR>margin-top:10px; <BR>} <BR>.input_box{ <BR>border:1px solid; <BR>margin-left:10px; <BR>margin-top:2px; <BR>height:15px; <BR>float:left; <BR>size:32 <BR>font-size: 14px; <BR>} <BR>.button_box{ <BR>float:left; <BR>height:23px; <BR>padding-bottom:3px; <BR>} <BR>.hide_box{ <BR>display:none; <BR>} <BR>.a_box{ <BR>margin-left:10px; <BR>margin-top:3px; <BR>height:15px; <BR>float:left; <BR>font-size: 14px; <BR>} <BR>.clear_box{ <BR>height:50px; <BR>} <BR></style> <BR> <BR><body onkeydown="get_key(event)"> <BR><div class="div_box"> <BR> <BR> <BR><div id="click_show" class="hide_box"> <BR>访问原站 <BR>后退 <BR>







<?php
}
//End_php


jQuery 版本 get_web.php

 <BR><?php <BR>header("Content-type: text/html; charset=utf-8"); <BR>if(!empty($_POST['input_text'])) { <BR>ini_set('default_socket_timeout', 10); <BR>if(!$data = file_get_contents($_POST['input_text'])) { <BR>echo "Time out!"; <BR>return ; <BR>} <BR>$charset_pos = stripos($data,'charset'); <BR>if($charset_pos) { <BR>if(stripos($data,'utf-8',$charset_pos)) { <BR>echo iconv('utf-8','utf-8',$data); <BR>}else if(stripos($data,'gb2312',$charset_pos)) { <BR>echo iconv('gb2312','utf-8',$data); <BR>}else if(stripos($data,'gbk',$charset_pos)) { <BR>echo iconv('gbk','utf-8',$data); <BR>} <BR>return; <BR>} <BR>echo $data; <BR>}else { <BR>?> <BR> <BR> <BR> <BR><title>Get Web Page</title> <BR><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <BR><meta http-equiv="Content-Language" content="zh-CN" /> <BR><script type="text/javascript" src="http://files.cnblogs.com/Zjmainstay/jquery-1.6.2.min.js"></script> <BR><script type="text/javascript"> <BR>$(document).ready(function(){ <BR>$(document).keyup(function(e){ <BR>e = e || window.event; <BR>if(e.keyCode == 13 && $("#input_text").val() != '') { <BR>$(".button_box").click(); <BR>} <BR>}); <BR>$(".button_box").click(function(){ <BR>if($("#input_text").val() == '') { <BR>$("#input_text").addClass('errorTips').focus(); <BR>return false; <BR>}else { <BR>$("#input_text").removeClass('errorTips'); <BR>} <BR>$.ajax({ <BR>url: '<?php echo $_SERVER['PHP_SELF'] ?>', <BR>data: 'input_text='+$("#input_text").val(), <BR>type:'POST', <BR>success:function(msg){ <BR>$(".html_tips").show(); <BR>$("#origin_website").attr('href',$("#input_text").val()); <BR>$("#back_a").attr('href',document.location.href); <BR>$("#click_show").show(); <BR>$("#output_page_html").empty().val(msg).css({height:parseInt($(document).height()-100)}).show(); <BR>$("#output_page").empty().html(msg).show(); <BR>} <BR>}); <BR>}); <BR>}); <BR></script> <BR><style> <BR>.div_box{ <BR>margin-top:10px; <BR>} <BR>.input_box{ <BR>border:1px solid; <BR>margin-left:10px; <BR>margin-top:2px; <BR>height:15px; <BR>float:left; <BR>size:32 <BR>font-size: 14px; <BR>} <BR>.button_box{ <BR>float:left; <BR>height:23px<strong>(本文来源gaodai#ma#com搞@@代~&码网</strong><pre>搞代gaodaima码

;
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>

<body>





访问原站
后退



站点



站点源码





<?php
}
//End_php


作者:Zjmainstay


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)_php技巧

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址