PHP uses Ajax to get web pages and output the implementation code (ZJMAINSTAY) _php tips

Source: Internet
Author: User
Watch:
1, file_get_contents timeout control.
2, the page coding judgment.
3, the keyboard enter key to capture the response.
4, keyboard event compatible processing. Event = Event | | window.event;
5, XMLHttpRequest and jQuery two implementation scenarios.
6, the page and the source code at the same time display.
XMLHttpRequest version get_web.php
Copy Code code as follows:

<?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 ">
<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>
<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" > Access to the original station </a>
<a id= "back_a" class= "A_box" href= "#" > Back </a>
</div>
</div>
<div class= "Clear_box" ></div>
<div id= "Output_page" ></div>
</body>
<?php
}
end_php

JQuery version get_web.php
Copy Code code as follows:

<?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 ">
<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 = && $ ("#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>
<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" > Access to the original station </a>
<a id= "back_a" class= "A_box" href= "#" > Back </a>
</div>
</div>
<div class= "Clear_box" ></div>
<div class= "Html_tips hide_box" > Site </div>
<div id= "Output_page" ></div>
<div class= "Html_tips hide_box" > site source </div>
<textarea id= "output_page_html" class= "Hide_box" ></textarea>
</body>
<?php
}
end_php

Author: Zjmainstay

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.