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];
}
php下api接口的并发http请求的更多相关文章
- App后台开发运维和架构实践学习总结(3)——RestFul架构下API接口设计注意点
1. 争取相容性和统一性 这里就要求让API设计得是可预测的.按照这种方式写出所有接口和接口所需要的参数.现在就要确保命名是一致的,接口所需的参数顺序也是一致的.你现在应该有products,orde ...
- 大叔也说Xamarin~Android篇~调用远程API接口,发POST请求
回到目录 Xamarin我们在上节已经教大家如何去部署它的环境了,今天来说一个实际的例子,使用android客户调用.net web api的一个接口,并发送POST请求,当服务端回到请求后做出响应, ...
- Spring框架下的 “接口调用、MVC请求” 调用参数、返回值、耗时信息输出
主要拦截前端或后天的请求,打印请求方法参数.返回值.耗时.异常的日志.方便开发调试,能很快定位到问题出现在哪个方法中. 前端请求拦截,mvc的拦截器 import java.util.Date; im ...
- 从api接口获取数据-okhttp
首先先介绍下api接口: API:应用程序接口(API:Application Program Interface) 通常用于数据连接,调用函数提供功能等等... 从api接口获取数据有四种方式:Ht ...
- HTTP API接口安全设计
HTTP API接口安全设计 API接口调用方式 HTTP + 请求签名机制 HTTP + 参数签名机制 HTTPS + 访问令牌机制 有没有更好的方案? OAuth授权机制 OAuth2.0服务 ...
- API 接口的安全设计验证:ticket,签名,时间戳
一.背景 1.与前端对接的API接口,如果被第三方抓包并进行恶意篡改参数,可能会导致数据泄露,甚至会被篡改数据 2.与第三方公司的接口对接,第三方如果得到你的接口文档,但是接口确没安全校验,是十分不安 ...
- 亿级用户下的新浪微博平台架构 前端机(提供 API 接口服务),队列机(处理上行业务逻辑,主要是数据写入),存储(mc、mysql、mcq、redis 、HBase等)
https://mp.weixin.qq.com/s/f319mm6QsetwxntvSXpKxg 亿级用户下的新浪微博平台架构 炼数成金前沿推荐 2014-12-04 序言 新浪微博在2014年3月 ...
- Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口
1.WebApi系列~通过HttpClient来调用Web Api接口 http://www.cnblogs.com/lori/p/4045413.html HttpClient使用详解(java版本 ...
- 使用HttpWebRequest请求API接口以及其他网站资源
很多时候,我们项目需要其他网站的资源,而这个被请求的网站可能属于你们自己开发管理的网站.也可能是公网上其他网站对外开发的API接口,比如说腾讯的微信公众平台的API接口.各大短信服务商的短信API接口 ...
随机推荐
- jmeter+WebDriver:启动浏览器进行web自动化
无论是web自动化还是手机app自动化,WebDriver是Selenium的核心模块,jmeter WebDriver 仅支持Firefox.Chrome 和 HTML Unit驱动,暂不支持IE ...
- HTML的发展及认识
首先HTML全称是Hypertext Markup Language,它是一门超文本标记语言: HTML已经有了HTML2.0.HTML3.2.HTML 4.0. HTML4.01. HTML5几个阶 ...
- ubuntu18.04安装docker
本文基于unbuntu18.04版本来安装docker,步骤如下: 1:右击桌面->打开终端(E). 2::输入以下命令: sudo snap install docker ,输入密码之后出下图 ...
- Docker学习总结(一)--Docker简介
什么是虚拟化 在计算机中,虚拟化是一种资源管理技术,是将计算机的各种实体资源,如服务器.网络.内存等,以抽象.转换后呈现出来,打破实体结构间的不可切割的障碍,使用户可以比之前更好的应用这些资源. 在实 ...
- HTML 画布(摘自菜鸟教程)
颜色.样式和阴影 属性 描述 fillStyle 设置或返回用于填充绘画的颜色.渐变或模式. strokeStyle 设置或返回用于笔触的颜色.渐变或模式. shadowColor 设置或返回用于阴影 ...
- 特殊字符处理 java-jsp
public String dealStr(String name){ String newStr=""; if(name != null && name.leng ...
- P3980 [NOI2008]志愿者招募 费用流 (人有多大胆地有多大产
https://www.luogu.org/problemnew/show/P3980 感觉费用流比网络流的图更难想到,要更大胆.首先由于日期是连续的,所以图中的点是横向排列的. 这道题有点绕道走的意 ...
- P3705 [SDOI2017]新生舞会 分数规划 费用流
#include <algorithm> #include <iterator> #include <iostream> #include <cstring& ...
- lightoj 1061 - N Queen Again(状压dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1061 题解:显然能满足情况的8皇后的摆法不多,于是便可以用题目给出的状态来匹配 ...
- 爬虫反爬之代理IP
爬虫反爬之代理IP 代理IP其实本就是在requests模块中的参数 定义: 代替原来的IP地址去对接网络的IP地址. 作用: 隐藏自身真实IP,避免被封. 获取代理IP网站 西刺代理.快代理.全网代 ...