Heim  >  Artikel  >  Backend-Entwicklung  >  PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)_PHP教程

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

WBOY
WBOYOriginal
2016-07-21 15:16:191331Durchsuche

看点:
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.comtruehttp://www.bkjia.com/PHPjc/325901.htmlTechArticle看点: 1、file_get_contents超时控制。 2、页面编码判断。 3、键盘Enter键捕捉响应。 4、键盘event兼容处理。//event = event || window.event; 5、XMLHttp...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn