python并发1000个http请求_php下api接口的并发http请求

php下api接口的并发http请求 ,提高app一个页面请求多个api接口,页面加载慢的问题;

func_helper.php
/**
 * 并发http请求
 *
 * [
 *  'url' //请求地址
 *  'method' //请求类型 默认为 get 请求
 *  'params'
 * ]
 */
if(!function_exists('multi_curl_smt')) {
    function multi_curl_smt($requests = [])
    {
        $response = [];
        $hander = [];
        $mh = curl_multi_init();

        foreach($requests as $id=>$item) {
            if(empty($item['url'])) {
                throw new Exception('基本参数URL不能为空');
            }

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $item['url']);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);


            curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
            if(defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')){
                curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
            }
            curl_setopt($ch, CURLOPT_TIMEOUT, isset($item['timeout']) ? $item['timeout'] : 3);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, isset($item['method']) ? $item['method'] : 'GET');

            //设置请求参数
            if(isset($item['data'])) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($item['data']));
            }

            //暂时屏蔽代理服务器
            //ENVIRONMENT == 'development' && curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888'); //设置代理服务器
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);

            if(defined('API_USERNAME') && defined('API_PASSWORD')) {
                curl_setopt($ch, CURLOPT_USERPWD, API_USERNAME . ":" . API_PASSWORD);
            }

            curl_multi_add_handle($mh,$ch);
            $hander[$id] = $ch;
        }

        $running=null;
        do {
            usleep(1000);
            curl_multi_exec($mh,$running);
        } while ($running > 0);

        //get content and remote handle
        foreach($hander as $id=>$ch) {
            $response[$id] = curl_multi_getcontent($ch);
            //检查结果是否需要转换为json, 默认转换
            $toArray = isset($requests[$id]['to_array']) ? $requests[$id]['to_array'] == true : true;
            if($toArray) {
                //$response[$id] = json_decode(trim($data, chr(239) . chr(187) . chr(191)), true);
                $response[$id] = json_decode($response[$id], true);
            }
            curl_multi_remove_handle($mh, $ch);
        }
        curl_multi_close($mh);
        
        //检查是否进行数组转换, 默认转换为数组

        return $response;
    }



调用
shop.php
public function index()
{

    $param= '';
    $class_id = null;
    if ($key = trim($this->input->get('key')))
    {
        $param .= '/key/'.urlencode($key);

    }
    if ($class_id = $this->input->get('shop_class_id2'))
    {
        $param .= '/shop_class/'.$class_id;
    }elseif ($class_id = $this->input->get('shop_class_id'))
    {
        $param .= '/shop_class/'.$class_id;
    }

    if ($this->cur_page)
    {
        $param .= '/page/'.$this->cur_page;
    }
    if ($this->per_page)
    {
        $param .= '/limit/'.$this->per_page;
    }
以上是api接口的并发http请求的一个方法   直接复制可以用,
------------------------------------------------------------------------------

下面是调用api接口的并发http请求方法的例子 ,


    //门店分类

    $multiParams = [];

    $store_id= $this->aSession['store_id'];
    $multiParams['shop_class']= $this->_shop_class();
    $multiParams['shop_class_children']= $this->_shop_class_children();
    $multiParams['shop_store']= $this->_shop_store($store_id);

    $result = multi_curl_smt($multiParams);
    //处理返回结果

    //获取一级分类
    $shop_class['data'] = $result['shop_class']['data'];

    if(!isset( $shop_class['data']))
    {
        $shop_class['data'] = '';
    }
    //门店所有分类
    $shop_class_children['data'] = $result['shop_class_children']['data'];

    if(!isset($shop_class_children['data']))
    {
        $shop_class['data'] = '';
    }

    //当前店铺下所有的门店
    $shop_store = $result['shop_store']['data'];
    if(empty($shop_store))
    {
        $temp = null;
    }else
    {
        $temp = $shop_store;
    }
    if ($this->aSession['group_id'] == 1)
    {
        $shop_list_url = $this->config->item('shop_all_store', 'api_url').'/store_id/'.$this->aSession['store_id'].$param;
        $shop_list = curl_smt($shop_list_url);

    }else{
        //获取门店列表
        $shop_list_url = $this->config->item('shop_list', 'api_url').'/seller_id/'.$this->aSession['seller_id'].$param;
        $shop_list = curl_smt($shop_list_url);
    }

    //数据整理
    $list = array();
    if ($shop_list['error'] == 'ok' && $shop_class_children['data'] != "" )
    {
        $tem = $shop_list['data']['list'];

        foreach ($tem as $k => $val)
        {

            foreach ($temp as $v1)
            {
                //找出上级门店
                if ($tem[$k]['shop_parent_id'] == $v1['shop_id'])
                {
                    $tem[$k]['shop_parent_id'] = $v1['shop_name'];
                }
            }
            foreach ($shop_class_children['data'] as $v)
            {
                if ($tem[$k]['shop_class_id'] == $v['class_id'])
                {
                    $tem[$k]['shop_class_id'] = $v['class_name'];
                }
            }
            $saas_order_db=$this->load->database('saas',TRUE);
            $store_info = $saas_order_db->from('store')->where(array('store_id'=>$val['store_id']))->select('store_name')->get()->row_array();           
 if ($store_info) {               
 $tem[$k]['store_name'] = $store_info['store_name'] ;          
  }       
 }       
 $list = $tem;      
  $this->aData['pagination'] = $this->page_div($shop_list['data']['total']);  
  }   

 $page= isset($shop_list['data'])? ceil($shop_list['data']['total'] / $this->per_page):0;   
 $this->load->vars('shop_name', $key);   
 $this->load->vars('class_id', $class_id);   
 $this->load->vars('shop_class', $shop_class['data']);   
 $this->load->vars('list', $list);    
 $this->load->vars('page', $page);   
 $this->load->view('store/shop_index',$this->aData);}

/**
* 以下为接口的请求数据的条件和url地址
*
**/
//(条件)
private function _shop_class(){ 
   $where = array();   
 return ['url'=>$this->config->item('shop_class_children', 'api_url').'/class_parent_id/1', 'data'=>$where];}

//(条件)
private function _shop_store($store_id){  
  $where = array();   
 $field ='shop_name';   
 return ['url'=>$this->config->item('shop_all_store', 'api_url').'/all/1/store_id/'.$store_id.'/field/'.$field, 'data'=>$where];
}
//(条件)
private function _shop_class_children(){ 
   $where = array();    
$where['class_parent_id']='';   
 return ['url'=>$this->config->item('shop_class_children', 'api_url'), 'data'=>$where];
}

以上内容希望帮助到大家,很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要戳这里PHP进阶架构师>>>视频、面试文档免费获取

或 者关注我每天分享技术文章

进阶PHP架构师​www.zhihu.com
3a1b9d464edfd5aa2050ae624ecfb995.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值