Common methods for sending HTTP requests in PHP analysis _php tips

Source: Internet
Author: User
Tags curl http request trim

This example describes a common way for PHP to send HTTP requests. Share to everyone for your reference, specific as follows:

HTTP request has get,post.

There are three ways to send HTTP requests from PHP [I know there are three kinds, and others tell me].

1. file_get_contents (); see details:http://www.jb51.net/article/41833.htm
2. Curl Send the request.
3. Fsocket send.

The following is said to be sent using Curl .

First, the environment needs to be configured with Curl components.

It's easier to make PHP support curl in Windows:

In php.ini, remove the semicolon in front of the Extension=php_curl.dll,
It is said that the Libeay32.dll and ssleay32.dll of the PHP root directory need to be copied to the system directory. I do not copy the experiment can also.
In Linux, if you are installing with the source code, you need to/configure--with-curl=path before make.
Where 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 it can be a dynamic library.
Note that when the Libcurl library configure, you can remove some unwanted functions,
such as SSL, LDAP and so on. When PHP is configure, check to see if some of the features in the Libcurl are turned on, and then adjust the generated PHP accordingly

Two examples of using curl requests.

Initializes a CURL object
$curl = Curl_init ();
Set the URL you need to crawl
curl_setopt ($curl, Curlopt_url, ' http://www.jb51.net ');
Set header response header output
curl_setopt ($curl, Curlopt_header, 1);
Sets the curl parameter to require that the results be saved to the string or to the screen.
//1 If Success returns only results, no content is automatically exported. If the failure returns false
curl_setopt ($curl, Curlopt_returntransfer, 0);
Run Curl, request Web page
$data = curl_exec ($curl);
Closes the URL request
curl_close ($curl);
Displays the obtained data
Print_r ($data);

Another example of a 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);
Whether the setting is
curl_setopt ($ch, curlopt_post,1) by POST or get method;
Transfer of 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);
}

Just write so much first.

Fsocket:

$gurl = "HTTP://MYTEST/LAB/T.PHP?UU=GGGGGG";
Print_r (Parse_url ($gurl));
echo "Below is the response content:<br> of Get mode";
Sock_get ($gurl);
 function Sock_get ($url) {$info = Parse_url ($url);
 $fp = Fsockopen ($info ["host"], $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 analog Post Submit $purl = "http://mytest/lab/t.php";
echo "Below is the response content of the Post method:<br>";
Sock_post ($purl, "uu=rrrrrrrrrrrr&&kk=mmmmmm");
 function Sock_post ($url, $query) {$info = Parse_url ($url);
 $fp = Fsockopen ($info ["host"], $errno, $ERRSTR, 3); $head = "POST". $info [' Path ']. "
 Http/1.0\r\n "; $head. = "Host:". $info [' Host ']. "
 \ r \ n "; $head. = "referer:http://". $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> ";

 }
}

Curl Add gzip parameters to refer to:

Example analysis of gzip compression performance test in PHP Curl: http://www.jb51.net/article/96778.htm

For more information about PHP interested readers can view the site topics: "Php Curl Usage Summary", "PHP Socket Usage Summary", "PHP Network Programming Skills Summary", "PHP object-oriented Programming Program Introduction", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

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.