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)
- <?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
- ?>
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