Php sends http requests in many ways. we still need to use php to send http requests. it can simulate browser behavior. generally, its application scenarios include: 1. the backend tests its own interface. 2. the backend requests others' data.
The backend tests its own interface. for example, if we write an interface to return json data, we can let the frontend test, but the frontend is not necessarily empty, or the front-end interface has not been made, since the interface is not necessarily ready-made, it may be a bit difficult to test. Of course, we can use Google's postman or Firefox's poster to send post and delete requests, but they are not so nice to apply. Therefore, we can use php programming to write test functions.
This is different from manually clicking a browser to request other people's data at the backend. It provides more powerful functions than clicking a browser and can automatically analyze some data, therefore, the function is more powerful.
The first implementation method: practical socket programming. generally, we use the fsockopen function to create a socket connection and use fputs to send a request. As for how to use specific functions, google or Baidu.
Method 2: use php curl extension. We use curl_init () to initialize a connection, and then set a bunch of curl_setopt () to set url and post data, finally, we use curl_exec () to implement the request.
The third method is: the practical file_get_contents function. In fact, we usually crawl a webpage and only use its first parameter. In fact, its third parameter has data, the following is an official manual for the portal: official documents.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.