Quellcode zur Simulation von Ajax-Anfragen mit PHP

WBOY
Freigeben: 2016-08-08 09:20:44
Original
1913 Leute haben es durchsucht

Ich habe es ausprobiert und es funktioniert sehr gut.

<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>
Nach dem Login kopieren

Urheberrechtserklärung: Dieser Artikel ist ein Originalartikel des Bloggers und darf nicht ohne die Erlaubnis des Bloggers reproduziert werden.

Das Obige stellt den Quellcode für die Verwendung von PHP zur Simulation von Ajax-Anfragen vor, einschließlich der relevanten Inhalte. Ich hoffe, dass es für Freunde hilfreich ist, die sich für PHP-Tutorials interessieren.

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
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
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage