使用PHP模擬ajax請求的源碼

WBOY
發布: 2016-08-08 09:20:44
原創
1913 人瀏覽過

我已經試過了,非常好用。

<code><span>/**
 * 构造ajax请求,不支持https
 */</span><span><span>function</span><span>ajax_http_request</span><span>(<span>$url</span>, <span>$data</span> = array<span>()</span>, <span>$type</span> = <span>"post"</span>, <span>$params</span> = array<span>()</span>, <span>$accept</span> = <span>""</span>, <span>$ua</span> = <span>""</span>, <span>$referer</span> = <span>""</span>, <span>$file</span> = false)</span>
{</span><span>$http</span> = strtolower(substr(<span>$url</span>, <span>0</span>, <span>7</span>));
    <span>$type</span> = strtolower(<span>$type</span>);

    <span>$url_params</span> = parse_url(<span>$url</span>);

    <span>if</span> (!<span>$url_params</span>) {
        <span>echo</span><span>'url错误'</span>;
        <span>return</span><span>false</span>;
    }

    <span>$hostip</span> = gethostbyname(<span>$url_params</span>[<span>'host'</span>]);

    <span>if</span> (!<span>$hostip</span>) {
        <span>echo</span><span>'无法访问服务器'</span>;
        <span>return</span><span>false</span>;
    }

    <span>$fp</span> = fsockopen(<span>$url_params</span>[<span>'host'</span>], <span>80</span>, <span>$errno</span>, <span>$errstr</span>, <span>30</span>);

    <span>if</span> (!<span>$fp</span>) {
        <span>echo</span><span>"$errstr ($errno)<br />"</span>;
        <span>return</span><span>false</span>;
    }

    <span>$query_string</span> = http_build_query(<span>$data</span>);

    <span>if</span> (<span>$type</span> == <span>'post'</span>) {
        <span>$out</span> = <span>'POST '</span>.<span>$url_params</span>[<span>'path'</span>].<span>" HTTP/1.1\r\n"</span>;
    } <span>else</span> {
        <span>if</span> (strpos(<span>$url</span>, <span>'?'</span>) != <span>false</span>) {
            <span>$path</span> = <span>$url</span> .<span>'&'</span>.<span>$query_string</span>;
        } <span>else</span> {
            <span>$path</span> = <span>$url</span> . <span>'?'</span>.<span>$query_string</span>;
        }

        <span>$out</span> = <span>'GET '</span>.<span>$path</span>.<span>' HTTP/1.1'</span>.<span>"\r\n"</span>;
    }

    <span>$out</span> .= <span>'Host: '</span>.<span>$url_params</span>[<span>'host'</span>].<span>"\r\n"</span>;

    <span>$out</span> .= <span>"Connection: Close\r\n"</span>;

    <span>if</span> (<span>$type</span> == <span>'post'</span>) {
        <span>if</span> (<span>$file</span>) {
            <span>$out</span> .= (<span>"Content-Type: multipart/form-data\r\n"</span>); <span>// ajax文件上传暂时没有此功能</span>
        } <span>else</span> {
            <span>$out</span> .= (<span>"Content-Type: application/x-www-form-urlencoded\r\n"</span>);
        }
        <span>$out</span> .= (<span>"Content-Length: "</span>.strlen(<span>$query_string</span>).<span>"\r\n"</span>);
    }

    <span>if</span> (<span>isset</span>(<span>$ua</span>)) {
        <span>$out</span> .= (<span>'User-Agent: '</span>.<span>$ua</span>.<span>"\r\n"</span>);
    } <span>else</span> {
        <span>$out</span> .= (<span>"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36\r\n"</span>);
    }

    <span>if</span> (<span>isset</span>(<span>$referer</span>)) {
        <span>$out</span> .= (<span>'Referer: http://'</span>.<span>$url_params</span>[<span>'host'</span>].<span>'/'</span>.<span>$referer</span>.<span>"\r\n"</span>);
    } <span>else</span> {
        <span>$out</span> .= (<span>'Referer: http://'</span>.<span>$url_params</span>[<span>'host'</span>].<span>"\r\n"</span>);
    }

    <span>$out</span> .= (<span>"Origin: http://"</span>.<span>$url_params</span>[<span>'host'</span>].<span>"\r\n"</span>);
    <span>$out</span> .= (<span>"X-Requested-With: XMLHttpRequest\r\n"</span>);
    <span>$out</span> .= (<span>"Accept:application/json, text/javascript, */*\r\n"</span>);

    <span>$out</span> .= <span>"Accept-Language:zh-CN,zh;q=0.8,en;q=0.6\r\n\r\n"</span>;

    <span>if</span> (<span>$type</span> == <span>'post'</span>) {
        <span>// 接下来是消息体信息</span><span>$out</span> .= <span>$query_string</span>;
    }

    fwrite(<span>$fp</span>, <span>$out</span>);

    <span>$output</span> = <span>""</span>;

    <span>while</span> (!feof(<span>$fp</span>)) {
        <span>$output</span> .= fgets(<span>$fp</span>, <span>128</span>);
    }

    fclose(<span>$fp</span>);

    <span>$result</span> = explode(<span>"\r\n\r\n"</span>, <span>$output</span>);
    <span>$result</span> = explode(<span>"\r\n"</span>,<span>$result</span>[<span>1</span>]);
    array_pop(<span>$result</span>);
    array_shift(<span>$result</span>);
    <span>return</span> implode(<span>"\r\n"</span>, <span>$result</span>);
}</code>
登入後複製

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

以上就介紹了使用PHP模擬ajax請求的源代碼,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板