Several methods for php to send http requests

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.