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

WBOY
Release: 2016-07-21 15:16:19
Original
1311 people have browsed it

看点:
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











}
//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









站点


站点源码




}
//End_php

作者:Zjmainstay

www.bkjia.com true http://www.bkjia.com/PHPjc/325901.html TechArticle 看点: 1、file_get_contents超时控制。 2、页面编码判断。 3、键盘Enter键捕捉响应。 4、键盘event兼容处理。//event = event || window.event; 5、XMLHttp...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!