A lot of times, we still need to use PHP to send HTTP requests, it can simulate the behavior of the browser, usually its application scenario is: 1. The backend tests its own interface. 2. Back-end request data from others.
Back-end testing their own interface, such as we wrote a return JSON data interface, we can let the front-end to test, but the front-end is not necessarily empty ah, or the front-end interface has not been done, because the interface is not necessarily ready, so the test will be a bit cumbersome. Of course we can use Google Chrome postman or Firefox poster, which can send post, delete requests and so on, but they are not so cool to apply. As a result, we can write test functions in a way that is useful for PHP programming.
For back-end requests for other people's data, this is different from our manual click on the browser, it can be more powerful than we click on the browser, and can automatically analyze some of the data, so the function is more powerful.
The first realization way: Practical socket programming, usually we use fsockopen this function to create a socket connection, with fputs to send a request, as to how the specific function, please google or Baidu it.
The second implementation: PHP's Curl extension, we use Curl_init () to initialize a connection, then set a bunch of curl_setopt () to set url,post data, and so on, and finally we use CURL_EXEC () to implement the request.
The Third Way is: practical file_get_contents function, in fact, we usually crawl a page may only be useful for its first parameter, in fact, its third parameter has data, the following is attached to its official manual, Portal: Official documents.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Several ways to send HTTP requests from PHP