PHP uses socket to send HTTP requests. PHP socket to send requests _ PHP Tutorial

Source: Internet
Author: User
PHP uses socket to send HTTP requests, and phpsocket to send requests. PHP uses socket to send HTTP requests. phpsocket to send requests this article describes how PHP uses socket to send HTTP requests. For your reference, the details are as follows: so PHP uses socket to send HTTP requests, and phpsocket to send requests

This example describes how PHP uses socket to send HTTP requests. We will share this with you for your reference. The details are as follows:

Socket mode:

$ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // socket_set_option ($ socket, SOL_SOCKET, SO_SNDTIMEO, array ("sec" => 20, "usec" => 0); socket_connect ($ socket, 'www .baidu.com ', 80 ); // The line feed indicates \ r \ n. Note that there may be spaces behind the copied code $ http = <
 
  

Fsockopen mode:

$fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30);if (!$fp) {  echo "$errstr ($errno)
\n";} else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: www.baidu.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $http); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp);}

Original socket method:

$ Fp = stream_socket_client ("tcp: // www.baidu.com: 80", $ errno, $ errstr, 30); if (! $ Fp) {echo "$ errstr ($ errno)
\ N ";}else {$ http = <

Stream method (get ):

$ Http = <
     
      
Array ('header' => $ http, 'timeout' => 1, // timeout second 'method' => 'get ', // the default method is 'protocol _ version' => '1. 1', // The default value is 1.0),); // for the parameter format, refer to parameter layout = stream_context_create ($ hdrs); echo file_get_contents ('http: // www.baidu.com ', 0, $ context );
     

Stream post:

$ Postdata = http_build_query (array ('Act '=> 'Save', 'id' => 387171); $ http = <
     
      
Array ('header' => $ http, 'timeout' => 1, // timeout second 'method' => 'post', 'content' => $ postdata, 'protocol _ version' => '1. 1', // The default value is 1.0),); // for the parameter format, see paihttp://php.net/manual/zh/context.curl.php?context = stream_context_create ($ hdrs); echo file_get_contents ('http: // test. cm/song. php ', 0, $ context );
     

Note: http1.1 must contain the Host header, but http1.0 may not

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.