当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP中Libevent HTTP客户端实现程序

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-15 14:17:51 浏览: 评论:0 

下面来给各位介绍一段用php实现的Libevent HTTP客户端实现程序,有需要了解的朋友可与小编一起来学习一下.

php Libevent HTTP,代码如下:

  1. <?php 
  2. //请求完成回调 
  3. function _request_handler($req, $base) { 
  4.   global $pend_req; 
  5.   //echo __FUNCTION__, PHP_EOL; 
  6.  
  7.   if (is_null($req)) { 
  8.     //echo "Timed out\n"; 
  9.   } else { 
  10.     $response_code = $req->getResponseCode(); 
  11.  
  12.     if ($response_code == 0) { 
  13.       //echo "Connection refused\n"; 
  14.     } elseif ($response_code != 200) { 
  15.       //echo "Unexpected response: $response_code\n"; 
  16.     } else { 
  17.       //echo "Success: $response_code\n"; 
  18.       /* 
  19.       $buf = $req->getInputBuffer(); 
  20.       echo "Body:\n"; 
  21.       while ($s = $buf->readLine(EventBuffer::EOL_ANY)) { 
  22.       echo $s, PHP_EOL; 
  23.       } 
  24.        */ 
  25.     } 
  26.   } 
  27.   $pend_req--; 
  28.   //退出循环 
  29.   if (!$pend_req) { 
  30.     $base = $conn->getBase(); 
  31.     $base->exit(NULL); 
  32.   } 
  33.   //释放内存 
  34.   unset($req); 
  35.   unset($conn); 
  36. } 
  37.  
  38. //$address = "www.phpfensi.com"; 
  39. $pend_req = 0; 
  40. $port = 80; 
  41. //初始化event base 
  42. $base = new EventBase(); 
  43. echo "Event method used: ", $base->getMethod(), PHP_EOL; 
  44.  //使用异步DNS 
  45. $dns_base = new EventDnsBase($base, TRUE); 
  46. $f= fopen("./50000.txt","r"); 
  47. while (!feof($f)) 
  48. { 
  49.   $line = fgets($f); 
  50.   //echo $address; 
  51.   $address = trim($line); 
  52.   //新建http连接事件到base 
  53.   $conn = new EventHttpConnection($base, $dns_base, $address, $port); 
  54.   $conn->setTimeout(1); 
  55.   //设置请求回调 
  56.   $req = new EventHttpRequest("_request_handler", $conn); 
  57.   //开源软件:phpfensi.com 
  58.   $req->addHeader("Host", $address, EventHttpRequest::OUTPUT_HEADER); 
  59.   $req->addHeader("Content-Length", "0", EventHttpRequest::OUTPUT_HEADER); 
  60.   $conn->makeRequest($req, EventHttpRequest::CMD_GET, "/"); 
  61.   $pend_req++; 
  62. } 
  63. fclose($f); 
  64. //事件主循环 
  65. $base->loop(); 
  66. ?> 

c语言版,代码如下:

  1. #include <stdio.h> 
  2. #include <string.h> 
  3. #include <ctype.h> 
  4. #include <stdlib.h> 
  5. #include <signal.h> 
  6. #include <unistd.h> 
  7. #include <evhttp.h> 
  8. #include <event2/event.h> 
  9. #include <event2/http.h> 
  10. #include <event2/bufferevent.h> 
  11. typedef struct my_struct_s my_struct_t; 
  12.  
  13. struct my_struct_s { 
  14.   struct evhttp_connection *conn; 
  15.   struct evhttp_request *req; 
  16.   struct evhttp_uri *uri; 
  17.   struct event *cleanup; 
  18. }; 
  19.  
  20. struct event_base *Base_Primary; 
  21.  
  22. char *trimwhitespace(char *str) 
  23. { 
  24.   char *end; 
  25.  
  26.   // Trim leading space 
  27.   while(isspace(*str)) str++; 
  28.  
  29.   if(*str == 0)  // All spaces? 
  30.     return str; 
  31.  
  32.   // Trim trailing space 
  33.   end = str + strlen(str) - 1; 
  34.   while(end > str && isspace(*end)) end--; 
  35.  
  36.   // Write new null terminator 
  37.   *(end+1) = 0; 
  38.  
  39.   return str; 
  40. } 
  41.  
  42. void connection_free(int sock, short which, void *arg) { 
  43.   //printf("freeing connection!!! The socket's FD would have been closed when the HTTP request ended and the ->req object would have been free'd\n"); 
  44.  
  45.   // Get our structure object 
  46.   my_struct_t *myStruct = arg; 
  47.  
  48.   // Cleanup our properties 
  49.   event_free(myStruct->cleanup); 
  50.   evhttp_connection_free(myStruct->conn); 
  51.   evhttp_request_free(myStruct->req); 
  52.   evhttp_uri_free(myStruct->uri); 
  53.  
  54.   // Free our custom structure 
  55.   free(myStruct); 
  56. } 
  57.  
  58. void http_request_done(struct evhttp_request *req, void *arg){ 
  59.  
  60.   // Get our custom struct 
  61.   my_struct_t *myStruct = arg; 
  62.  
  63.   // Setup our timeout information (we delay 5 seconds) 
  64.   struct timeval Timeout; 
  65.   Timeout.tv_sec = 0; 
  66.   Timeout.tv_usec = 0; 
  67.  
  68.   // Add this structure to our cleanup base to be cleaned up synchronously 
  69.   // TODO: Probably not the best way to cleanup and event, but it'l work for the purposes of illustration. 
  70.   // This way would ensure no race conditions exist, but it's probably not the most efficient depending on how many requests, etc we're dealing with. 
  71.   myStruct->cleanup = evtimer_new(Base_Primary, connection_free, (void *)myStruct); 
  72.   evtimer_add(myStruct->cleanup, &Timeout); 
  73.  
  74.   //printf("http_request_done, we put our custom strucutre into a cleanup event to be freed!\n"); 
  75. } 
  76.  
  77. int http_req(char *uri) { 
  78.  
  79.   // Allocate our custom struture 
  80.   my_struct_t *myStruct = malloc(sizeof(my_struct_t)); 
  81.  
  82.   // Create our EVHTP connection and request 
  83.   myStruct->uri = evhttp_uri_parse(uri); 
  84.   myStruct->conn = evhttp_connection_base_new(Base_Primary, NULL, uri, 80); 
  85.   myStruct->req = evhttp_request_new(http_request_done, myStruct); 
  86.   evhttp_add_header(evhttp_request_get_output_headers(myStruct->req), "Host", "localhost"); 
  87.   evhttp_add_header(evhttp_request_get_output_headers(myStruct->req), "Connection", "close"); 
  88.   evhttp_make_request(myStruct->conn, myStruct->req, EVHTTP_REQ_GET, uri); 
  89.   evhttp_connection_set_timeout(myStruct->req->evcon, 2); 
  90.   return 1; 
  91. } 
  92.  
  93.  
  94. // Define our primary function 
  95. int main(int argc, char *argv[]) { 
  96.  
  97.   // Initialize our bases 
  98.   Base_Primary = event_base_new(); 
  99.  
  100.   char filename[] = "/tmp/50000.txt"; //文件名 
  101.   FILE *fp;  
  102.   char StrLine[1024];             //每行最大读取的字符数 
  103.   char *host; 
  104.   if((fp = fopen(filename,"r")) == NULL) //判断文件是否存在及可读 
  105.   {  
  106.     printf("error!");  
  107.     return -1;  
  108.   }  
  109.  
  110.   while (!feof(fp))  
  111.   {  
  112.     fgets(StrLine,1024,fp);  //读取一行 
  113.     host = StrLine; 
  114.     host = trimwhitespace(host); 
  115.     //printf("%s", host); //输出 
  116.     http_req(host); 
  117.   } 
  118.   fclose(fp);   
  119.  
  120.   // 
  121.   //event_base_loop(Base_Primary); 
  122.   event_base_dispatch(Base_Primary); 
  123.  
  124.   // Free our primary base 
  125.   event_base_free(Base_Primary); 
  126.   return 1; 
  127. }

Tags: Libevent HTTP客户端

分享到: