PHP异步执行(curl)

引入AsyncCURL类,AsyncCURL.php

下载链接 https://download.csdn.net/download/I_lost/19871507

<?php
namespace app\common\lib\tools;
/*
  // 使用范例
  // 传入参数说明
  // url 请求地址
  // data POST方式数据
  //并发调用
  $param1 = array(
      array(
        'url' => "http://localhost/a.php?s=1",
        ),
      array(
        'url' => "http://localhost/a.php?s=1",
        'data' => array('aaa' => 1, 'bbb' => 2),
        ),
      );
  //单个调用
  $param2 = array(
      'url' => "http://localhost/a.php?s=0",
      'data' => array('aaa' => 1, 'bbb' => 2),
      );
  //单个调用(GET简便方式)
  $param3 = 'http://localhost/a.php?s=2';
  $ac = new AsyncCURL();
  $ac->set_param($param1);
  $ret = $ac->send();
  //返回值为请求参数顺序的结果数组(元素值为False表示请求错误)
  var_dump($ret);
*/

class AsyncCURL
{
    /**
     * 是否需要返回HTTP头信息
     */
    public $curlopt_header = 0;
    public $header=[];
    /**
     * 单个CURL调用超时限制
     */
    public $curlopt_timeout = 0.1;
    private $param = array();
    /**
     * 构造函数(可直接传入请求参数)
     *
     * @param array 可选
     * @return void
     */
    public function __construct($param = False)
    {
        ini_set('max_execution_time',(string)(60*60*12));
        if ($param !== False)
        {
            $this->param = $this->init_param($param);
        }
    }
    /**
     * 设置请求参数
     *
     * @param array
     * @return void
     */
    public function set_param($param){
        $this->param = $this->init_param($param);
    }

    public function set_header($header){
        $this->header=$header;
    }
    /**
     * 发送请求
     *
     * @return array
     */
    public function send()
    {
        if(!is_array($this->param) || !count($this->param))
        {
            return False;
        }
        $curl = $ret = array();
        $handle = curl_multi_init();
        foreach ($this->param as $k => $v)
        {
            $param = $this->check_param($v);
            if (!$param) $curl[$k] = False;
            else $curl[$k] = $this->add_handle($handle, $param);
        }
        $this->exec_handle($handle);
        foreach ($this->param as $k => $v)
        {
            if ($curl[$k])
            {
                $ret[$k] = curl_multi_getcontent($curl[$k]);
                curl_multi_remove_handle($handle, $curl[$k]);
            } else {
                $ret[$k] = False;
            }
        }
        curl_multi_close($handle);
        return $ret;
    }

    private function init_param($param)
    {
        $ret = False;
        if (isset($param['url']))
        {
            $ret = array($param);
        } else {
            $ret = isset($param[0]) ? $param : False;
        }
        return $ret;
    }
    private function check_param($param = array())
    {
        $ret = array();
        if (is_string($param))
        {
            $url = $param;
        } else {
            extract($param);
        }
        if (isset($url))
        {
            $url = trim($url);
            $url = (stripos($url, 'https://') === 0 || stripos($url, 'http://') === 0 )? $url : NULL;
        }
        if (isset($data)  && !empty($data))
        {
            $method = 'POST';
        } else {
            $method = 'GET';
            unset($data);
        }
        if (isset($url)) $ret['url'] = $url;
        if (isset($method)) $ret['method'] = $method;
        if (isset($data)) $ret['data'] = $data;
        $ret = isset($url) ? $ret : False;
        return $ret;
    }
    private function add_handle($handle, $param)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $param['url']);
        curl_setopt($curl, CURLOPT_HEADER, $this->curlopt_header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, $this->curlopt_timeout);
        if(!empty($this->header)){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
        }
        if ($param['method'] == 'POST')
        {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $param['data']);
        }
        curl_multi_add_handle($handle, $curl);
        return $curl;
    }
    private function exec_handle($handle)
    {
        $flag = null;
        do {
            curl_multi_exec($handle, $flag);
        } while ($flag > 0);
    }
}

调用方法:

// data POST方式数据
//并发调用
$curlParam = [
  [
    'url' => "http://localhost/a.php?s=1",
    'data' => array('aaa' => 1, 'bbb' => 2),
  ],
  [
    'url' => "http://localhost/index/test",
    'data' => array('aaa' => 1, 'bbb' => 2),
  ]
];
  
//单个调用
$curlParam= [
    'url' => "http://localhost/index/test",
    'data' => array('aaa' => 1, 'bbb' => 2),
  ];

//单个调用(GET简便方式)
$curlParam= 'http://localhost/a.php?s=2';


$accurl = new AsyncCURL();
$accurl->set_param($curlParam);
$accurl->curlopt_timeout = 1;
$accurl->send();


注意:

调用方法里面要添加一行代码

ignore_user_abort(true);

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值