音效素材网提供各类素材,打造精品素材网站!

站内导航 站长工具 投稿中心 手机访问

音效素材

php curl批处理实现可控并发异步操作示例
日期:2021-09-06 20:07:46   来源:脚本之家

本文实例讲述了php curl批处理实现可控并发异步操作。分享给大家供大家参考,具体如下:

通常情况下 PHP 中的 cURL 是阻塞运行的,就是说创建一个 cURL 请求以后必须等它执行成功或者超时才会执行下一个请求:API接口访问一般会首选CURL

在实际项目或者自己编写小工具(比如新闻聚合,商品价格监控,比价)的过程中, 通常需要从第3方网站或者API接口获取数据, 在需要处理1个URL队列时, 为了提高性能, 可以采用cURL提供的curl_multi_*族函数实现简单的并发.

<?php
include 'curl.class.php';
function callback($response, $info, $error, $request)
{
 echo 'response:<br>';
 print_r($response);
 echo '<br>' . date("Y-m-d H:i:s") . '&nbsp;&nbsp;&nbsp;<br>';
 echo '<br>' . str_repeat("-", 100) . '<br>';
}
$USER_COOKIE = (!empty($_REQUEST['cookie'])) ? $_REQUEST['cookie'] : file_get_contents("cookie.txt");
$curl = new Curl ("callback");
$data = array(
 array(
  'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=qmr&type=rec_gametime&referfrom=&rt=0.42521539455332336', //秦美人
  'method' => 'POST',
  'post_data' => '',
  'header' => null,
  'options' => array(
   CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=qmr&fenQuNum=3",
   CURLOPT_COOKIE => $USER_COOKIE,
  )
 ),
 array(
  'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=sq&type=rec_gametime&referfrom=&rt=0.42521539455332336', //神曲
  'method' => 'POST',
  'post_data' => '',
  'header' => null,
  'options' => array(
   CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=sq&fenQuNum=41",
   CURLOPT_COOKIE => $USER_COOKIE,
  )
 ),
 array(
  'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=frxz&type=rec_gametime&referfrom=&rt=0.42521539455332336', //凡人修真
  'method' => 'POST',
  'post_data' => '',
  'header' => null,
  'options' => array(
   CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=frxz&fenQuNum=3",
   CURLOPT_COOKIE => $USER_COOKIE,
  )
 ),
 array(
  'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=smxj&type=rec_gametime&referfrom=&rt=0.42521539455332336', //神魔仙界
  'method' => 'POST',
  'post_data' => '',
  'header' => null,
  'options' => array(
   CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=smxj&fenQuNum=2",
   CURLOPT_COOKIE => $USER_COOKIE,
  )
 ),
 array(
  'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=qsqy&type=rec_gametime&referfrom=&rt=0.42521539455332336', //倾世情缘
  'method' => 'POST',
  'post_data' => '',
  'header' => null,
  'options' => array(
   CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=qsqy&fenQuNum=11",
   CURLOPT_COOKIE => $USER_COOKIE,
  )
 ),
);
foreach ($data as $val) {
 $request = new Curl_request ($val ['url'], $val ['method'], $val ['post_data'], $val ['header'], $val ['options']);
 $curl->add($request);
}
$curl->execute();
echo $curl->display_errors();

使用下来效果很好,没有副作用,并发数可控,应用之处多多,自己发挥想象吧

<?php
/**
 * cURL批量处理 工具类
 * 
 * @since Version 1.0
 * @author Justmepzy <justmepzy@gmail.com>
 * @link http://t.qq.com/JustPzy
 */
/**
 *单一的请求对象
 */
class Curl_request {
 public $url   = '';
 public $method   = 'GET';
 public $post_data  = null;
 public $headers  = null;
 public $options  = null;
 /**
  * 
  * @param string $url
  * @param string $method
  * @param string $post_data
  * @param string $headers
  * @param array $options
  * @return void
  */
 public function __construct($url, $method = 'GET', $post_data = null, $headers = null, $options = null) {
  $this->url = $url;
  $this->method = strtoupper( $method );
  $this->post_data = $post_data;
  $this->headers = $headers;
  $this->options = $options;
 }
 /**
  * @return void
  */
 public function __destruct() {
  unset ( $this->url, $this->method, $this->post_data, $this->headers, $this->options );
 }
}
/**
 * 包含请求列队处理
 */
class Curl {
 /**
  * 请求url个数
  * @var int
  */
 private $size    = 5;
 /**
  * 等待所有cURL批处理中的活动连接等待响应时间
  * @var int
  */
 private $timeout   = 5;
 /**
  * 完成请求回调函数
  * @var string
  */
 private $callback   = null;
 /**
  * cRUL配置
  * @var array
  */
 private $options   = array (CURLOPT_SSL_VERIFYPEER => 0,CURLOPT_RETURNTRANSFER => 1,CURLOPT_CONNECTTIMEOUT => 30 );
 /**
  * 请求头
  * @var array
  */
 private $headers   = array ();
 /**
  * 请求列队
  * @var array
  */
 private $requests   = array ();
 /**
  * 请求列队索引
  * @var array
  */
 private $request_map  = array ();
 /**
  * 错误
  * @var array
  */
 private $errors   = array ();
 /**
  * @access public
  * @param string $callback 回调函数
  * 该函数有4个参数($response,$info,$error,$request)
  * $response url返回的body
  * $info  cURL连接资源句柄的信息
  * $error  错误
  * $request  请求对象
  */
 public function __construct($callback = null) {
  $this->callback = $callback;
 }
 /**
  * 添加一个请求对象到列队
  * @access public
  * @param object $request
  * @return boolean
  */
 public function add($request) {
  $this->requests [] = $request;
  return TRUE;
 }
 /**
  * 创建一个请求对象并添加到列队
  * @access public
  * @param string $url
  * @param string $method
  * @param string $post_data
  * @param string $headers
  * @param array $options
  * @return boolean
  */
 public function request($url, $method = 'GET', $post_data = null, $headers = null, $options = null) {
  $this->requests [] = new Curl_request ( $url, $method, $post_data, $headers, $options );
  return TRUE;
 }
 /**
  * 创建GET请求对象
  * @access public
  * @param string $url
  * @param string $headers
  * @param array $options
  * @return boolean
  */
 public function get($url, $headers = null, $options = null) {
  return $this->request ( $url, "GET", null, $headers, $options );
 }
 /**
  * 创建一个POST请求对象
  * @access public
  * @param string $url
  * @param string $post_data
  * @param string $headers
  * @param array $options
  * @return boolean
  */
 public function post($url, $post_data = null, $headers = null, $options = null) {
  return $this->request ( $url, "POST", $post_data, $headers, $options );
 }
 /**
  * 执行cURL
  * @access public
  * @param int $size 最大连接数
  * @return Ambigous <boolean, mixed>|boolean
  */
 public function execute($size = null) {
  if (sizeof ( $this->requests ) == 1) {
   return $this->single_curl ();
  } else {
   return $this->rolling_curl ( $size );
  }
 }
 /**
  * 单个url请求
  * @access private
  * @return mixed|boolean
  */
 private function single_curl() {
  $ch = curl_init ();
  $request = array_shift ( $this->requests );
  $options = $this->get_options ( $request );
  curl_setopt_array ( $ch, $options );
  $output = curl_exec ( $ch );
  $info = curl_getinfo ( $ch );
  // it's not neccesary to set a callback for one-off requests
  if ($this->callback) {
   $callback = $this->callback;
   if (is_callable ( $this->callback )) {
    call_user_func ( $callback, $output, $info, $request );
   }
  } else
   return $output;
  return true;
 }
 /**
  * 多个url请求
  * @access private
  * @param int $size 最大连接数
  * @return boolean
  */
 private function rolling_curl($size = null) {
  if ($size)
   $this->size = $size;
  else 
   $this->size = count($this->requests);
  if (sizeof ( $this->requests ) < $this->size)
   $this->size = sizeof ( $this->requests );
  if ($this->size < 2)
   $this->set_error ( 'size must be greater than 1' );
  $master = curl_multi_init ();
  //添加cURL连接资源句柄到map索引
  for($i = 0; $i < $this->size; $i ++) {
   $ch = curl_init ();
   $options = $this->get_options ( $this->requests [$i] );
   curl_setopt_array ( $ch, $options );
   curl_multi_add_handle ( $master, $ch );
   $key = ( string ) $ch;
   $this->request_map [$key] = $i;
  }
  $active = $done = null;
  do {
   while ( ($execrun = curl_multi_exec ( $master, $active )) == CURLM_CALL_MULTI_PERFORM )
    ;
   if ($execrun != CURLM_OK)
    break;
   //有一个请求完成则回调
   while ( $done = curl_multi_info_read ( $master ) ) {
    //$done 完成的请求句柄
    $info = curl_getinfo ( $done ['handle'] );//
    $output = curl_multi_getcontent ( $done ['handle'] );//
    $error = curl_error ( $done ['handle'] );//
    $this->set_error ( $error );
    //调用回调函数,如果存在的话
    $callback = $this->callback;
    if (is_callable ( $callback )) {
     $key = ( string ) $done ['handle'];
     $request = $this->requests [$this->request_map [$key]];
     unset ( $this->request_map [$key] );
     call_user_func ( $callback, $output, $info, $error, $request );
    }
    curl_close ( $done ['handle'] );
    //从列队中移除已经完成的request
    curl_multi_remove_handle ( $master, $done ['handle'] );
   }
   //等待所有cURL批处理中的活动连接
   if ($active)
    curl_multi_select ( $master, $this->timeout );
  } while ( $active );
  //完成关闭
  curl_multi_close ( $master );
  return true;
 }
 /**
  * 获取没得请求对象的cURL配置
  * @access private
  * @param object $request
  * @return array
  */
 private function get_options($request) {
  $options = $this->__get ( 'options' );
  if (ini_get ( 'safe_mode' ) == 'Off' || ! ini_get ( 'safe_mode' )) {
   $options [CURLOPT_FOLLOWLOCATION] = 1;
   $options [CURLOPT_MAXREDIRS] = 5;
  }
  $headers = $this->__get ( 'headers' );
  if ($request->options) {
   $options = $request->options + $options;
  }
  $options [CURLOPT_URL] = $request->url;
  if ($request->post_data && strtolower($request->method) == 'post' ) {
   $options [CURLOPT_POST] = 1;
   $options [CURLOPT_POSTFIELDS] = $request->post_data;
  }
  if ($headers) {
   $options [CURLOPT_HEADER] = 0;
   $options [CURLOPT_HTTPHEADER] = $headers;
  }
  return $options;
 }
 /**
  * 设置错误信息
  * @access public
  * @param string $msg
  */
 public function set_error($msg) {
  if (! empty ( $msg ))
   $this->errors [] = $msg;
 }
 /**
  * 获取错误信息
  * @access public
  * @param string $open
  * @param string $close
  * @return string
  */
 public function display_errors($open = '<p>', $close = '</p>') {
  $str = '';
  foreach ( $this->errors as $val ) {
   $str .= $open . $val . $close;
  }
  return $str;
 }
 /**
  * @access public
  * @param string $name
  * @param string $value
  * @return boolean
  */
 public function __set($name, $value) {
  if ($name == 'options' || $name == 'headers') {
   $this->{$name} = $value + $this->{$name};
  } else {
   $this->{$name} = $value;
  }
  return TRUE;
 }
 /**
  * 
  * @param string $name
  * @return mixed
  * @access public
  */
 public function __get($name) {
  return (isset ( $this->{$name} )) ? $this->{$name} : null;
 }
 /**
  * @return void
  * @access public
  */
 public function __destruct() {
  unset ( $this->size, $this->timeout, $this->callback, $this->options, $this->headers, $this->requests, $this->request_map, $this->errors );
 }
}
// END Curl Class
/* End of file curl.class.php */

希望本文所述对大家PHP程序设计有所帮助。

    您感兴趣的教程

    在docker中安装mysql详解

    本篇文章主要介绍了在docker中安装mysql详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编...

    详解 安装 docker mysql

    win10中文输入法仅在桌面显示怎么办?

    win10中文输入法仅在桌面显示怎么办?

    win10系统使用搜狗,QQ输入法只有在显示桌面的时候才出来,在使用其他程序输入框里面却只能输入字母数字,win10中...

    win10 中文输入法

    一分钟掌握linux系统目录结构

    这篇文章主要介绍了linux系统目录结构,通过结构图和多张表格了解linux系统目录结构,感兴趣的小伙伴们可以参考一...

    结构 目录 系统 linux

    PHP程序员玩转Linux系列 Linux和Windows安装

    这篇文章主要为大家详细介绍了PHP程序员玩转Linux系列文章,Linux和Windows安装nginx教程,具有一定的参考价值,感兴趣...

    玩转 程序员 安装 系列 PHP

    win10怎么安装杜比音效Doby V4.1 win10安装杜

    第四代杜比®家庭影院®技术包含了一整套协同工作的技术,让PC 发出清晰的环绕声同时第四代杜比家庭影院技术...

    win10杜比音效

    纯CSS实现iOS风格打开关闭选择框功能

    这篇文章主要介绍了纯CSS实现iOS风格打开关闭选择框,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作...

    css ios c

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的办法

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的

    Win7给电脑C盘扩容的办法大家知道吗?当系统分区C盘空间不足时,就需要给它扩容了,如果不管,C盘没有足够的空间...

    Win7 C盘 扩容

    百度推广竞品词的投放策略

    SEM是基于关键词搜索的营销活动。作为推广人员,我们所做的工作,就是打理成千上万的关键词,关注它们的质量度...

    百度推广 竞品词

    Visual Studio Code(vscode) git的使用教程

    这篇文章主要介绍了详解Visual Studio Code(vscode) git的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...

    教程 Studio Visual Code git

    七牛云储存创始人分享七牛的创立故事与

    这篇文章主要介绍了七牛云储存创始人分享七牛的创立故事与对Go语言的应用,七牛选用Go语言这门新兴的编程语言进行...

    七牛 Go语言

    Win10预览版Mobile 10547即将发布 9月19日上午

    微软副总裁Gabriel Aul的Twitter透露了 Win10 Mobile预览版10536即将发布,他表示该版本已进入内部慢速版阶段,发布时间目...

    Win10 预览版

    HTML标签meta总结,HTML5 head meta 属性整理

    移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析HTML代码,更好地将移动web前端页面表现出来...

    移动端html5模拟长按事件的实现方法

    这篇文章主要介绍了移动端html5模拟长按事件的实现方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家...

    移动端 html5 长按

    HTML常用meta大全(推荐)

    这篇文章主要介绍了HTML常用meta大全(推荐),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    cdr怎么把图片转换成位图? cdr图片转换为位图的教程

    cdr怎么把图片转换成位图? cdr图片转换为

    cdr怎么把图片转换成位图?cdr中插入的图片想要转换成位图,该怎么转换呢?下面我们就来看看cdr图片转换为位图的...

    cdr 图片 位图

    win10系统怎么录屏?win10系统自带录屏详细教程

    win10系统怎么录屏?win10系统自带录屏详细

    当我们是使用win10系统的时候,想要录制电脑上的画面,这时候有人会想到下个第三方软件,其实可以用电脑上的自带...

    win10 系统自带录屏 详细教程

    + 更多教程 +
    ASP编程JSP编程PHP编程.NET编程python编程