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

前端之家收集整理的这篇文章主要介绍了PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

看点:
1、file_get_contents超时控制。
2、页面编码判断。
3、键盘Enter键捕捉响应。
4、键盘event兼容处理。//event = event || window.event;
5、XMLHttpRequest 和 jQuery 两种实现方案。
6、页面及源码同时展示。
XMLHttpRequest版本 get_web.PHP
<div class="codetitle"><a style="CURSOR: pointer" data="27651" class="copybut" id="copybut27651" onclick="doCopy('code27651')"> 代码如下:

<div class="codebody" id="code27651">
<?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',$data);
}else if(stripos($data,'gb2312',$charset_pos)) {
echo iconv('gb2312','gbk',$charset_pos)) {
echo iconv('gbk',$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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;

Get Web Page


<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(targetid).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('originwebsite').href = url;
}



<body onkeydown="getkey(event)">
<div class="div
Box">
<input id="inputtext" class="inputBox" type="text" value="" onclick="check(true)" onblur="check(false)">

<div id="clickshow" class="hideBox">
<a id="origin_website" class="a_box" href="#" target="_black">访问原站
<a id="back_a" class="abox" href="#">后退


<div class="clear
Box">

<div id="output_page">



<?PHP
}
//End_PHP

jQuery 版本 get_web.PHP
<div class="codetitle"><a style="CURSOR: pointer" data="19365" class="copybut" id="copybut19365" onclick="doCopy('code19365')"> 代码如下:
<div class="codebody" id="code19365">
<?PHP
header("Content-type: text/html; charset=utf-8");
if(!empty($_POST['input_text'])) {
ini_set('default_socket_timeout',$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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;

Get Web Page


<script type="text/javascript" src="//files.cnblogs.com/Zjmainstay/jquery-1.6.2.min.js">
<script type="text/javascript">
$(document).ready(function(){
$(document).keyup(function(e){
e = e || window.event;
if(e.keyCode == 13 && $("#inputtext").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();
$("#outputpage").empty().html(msg).show();
}
});
});
});




<div class="div
Box">
<input id="inputtext" class="inputBox" type="text" value="">

<div id="clickshow" class="hideBox">
<a id="origin_website" class="a_box" href="#" target="_black">访问原站
<a id="back_a" class="abox" href="#">后退


<div class="clear
Box">
<div class="htmltips hideBox">站点
<div id="output_page">
<div class="htmltips hideBox">站点源码
<textarea id="output_pagehtml" class="hideBox">


<?PHP
}
//End_PHP

作者:Zjmainstay

原文链接:https://www.f2er.com/ajax/27036.html
AJAX获取网页

猜你在找的Ajax相关文章