PHP uses socket to send GET and POST requests, socketget_PHP tutorial

WBOY
Release: 2016-07-13 09:45:39
Original
895 people have browsed it

php uses socket to send GET, POST requests, socketget

As a PHP programmer, you will definitely come into contact with the http protocol, and only with a deep understanding of the http protocol can your programming level be improved. Recently, I have been learning about HTTP programming in PHP. Many things suddenly became clear to me and I benefited a lot. Hope to share it with everyone. This article needs to be read by developers with a certain http foundation.

Today I will show you how to use socket to send GET and POST requests. I will use an Http class encapsulated by Teacher Yan Shiba to illustrate.

In daily programming, I believe that many people, like me, use the browser to make GET and POST requests to the server most of the time. So, can I use other methods to make GET and POST requests? The answer must be yes. Anyone who knows the HTTP protocol knows that the essence of the browser submitting a request is to send a request information to the server. This request information consists of a request line, a request header, and a request body (optional). The server returns a response information based on the request information. The connection is lost.

The format of the HTTP request is as follows:

<span>1</span> <span><</span><span>request-line</span><span>></span>
<span>2</span> <span><</span><span>headers</span><span>></span>
<span>3</span> <span><</span><span>blank </span><span>line</span><span>></span>
<span>4</span> [<span><</span><span>request-body</span><span>></span>]
Copy after login

The format of the HTTP response is very similar to the format of the request:

<span><</span><span>status-line</span><span>></span>
<span><</span><span>headers</span><span>></span>
<span><</span><span>blank </span><span>line</span><span>></span><span>

[</span><span><</span><span>response-body</span><span>></span>]
Copy after login

We can use the principle of HTTP to send requests, and we can reconsider using sockets to send HTTP requests.

The original English meaning of Socket is "hole" or "socket". Also commonly called a "socket", it is used to describe an IP address and port. It is a handle to a communication chain and can be used to implement communication between different virtual machines or different computers. Hosts on the Internet generally run multiple service software and provide several services at the same time. Each service opens a Socket and is bound to a port. Different ports correspond to different services. From this point of view, it is actually as easy to use sockets to operate remote files as to read and write local files. Think of local files as being transmitted through hardware, and remote files as long as they are transmitted through network cables.

Therefore, sending a request can be considered as Establishing a connection->Opening the socket interface (fsockopen())->Writing request (fwrite())->Reading response (fread()-> ;Close the file (fclose()) Without further ado, let’s go straight to the code:

 

<?<span>php 

</span><span>interface</span><span> Proto {
    </span><span>//</span><span> 连接url</span>
    <span>function</span> conn(<span>$url</span><span>);

    </span><span>//</span><span>发送get查询</span>
    <span>function</span><span> get();

    </span><span>//</span><span> 发送post查询</span>
    <span>function</span><span> post();

    </span><span>//</span><span> 关闭连接</span>
    <span>function</span><span> close();
}



</span><span>class</span> Http <span>implements</span><span> Proto {

    </span><span>const</span> CRLF  = "\r\n"<span>;

    </span><span>protected</span> <span>$errno</span> = -1<span>;
    </span><span>protected</span> <span>$errstr</span> = ''<span>;
    </span><span>protected</span> <span>$response</span> = ''<span>;

    </span><span>protected</span> <span>$url</span> = <span>null</span><span>;
    </span><span>protected</span> <span>$version</span> = 'HTTP/1.1'<span>;
    </span><span>protected</span> <span>$fh</span> = <span>null</span><span>;
    
    </span><span>protected</span> <span>$line</span> = <span>array</span><span>();
    </span><span>protected</span> <span>$header</span> = <span>array</span><span>();
    </span><span>protected</span> <span>$body</span> = <span>array</span><span>();

    
    </span><span>public</span> <span>function</span> __construct(<span>$url</span><span>) {
        </span><span>$this</span>->conn(<span>$url</span><span>);
        </span><span>$this</span>->setHeader('Host: ' . <span>$this</span>->url['host'<span>]);
    }

    </span><span>//</span><span> 此方法负责写请求行</span>
    <span>protected</span> <span>function</span> setLine(<span>$method</span><span>) {
        </span><span>$this</span>->line[0] = <span>$method</span> . ' ' . <span>$this</span>->url['path'] . '?' .<span>$this</span>->url['query'] . ' '. <span>$this</span>-><span>version;
    }

    </span><span>//</span><span> 此方法负责写头信息</span>
    <span>public</span> <span>function</span> setHeader(<span>$headerline</span><span>) {
        </span><span>$this</span>-><span>header</span>[] = <span>$headerline</span><span>; 
    }

    </span><span>//</span><span> 此方法负责写主体信息</span>
    <span>protected</span> <span>function</span> setBody(<span>$body</span><span>) {
         </span><span>$this</span>->body[] = <span>http_build_query</span>(<span>$body</span><span>);
    }


    </span><span>//</span><span> 连接url</span>
    <span>public</span> <span>function</span> conn(<span>$url</span><span>) {
        </span><span>$this</span>->url = <span>parse_url</span>(<span>$url</span><span>);
        </span><span>//</span><span> 判断端口</span>
        <span>if</span>(!<span>isset</span>(<span>$this</span>->url['port'<span>])) {
            </span><span>$this</span>->url['port'] = 80<span>;
        }

        </span><span>//</span><span> 判断query</span>
        <span>if</span>(!<span>isset</span>(<span>$this</span>->url['query'<span>])) {
            </span><span>$this</span>->url['query'] = ''<span>;
        }

        </span><span>$this</span>->fh = <span>fsockopen</span>(<span>$this</span>->url['host'],<span>$this</span>->url['port'],<span>$this</span>->errno,<span>$this</span>->errstr,3<span>);
    }

    </span><span>//</span><span>构造get请求的数据</span>
    <span>public</span> <span>function</span><span> get() {
        </span><span>$this</span>->setLine('GET'<span>);
        </span><span>$this</span>-><span>request();
        </span><span>return</span> <span>$this</span>-><span>response;
    }

    </span><span>//</span><span> 构造post查询的数据</span>
    <span>public</span> <span>function</span> post(<span>$body</span> = <span>array</span><span>()) {      
        </span><span>$this</span>->setLine('POST'<span>);

        </span><span>//</span><span> 设计content-type</span>
        <span>$this</span>->setHeader('Content-type: application/x-www-form-urlencoded'<span>);
        
        </span><span>//</span><span> 设计主体信息,比GET不一样的地方</span>
        <span>$this</span>->setBody(<span>$body</span><span>);


        </span><span>//</span><span> 计算content-length</span>
        <span>$this</span>->setHeader('Content-length: ' . <span>strlen</span>(<span>$this</span>->body[0<span>]));

        </span><span>$this</span>-><span>request();

        </span><span>return</span> <span>$this</span>-><span>response;
    }

    </span><span>//</span><span> 真正请求</span>
    <span>public</span> <span>function</span><span> request() {
        </span><span>//</span><span> 把请求行,头信息,实体信息 放在一个数组里,便于拼接</span>
        <span>$req</span> = <span>array_merge</span>(<span>$this</span>->line,<span>$this</span>-><span>header</span>,<span>array</span>(''),<span>$this</span>->body,<span>array</span>(''<span>));
        </span><span>//</span><span>print_r($req);</span>

        <span>$req</span> = <span>implode</span>(self::CRLF,<span>$req</span><span>); 
        </span><span>//</span><span>echo $req; exit;</span>

        <span>fwrite</span>(<span>$this</span>->fh,<span>$req</span><span>);
        
        </span><span>while</span>(!<span>feof</span>(<span>$this</span>-><span>fh)) {
            </span><span>$this</span>->response .= <span>fread</span>(<span>$this</span>->fh,1024<span>);
        }

        </span><span>$this</span>->close(); <span>//</span><span> 关闭连接</span>
<span>    }


    </span><span>//</span><span> 关闭连接</span>
    <span>public</span> <span>function</span><span> close() {
        </span><span>fclose</span>(<span>$this</span>-><span>fh);
    }

}</span>
Copy after login

Use this class to send a simple GET request:

<?<span>php<br /><br />//记得引用Http类
</span><span>$url</span>="http://home.cnblogs.com/u/DeanChopper/"<span>;
</span><span>$http</span>=<span>new</span> Http(<span>$url</span><span>);


</span><span>$response</span>=<span>$http</span>-><span>get();

</span><span>print_r</span>(<span>$response</span>);
Copy after login

The return value is information. You can further process the response information to get the content you want.

 

 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1040167.htmlTechArticlephp uses socket to send GET, POST requests, socketget. As a php programmer, you will definitely be exposed to the http protocol, and you can only have an in-depth understanding of it. http protocol, programming level will be further improved. Recently I have been...
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