Share a powerful collection class and also simulate PHP multi-process

Source: Internet
Author: User

When doing the collection, you can use file_get_contents () to get the Web source code, but use file_get_contents acquisition, slow, and time-out, bad control. If the collected page does not exist, it will take a long time to wait. Generally, curl is the fastest, followed by the socket, and finally the file_get_contents.
now to share with you a very powerful collection class, will be based on your server's current configuration, automatically choose the fastest way. has already encapsulated curl and socket,file_get_contents.

The usage is simple:
1, request with Get method
Http::d oget (URL);//supermarket time can be ignored, default is 5 seconds
Http::d oget (URL, time-out);
such as Echo Http::d oget (' http://www.baidu.com ');

2, using the Post method request
Http::d opost (URL, data, time-out);

Such as
$url = ' http://www.canphp.com/test.php ';
$data [' name ']= ' riding alone ';
$data [' Email ']= ' [email protected] ';
Http::d opost ($url, $data, 10);

test.php page Receive data
$_post[' name '];
$_post[' email '];

this HTTP class can be used not only for collection, but also for a powerful role in simulating PHP asynchronous multi-process.
Like index.php and a.php, b.php, c.php.
In the index.php
Http::d oget (' http://www.canphp.com/a.php ', 1);
Http::d oget (' http://www.canphp.com/b.php ', 1);
Http::d oget (' http://www.canphp.com/c.php ', 1);

a.php, b.php, c.php program respectively in the head PlusIgnore_user_abort (true);
Then you can achieve a multi-process.

Principle:
Send a request via curl or socket to a.php, b.php, c.php, because the timeout time is relatively short, just triggered a.php, b.php, c.php three pages, do not need to wait for data return, connection is interrupted, but a.php, b.php, c.php program Added aIgnore_user_abort (true);Ignores the client connection and continues execution.

Specific cases can watch very evil very powerful AV program (http://www.canphp.com/bbs/thread-295-1-1.html)
  1. <?php
  2. Data acquisition, Doget,dopost
  3. Class Http
  4. {//class definition Start
  5. Get data by Get method
  6. static public Function doget ($url, $timeout =5)
  7. {
  8. $code =self::getsupport ();
  9. Switch ($code)
  10. {
  11. Case 1:return Self::curl ($url, ", $timeout);
  12. Case 2:return Self::socketget ($url, $timeout);
  13. Case 3:return @file_get_contents ($url);
  14. Default:return false;
  15. }
  16. }
  17. Send data via post
  18. static public Function DoPost ($url, $data, $timeout =5)
  19. {
  20. $code =self::getsupport ();
  21. Switch ($code)
  22. {
  23. Case 1:return Self::curl ($url, $data, $timeout);
  24. Case 2:return Self::socketpost ($url, $data, $timeout);
  25. Default:return false;
  26. }
  27. }
  28. Get support for how to read remote files
  29. static public Function Getsupport ()
  30. {
  31. if (function_exists (' Curl_init '))//curl mode
  32. {
  33. return 1;
  34. }
  35. else if (function_exists (' Fsockopen '))//socket
  36. {
  37. return 2;
  38. }
  39. else if (function_exists (' file_get_contents '))//php system functions file_get_contents
  40. {
  41. return 3;
  42. }
  43. else if (ini_get (' Allow_url_fopen ') &&function_exists (' fopen ')//php system functions fopen
  44. {
  45. return 4;
  46. }
  47. Else
  48. {
  49. return 0;
  50. }
  51. }
  52. static public Function gethttpcontent ($fsock =null) {
  53. $out = null;
  54. while ($buff = @fgets ($fsock, 2048)) {
  55. $out. = $buff;
  56. }
  57. Fclose ($fsock);
  58. $pos = Strpos ($out, "\r\n\r\n");
  59. $head = substr ($out, 0, $pos); HTTP Head
  60. $status = substr ($head, 0, Strpos ($head, "\ r \ n")); HTTP status Line
  61. $body = substr ($out, $pos + 4, strlen ($out)-($pos + 4));//page body
  62. if (Preg_match ("/^http\/\d\.\d\s ([\d]+) \s.*$/", $status, $matches)) {
  63. if (Intval ($matches [1])/100 = = 2) {
  64. return $body;
  65. }else{
  66. return false;
  67. }
  68. }else{
  69. return false;
  70. }
  71. }
  72. static public Function Socketget ($url, $timeout =5) {
  73. $url 2 = Parse_url ($url);
  74. $url 2["path"] = Isset ($url 2["path"])? $url 2["path"]: "/";
  75. $url 2["port"] = Isset ($url 2["Port"])? $url 2["Port"]: 80;
  76. $url 2["Query"] = Isset ($url 2["Query"])? "?". $url 2["Query"]: "";
  77. $host _ip = @gethostbyname ($url 2["host"]);
  78. $fsock _timeout = $timeout; Timeout period
  79. if ($fsock = Fsockopen ($host _ip, $url 2[' Port '], $errno, $errstr, $fsock _timeout)) < 0) {
  80. return false;
  81. }
  82. $request = $url 2["path"]. $url 2["Query"];
  83. $in = "GET". $request. "Http/1.1\r\n";
  84. $in. = "Accept: */*\r\n";
  85. $in. = "user-agent:payb-agent\r\n";
  86. $in. = "Host:". $url 2["Host"]. "\ r \ n";
  87. $in. = "connection:close\r\n\r\n";
  88. if ([email protected] ($fsock, $in, strlen ($in))) {
  89. @fclose ($fsock);
  90. return false;
  91. }
  92. Return self::gethttpcontent ($fsock);
  93. }
  94. static public Function Socketpost ($url, $post _data=array (), $timeout =5) {
  95. $url 2 = Parse_url ($url);
  96. $url 2["path"] = ($url 2["path"] = = ""? " /": $url 2[" path "]);
  97. $url 2["port"] = ($url 2["port"] = = ""? : $url 2["Port"]);
  98. $host _ip = @gethostbyname ($url 2["host"]);
  99. $fsock _timeout = $timeout; Timeout period
  100. if ($fsock = Fsockopen ($host _ip, $url 2[' Port '], $errno, $errstr, $fsock _timeout)) < 0) {
  101. return false;
  102. }
  103. $request = $url 2["path"]. ($url 2["Query"]? "?" . $url 2["Query"]: "");
  104. $post _data2 = http_build_query ($post _data);
  105. $in = "POST". $request. "Http/1.1\r\n";
  106. $in. = "Accept: */*\r\n";
  107. $in. = "Host:". $url 2["Host"]. "\ r \ n";
  108. $in. = "user-agent:lowell-agent\r\n";
  109. $in. = "content-type:application/x-www-form-urlencoded\r\n";
  110. $in. = "Content-length:". strlen ($post _data2). "\ r \ n";
  111. $in. = "connection:close\r\n\r\n";
  112. $in. = $post _data2. "\r\n\r\n";
  113. unset ($post _data2);
  114. if ([email protected] ($fsock, $in, strlen ($in))) {
  115. @fclose ($fsock);
  116. return false;
  117. }
  118. Return self::gethttpcontent ($fsock);
  119. }
  120. static public Function curl ($url, $data =array (), $timeout =5)
  121. {
  122. $ch = Curl_init ();
  123. if (Is_array ($data) && $data)
  124. {
  125. $formdata = Http_build_query ($data);
  126. curl_setopt ($ch, Curlopt_post, true);
  127. curl_setopt ($ch, Curlopt_postfields, $formdata);
  128. }
  129. curl_setopt ($ch, Curlopt_url, $url);
  130. curl_setopt ($ch, Curlopt_returntransfer, true);
  131. curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
  132. curl_setopt ($ch, Curlopt_timeout, $timeout);
  133. $result = curl_exec ($ch);
  134. Curl_close ($ch);
  135. return $result;
  136. }
  137. }//class definition End
  138. ?>
Copy Code

<?php
Data acquisition, Doget,dopost
Class Http
{//class definition Start
Get data by Get method
static public Function doget ($url, $timeout =5)
{
$code =self::getsupport ();
Switch ($code)
{
Case 1:return Self::curl ($url, ", $timeout);
Case 2:return Self::socketget ($url, $timeout);
Case 3:return @file_get_contents ($url);
Default:return false;
}
}
Send data via post
static public Function DoPost ($url, $data, $timeout =5)
{
$code =self::getsupport ();
Switch ($code)
{
Case 1:return Self::curl ($url, $data, $timeout);
Case 2:return Self::socketpost ($url, $data, $timeout);
Default:return false;
}
}

Get support for how to read remote files
static public Function Getsupport ()
{
if (function_exists (' Curl_init '))//curl mode
{
return 1;
}
else if (function_exists (' Fsockopen '))//socket
{
return 2;
}
else if (function_exists (' file_get_contents '))//php system functions file_get_contents
{
return 3;
}
else if (ini_get (' Allow_url_fopen ') &&function_exists (' fopen ')//php system functions fopen
{
return 4;
}
Else
{
return 0;
}
}
static public Function gethttpcontent ($fsock =null) {
$out = null;
while ($buff = @fgets ($fsock, 2048)) {
$out. = $buff;
}
Fclose ($fsock);
$pos = Strpos ($out, "\r\n\r\n");
$head = substr ($out, 0, $pos); HTTP Head
$status = substr ($head, 0, Strpos ($head, "\ r \ n")); HTTP status Line
$body = substr ($out, $pos + 4, strlen ($out)-($pos + 4));//page body
if (Preg_match ("/^http\/\d\.\d\s ([\d]+) \s.*$/", $status, $matches)) {
if (Intval ($matches [1])/100 = = 2) {
return $body;
}else{
return false;
}
}else{
return false;
}
}
static public Function Socketget ($url, $timeout =5) {
$url 2 = Parse_url ($url);
$url 2["path"] = Isset ($url 2["path"])? $url 2["path"]: "/";
$url 2["port"] = Isset ($url 2["Port"])? $url 2["Port"]: 80;
$url 2["Query"] = Isset ($url 2["Query"])? "?". $url 2["Query"]: "";
$host _ip = @gethostbyname ($url 2["host"]);
$fsock _timeout = $timeout; Timeout period
if ($fsock = Fsockopen ($host _ip, $url 2[' Port '], $errno, $errstr, $fsock _timeout)) < 0) {
return false;
}
$request = $url 2["path"]. $url 2["Query"];
$in = "GET". $request. "Http/1.1\r\n";
$in. = "Accept: */*\r\n";
$in. = "user-agent:payb-agent\r\n";
$in. = "Host:". $url 2["Host"]. "\ r \ n";
$in. = "connection:close\r\n\r\n";
If[email protected]($fsock, $in, strlen ($in))) {
@fclose ($fsock);
return false;
}
Return self::gethttpcontent ($fsock);
}

static public Function Socketpost ($url, $post _data=array (), $timeout =5) {
$url 2 = Parse_url ($url);
$url 2["path"] = ($url 2["path"] = = ""? " /": $url 2[" path "]);
$url 2["port"] = ($url 2["port"] = = ""? : $url 2["Port"]);
$host _ip = @gethostbyname ($url 2["host"]);
$fsock _timeout = $timeout; Timeout period
if ($fsock = Fsockopen ($host _ip, $url 2[' Port '], $errno, $errstr, $fsock _timeout)) < 0) {
return false;
}
$request = $url 2["path"]. ($url 2["Query"]? "?" . $url 2["Query"]: "");
$post _data2 = http_build_query ($post _data);
$in = "POST". $request. "Http/1.1\r\n";
$in. = "Accept: */*\r\n";
$in. = "Host:". $url 2["Host"]. "\ r \ n";
$in. = "user-agent:lowell-agent\r\n";
$in. = "content-type:application/x-www-form-urlencoded\r\n";
$in. = "Content-length:". strlen ($post _data2). "\ r \ n";
$in. = "connection:close\r\n\r\n";
$in. = $post _data2. "\r\n\r\n";
unset ($post _data2);
If[email protected]($fsock, $in, strlen ($in))) {
@fclose ($fsock);
return false;
}
Return self::gethttpcontent ($fsock);
}

static public Function curl ($url, $data =array (), $timeout =5)
{
$ch = Curl_init ();
if (Is_array ($data) && $data)
{
$formdata = Http_build_query ($data);
curl_setopt ($ch, Curlopt_post, true);
curl_setopt ($ch, Curlopt_postfields, $formdata);
}
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, true);
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
curl_setopt ($ch, Curlopt_timeout, $timeout);
$result = curl_exec ($ch);
Curl_close ($ch);
return $result;
}

}//class definition End
?>

Share a powerful collection class, but also can simulate PHP multi-process

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.