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接口 ...
随机推荐
- 企查查app 初步探索
企查查app sign算法破解初步探索 之前有说过企查查的sign的解密,但这次是企查查app的sign算法破解,目前是初步进程. 已删除!!!! 上边一些变量已经找到了,其中就有时间戳,其余两个需要 ...
- netty源码解解析(4.0)-19 ChannelHandler: codec--常用编解码实现
数据包编解码过程中主要的工作就是:在编码过程中进行序列化,在解码过程中从Byte流中分离出数据包然后反序列化.在MessageToByteEncoder中,已经解决了序列化之后的问题,ByteToMe ...
- Oracle - Tables
创建表 a: Sql语句创建 -- Create table create table Table_Name ( 字段1 VARCHAR2(50), 字段2 VARCHAR2(50) not null ...
- 关于web.xml配置
整理自网上: web应用是一种可以通过Web访问的应用程序.在J2EE领域下,web应用就是遵守基于JAVA技术的一系列标准的应用程序. 最简单的web应用什么样? 2个文件夹.1个xml文件就能成为 ...
- NLP(三) 预处理
分词 from nltk.tokenize import LineTokenizer,SpaceTokenizer,TweetTokenizer from nltk import word_token ...
- 牛客2018国庆集训派对Day3 I Metropolis 多源最短路径
传送门:https://www.nowcoder.com/acm/contest/203/I 题意: 求每个大都会到最近的一个大都会的距离. 思路: 把每个大都会设为起点,跑一遍最短路.在跑最短路的时 ...
- Spreading the Wealth uva 11300
A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...
- Requests库整理
一.Requests库的安装 win平台下,直接在命令行使用 pip install requests 即可进行安装 成功后测试如下 >>> import requests > ...
- 3.python之文件操作
一.文件操作初识 f = open('文件路径', '编码方式', '操作方式') # 注意里面所有内容,需加引号 ” 打开一个文件需要知道的内容有: 文件路径:c:\文件.txt(绝对路径和相对路径 ...
- SpringBoot初体验之整合SpringMVC
作为开发人员,大家都知道,SpringBoot是基于Spring4.0设计的,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程.另外Spr ...