Common Methods for php to send http requests, php to send http requests

Source: Internet
Author: User

Common Methods for php to send http requests, php to send http requests

This document describes common methods for php to send http requests. We will share this with you for your reference. The details are as follows:

Http requests include get and post.

Php sends http requests in three ways: [I know three methods, but I can tell others].

1. file_get_contents (); For details, see: http://www.bkjia.com/article/41833.htm
2. curl sends a request.
3. fsocket transmission.

NextUse curl to send.

First, configure the curl component in the environment.

In windows, it is relatively simple to allow php to support curl:

Remove the semicolon before extension = php_curl.dll in php. ini,
Some people say they need to copy libeay32.dll and ssleay32.dll from the php root directory to the system directory. I can copy the experiment without copying it.
In linux, if you use the source code for installation, You Need To before make,./configure -- with-curl = path,
Path is the location of your libcurl library. For example, after you install the libcurl library,
Path may be/usr/local/. libcurl can be a static library or a dynamic library.
Note that some unwanted functions can be removed when the libcurl library configure is used,
For example, ssl and ldap. When php configure is enabled, it will check whether some functions in libcurl are enabled, and then adjust the generated php

Two examples of sending requests using curl.

// Initialize a cURL object $ curl = curl_init (); // set the URLcurl_setopt ($ curl, CURLOPT_URL, 'HTTP: // www.bkjia.com ') to be crawled '); // set whether the header Response header outputs curl_setopt ($ curl, CURLOPT_HEADER, 1); // set the cURL parameter to ensure that the result is saved to the string or output to the screen. // 1 if the result is returned successfully, NO content is automatically output. If the request fails, FALSEcurl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 0) is returned. // run cURL to request the webpage $ data = curl_exec ($ curl); // close the URL request curl_close ($ curl ); // display the obtained data print_r ($ data );

Another example of the post method:

// Post method $ phoneNumber = "13912345678"; $ message = "testMessage"; $ curlPost = "phone = ". urlencode ($ phoneNumber ). "& message = ". $ message; $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, 'HTTP: // mytest/lab/t. php '); curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0); // set whether to use the post or get method curl_setopt ($ ch, CURLOPT_POST, 1); // The passed variable curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost); $ data = curl_exec ($ ch); curl_close ($ ch );

In this http: // mytest/lab/t. php file:

if(!empty($_POST)){ print_r($_POST);}

Write so much first.

Fsocket:

$ Gurl = "http: // mytest/lab/t. php? Uu = gggggg "; // print_r (parse_url ($ gurl); echo" response content in GET mode: <br> "; sock_get ($ gurl ); function sock_get ($ url) {$ info = parse_url ($ url); $ fp = fsockopen ($ info ["host"], 80, $ errno, $ errstr, 3 ); $ head = "GET ". $ info ['path']. "? ". $ Info ["query"]. "HTTP/1.0 \ r \ n"; $ head. = "Host :". $ info ['host']. "\ r \ n"; $ head. = "\ r \ n"; $ write = fputs ($ fp, $ head); while (! Feof ($ fp) {$ line = fgets ($ fp); echo $ line. "<br>" ;}}// fsocket simulate post submission $ purl = "http: // mytest/lab/t. php "; echo" The following is the POST Response content: <br> "; sock_post ($ purl," uu = rrrrrrrrrrrr & kk = mmmmmm "); function sock_post ($ url, $ query) {$ info = parse_url ($ url); $ fp = fsockopen ($ info ["host"], 80, $ errno, $ errstr, 3); $ head = "POST ". $ info ['path']. "HTTP/1.0 \ r \ n"; $ head. = "Host :". $ info ['host']. "\ r \ n"; $ head. = "Referer: htt P ://". $ info ['host']. $ info ['path']. "\ r \ n"; $ head. = "Content-type: application/x-www-form-urlencoded \ r \ n"; $ head. = "Content-Length :". strlen (trim ($ query )). "\ r \ n"; $ head. = "\ r \ n"; $ head. = trim ($ query); $ write = fputs ($ fp, $ head); print_r (fgets ($ fp); while (! Feof ($ fp) {$ line = fgets ($ fp); echo $ line. "<br> ";}}

For details about how to add gzip parameters to curl, refer:

Analysis of gzip compression performance test instance in php curl: http://www.bkjia.com/article/96778.htm

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.