Several ways to send HTTP requests from PHP

Source: Internet
Author: User
Tags php programming

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

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.