欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP 利用AJAX獲取網(wǎng)頁并輸出的實(shí)現(xiàn)代碼(Zjmainstay)

 更新時間:2012年08月31日 11:27:39   作者:  
PHP 利用AJAX獲取網(wǎng)頁并輸出的實(shí)現(xiàn)代碼,需要的朋友可以參考下
看點(diǎn):
1、file_get_contents超時控制。
2、頁面編碼判斷。
3、鍵盤Enter鍵捕捉響應(yīng)。
4、鍵盤event兼容處理。//event = event || window.event;
5、XMLHttpRequest 和 jQuery 兩種實(shí)現(xiàn)方案。
6、頁面及源碼同時展示。
XMLHttpRequest版本 get_web.php
復(fù)制代碼 代碼如下:

<?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
復(fù)制代碼 代碼如下:

<?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">站點(diǎn)</div>
<div id="output_page"></div>
<div class="html_tips hide_box">站點(diǎn)源碼</div>
<textarea id="output_page_html" class="hide_box"></textarea>
</body>
</html>
<?php
}
//End_php

作者:Zjmainstay

相關(guān)文章

  • 自己寫的兼容低于PHP 5.5版本的array_column()函數(shù)

    自己寫的兼容低于PHP 5.5版本的array_column()函數(shù)

    這篇文章主要介紹了自己寫的兼容低于PHP 5.5版本的array_column()函數(shù),array_column是PHP 5.5新增函數(shù),有時在低版本中也可能要用到,需要的朋友可以參考下
    2014-10-10
  • PHP類的反射用法實(shí)例

    PHP類的反射用法實(shí)例

    這篇文章主要介紹了PHP類的反射用法,實(shí)例講述了反射類的常用操作,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-11-11
  • PHP5.5和之前的版本empty函數(shù)的不同之處

    PHP5.5和之前的版本empty函數(shù)的不同之處

    這篇文章主要介紹了PHP5.5和之前的版本empty函數(shù)的不同之處,開發(fā)時需要注意,假如你的本地開發(fā)PHP版本是PHP5.5以上,而服務(wù)器低于PHP5.5,更要注意這個問題,需要的朋友可以參考下
    2014-06-06
  • PHP設(shè)計模式之裝飾器模式實(shí)例詳解

    PHP設(shè)計模式之裝飾器模式實(shí)例詳解

    這篇文章主要介紹了PHP設(shè)計模式之裝飾器模式,簡單介紹了裝飾器模式的概念、功能并結(jié)合實(shí)例形式詳細(xì)分析了php實(shí)現(xiàn)與使用裝飾器模式的相關(guān)操作技巧,需要的朋友可以參考下
    2018-02-02
  • PHP方法的返回值示例詳解

    PHP方法的返回值示例詳解

    這篇文章主要給大家介紹了關(guān)于PHP方法返回值的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • PHP導(dǎo)出MySQL數(shù)據(jù)到Excel文件(fputcsv)

    PHP導(dǎo)出MySQL數(shù)據(jù)到Excel文件(fputcsv)

    經(jīng)常會碰到需要從數(shù)據(jù)庫中導(dǎo)出數(shù)據(jù)到Excel文件,用一些開源的類庫,比如PHPExcel,確實(shí)比較容易實(shí)現(xiàn),但對大量數(shù)據(jù)的支持很不好,很容易到達(dá)PHP內(nèi)存使用上限。
    2011-07-07
  • Thinkphp6 配置并使用redis圖文詳解

    Thinkphp6 配置并使用redis圖文詳解

    這篇文章主要介紹了Thinkphp6 配置并使用redis的方法,結(jié)合實(shí)例形式詳細(xì)分析了Redis的安裝、配置以及thinkphp6操作Redis的基本技巧,需要的朋友可以參考下
    2023-06-06
  • php中使用接口實(shí)現(xiàn)工廠設(shè)計模式的代碼

    php中使用接口實(shí)現(xiàn)工廠設(shè)計模式的代碼

    php實(shí)現(xiàn)工廠設(shè)計模式,使用接口實(shí)現(xiàn),表面上接口沒有什么用,因?yàn)閜hp是類型自動轉(zhuǎn)換的。實(shí)現(xiàn)上使用接口可以約束類的定義,從而實(shí)現(xiàn)一致的訪問
    2012-06-06
  • 通過PHP current函數(shù)獲取未知字符鍵名數(shù)組第一個元素的值

    通過PHP current函數(shù)獲取未知字符鍵名數(shù)組第一個元素的值

    在開發(fā)中經(jīng)常遇到這樣問題,獲取數(shù)組第一個元素的值,如果是數(shù)字索引那還好,直接$array[0],如果鍵名是字符串,你又未知這個字符串呢?用current()函數(shù)就可以做到
    2013-06-06
  • php實(shí)現(xiàn)保存submit內(nèi)容之后禁止刷新

    php實(shí)現(xiàn)保存submit內(nèi)容之后禁止刷新

    這篇文章主要介紹了php保存submit內(nèi)容之后禁止刷新的具體實(shí)現(xiàn),需要的朋友可以參考下
    2014-03-03

最新評論