Rumah > php教程 > php手册 > teks badan

PHP 利用AJAX获取网页并输出(原创自Zjmainstay)

WBOY
Lepaskan: 2016-06-06 19:47:28
asal
1079 orang telah melayarinya

看点: 1、file_get_contents超时控制。 2、页面编码判断。 3、键盘Enter键捕捉响应。 4、键盘event兼容处理。// event = event || window. event; 5、 XMLHttpRequest 和 jQuery 两种实现方案。 6、页面及源码同时展示。 XMLHttpRequest版本 get_web.php ?

 看点:

      1、file_get_contents超时控制。

      2、页面编码判断。

      3、键盘Enter键捕捉响应。

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

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

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

PHP 利用AJAX获取网页并输出(原创自Zjmainstay)PHP 利用AJAX获取网页并输出(原创自Zjmainstay)XMLHttpRequest版本 get_web.php

<span>php
    </span><span>header</span>("Content-type: text/html; charset=utf-8"<span>);
    </span><span>if</span>(!<span>empty</span>(<span>$_POST</span>['input_text'<span>])) {
        </span><span>ini_set</span>('default_socket_timeout', 10<span>);
        </span><span>if</span>(!<span>$data</span> = <span>file_get_contents</span>(<span>$_POST</span>['input_text'<span>])) {
            </span><span>echo</span> "Time out!"<span>;
            </span><span>return</span><span> ;
        }
        </span><span>$charset_pos</span> = <span>stripos</span>(<span>$data</span>,'charset'<span>);
        </span><span>if</span>(<span>$charset_pos</span><span>) {
            </span><span>if</span>(<span>stripos</span>(<span>$data</span>,'utf-8',<span>$charset_pos</span><span>)) {
                </span><span>echo</span> <span>iconv</span>('utf-8','utf-8',<span>$data</span><span>);
            }</span><span>else</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'gb2312',<span>$charset_pos</span><span>)) {
                </span><span>echo</span> <span>iconv</span>('gb2312','utf-8',<span>$data</span><span>);
            }</span><span>else</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'gbk',<span>$charset_pos</span><span>)) {
                </span><span>echo</span> <span>iconv</span>('gbk','utf-8',<span>$data</span><span>);
            }
            </span><span>return</span><span>;
        }
        </span><span>echo</span> <span>$data</span><span>;
    }</span><span>else</span><span> {
</span>?>
    span>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">
            <span>function<span> createXMLHTTP()
            {
               <span>try<span>
               {
                  <span>var request = <span>new<span> XMLHttpRequest();
               }
               <span>catch<span>(e1)
               {
                   <span>var arrVersions = ["Microsoft.XMLHTTP","MSXML2.XMLHttp.4.0",
                       "MSXML2.XMLHttp.3.0","MSXML2.XMLHttp.5.0"<span>];
                   <span>for(<span>var i=0;i < arrVersions.length;i++<span>){
                       <span>try<span>{
                           request = <span>new<span> ActiveXObject(arrVersions[i]);
                       }<span>catch<span>(e2){
                           request = <span>false<span>;
                       }
                   }
               }
               <span>return<span> request;
            }

            <span>function ajax_post(url, params,<span> target_id)
            {
               request = <span>new<span> createXMLHTTP();

               request.onreadystatechange = <span>function<span>() {
                  <span>if (this.readyState == 4<span>)
                     <span>if (this.status == 200<span>)
                        <span>if (this.responseText != <span>null<span>)
                           document.getElementById(target_id).innerHTML = this.<span>responseText;
               }

               request.open("POST", url, <span>true<span>);
               request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"<span>);
               request.setRequestHeader("Content-length", params.<span>length);
               request.setRequestHeader("Connection", "close"<span>);
               request.<span>send(params);
            }
            
            <span>var checked = <span>false<span>;
            <span>function<span> check_(value) {
                checked =<span> value;
            }

            <span>function<span> get_key(event) {
                event = event || window.<span>event;
                <span>if(event.keyCode==13 && checked != <span>false<span>)
                {
                    <span>var url = document.getElementById('input_text').<span>value;
                    <span>if(url != ''<span>) {
                        get_page();
                    }<span>else<span> {
                        document.getElementById('input_text').<span>onfocus();
                        <span>return <span>false<span>;
                    }
                }
            }

            <span>function<span> get_page() {
                <span>var url = document.getElementById('input_text').<span>value;
                <span>if(!<span>url) {
                    <span>return <span>false<span>;
                }<span>else<span> {
                    <span>if(document.getElementById('output_page').innerHTML != ''<span>) {
                        document.getElementById('output_page').innerHTML = ''<span>;
                    }
                }
                <span>if(url.indexOf('http://') == -1<span>) {
                    url = 'http://'+<span>url;
                }
                ajax_post(
                    '<?php echo $_SERVER['PHP_SELF']; ?>',
                    'input_text='+url,
                    'output_page'<span>
                );
                document.getElementById('click_show').style.display = 'block'<span>;
                document.getElementById('back_a').href = document.location.<span>href;
                document.getElementById('origin_website').href =<span> url;
            }
        </script>
        <style>
            .<span>div_box{
                margin-top:<span>10px;
            }
            .<span>input_box{
                border:<span>1px solid;
                margin-left:<span>10px;
                margin-top:<span>2px;
                height:<span>15px;
                <span>float:<span>left;
                size:32<span>
                font-size:<span> 14px;
            }
            .<span>button_box{
                <span>float:<span>left;
                height:<span>23px;
                padding-bottom:<span>3px;
            }
            .<span>hide_box{
                display:<span>none;            
            }
            .<span>a_box{
                margin-left:<span>10px;
                margin-top:<span>3px;
                height:<span>15px;
                <span>float:<span>left;
                font-size:<span> 14px;
            }
            .<span>clear_box{
                height:<span>50px;
            }
        </style>
    
    
    <div>class="div_box">
        <input id="input_text">class="input_box" type="text" value="" onclick="check_(true)" onblur="check_(false)">
        <input type="button">class="button_box" onclick="get_page()" value="Get it!" >
        <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>
    
    
<span>php
    }
    

</span><span>//</span><span>End_php</span>
Salin selepas log masuk

PHP 利用AJAX获取网页并输出(原创自Zjmainstay)PHP 利用AJAX获取网页并输出(原创自Zjmainstay)jQuery 版本 get_web.php

<span>php
    </span><span>header</span>("Content-type: text/html; charset=utf-8"<span>);
    </span><span>if</span>(!<span>empty</span>(<span>$_POST</span>['input_text'<span>])) {
        </span><span>ini_set</span>('default_socket_timeout', 10<span>);
        </span><span>if</span>(!<span>$data</span> = <span>file_get_contents</span>(<span>$_POST</span>['input_text'<span>])) {
            </span><span>echo</span> "Time out!"<span>;
            </span><span>return</span><span> ;
        }
        </span><span>$charset_pos</span> = <span>stripos</span>(<span>$data</span>,'charset'<span>);
        </span><span>if</span>(<span>$charset_pos</span><span>) {
            </span><span>if</span>(<span>stripos</span>(<span>$data</span>,'utf-8',<span>$charset_pos</span><span>)) {
                </span><span>echo</span> <span>iconv</span>('utf-8','utf-8',<span>$data</span><span>);
            }</span><span>else</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'gb2312',<span>$charset_pos</span><span>)) {
                </span><span>echo</span> <span>iconv</span>('gb2312','utf-8',<span>$data</span><span>);
            }</span><span>else</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'gbk',<span>$charset_pos</span><span>)) {
                </span><span>echo</span> <span>iconv</span>('gbk','utf-8',<span>$data</span><span>);
            }
            </span><span>return</span><span>;
        }
        </span><span>echo</span> <span>$data</span><span>;
    }</span><span>else</span><span> {
</span>?>
    span>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"><span>
            $(document).ready(<span>function<span>(){
                $(document).keyup(<span>function<span>(e){
                    e = e || window.<span>event; 
                    <span>if(e.keyCode == 13 && $("#input_text").val() != ''<span>) {
                        $(".button_box").<span>click();
                    }
                });
                $(".button_box").click(<span>function<span>(){
                    <span>if($("#input_text").val() == ''<span>) {
                        $("#input_text").addClass('errorTips').<span>focus();
                        <span>return <span>false<span>;
                    }<span>else<span> {
                        $("#input_text").removeClass('errorTips'<span>);
                    }
                    $.<span>ajax({
                        url: '<?php echo $_SERVER['PHP_SELF'] ?>',<span>
                        data: 'input_text='+$("#input_text").val(),<span>
                        type:'POST',<span>
                        success:<span>function<span>(msg){
                            $(".html_tips").<span>show();
                            $("#origin_website").attr('href',$("#input_text").<span>val());
                            $("#back_a").attr('href',document.location.<span>href);
                            $("#click_show").<span>show();
                            $("#output_page_html").<span>empty().val(msg).css({height:parseInt($(document).height()-100)}).<span>show();
                            $("#output_page").<span>empty().html(msg).<span>show();
                        }
                    });
                });
            });
            
        </script>
        <style>
            .<span>div_box{
                margin-top:<span>10px;
            }
            .<span>input_box{
                border:<span>1px solid;
                margin-left:<span>10px;
                margin-top:<span>2px;
                height:<span>15px;
                <span>float:<span>left;
                size:32<span>
                font-size:<span> 14px;
            }
            .<span>button_box{
                <span>float:<span>left;
                height:<span>23px;
                padding-bottom:<span>3px;
            }
            .<span>hide_box{
                display:<span>none;            
            }
            .<span>a_box{
                margin-left:<span>10px;
                margin-top:<span>3px;
                height:<span>15px;
                <span>float:<span>left;
                font-size:<span> 14px;
            }
            .<span>clear_box{
                height:<span>50px;
            }
            .<span>error_tips{
                border:<span>1px solid red;
            }
            <span>#<span>output_page_html{
                width:<span>960px;
                margin:0<span> auto;
            }
            .<span>html_tips{
                <span>float:<span> left;
                margin: 0<span> 21px;
                font-size:1.<span>8em;
            }
        </style>
    
    
    <div>class="div_box">
        <input id="input_text">class="input_box" type="text" value="">
        <input type="button">class="button_box" value="Get it!" >
        <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">站点</div>
    <div id="output_page"></div>
    <div>class="html_tips hide_box">站点源码</div>
    <textarea id="output_page_html">class="hide_box"></textarea>    
    
    
<span>php
    }  

</span><span>//</span><span>End_php</span>
Salin selepas log masuk

 

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan