怎么在PHP中使用AJAX對網(wǎng)頁進(jìn)行獲取-創(chuàng)新互聯(lián)

怎么在PHP中使用AJAX對網(wǎng)頁進(jìn)行獲取?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)瓊海免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

看點(diǎn):
1、file_get_contents超時(shí)控制。
2、頁面編碼判斷。
3、鍵盤Enter鍵捕捉響應(yīng)。
4、鍵盤event兼容處理。//event = event || window.event;
5、XMLHttpRequest 和 jQuery 兩種實(shí)現(xiàn)方案。
6、頁面及源碼同時(shí)展示。
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" "/tupian/20230522/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" "/tupian/20230522/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="/tupian/20230522/sorry.cnblogs.com
<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


關(guān)于怎么在PHP中使用AJAX對網(wǎng)頁進(jìn)行獲取問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

分享題目:怎么在PHP中使用AJAX對網(wǎng)頁進(jìn)行獲取-創(chuàng)新互聯(lián)
URL地址:http://bm7419.com/article44/dgosee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、建站公司、網(wǎng)頁設(shè)計(jì)公司、App設(shè)計(jì)外貿(mào)建站、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司