Home > Backend Development > PHP Tutorial > Source code for simulating ajax requests using PHP

Source code for simulating ajax requests using PHP

WBOY
Release: 2016-08-08 09:20:44
Original
1948 people have browsed it

I have tried it and it works very well.

<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>
Copy after login

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the source code of using PHP to simulate ajax requests, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template