php采集文章获取链接ajax翻页,PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)

看点:

1、file_get_contents超时控制。

2、页面编码判断。

3、键盘Enter键捕捉响应。

4、键盘event兼容处理。//event = event || window.event;

5、XMLHttpRequest 和 jQuery 两种实现方案。

6、页面及源码同时展示。

XMLHttpRequest版本 get_web.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 {

?>

Get Web Page

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;

}

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

}

}

//End_php

jQuery 版本 get_web.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 {

?>

Get Web Page

$(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();

}

});

});

});

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

}

站点
站点源码

}

//End_php

作者:Zjmainstay

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值