php 怎么获取 drizzle-nginx-module 返回的json 数据

117 views
Skip to first unread message

瞿秋丰

unread,
May 15, 2013, 11:23:03 PM5/15/13
to open...@googlegroups.com
drizzle-nginx-module  有连接池又是不阻塞的  
我想 用 openresty 直接做一个读取mysql数据返回json的api
这样就可以避免 php 读取mysql数据的一些弊端  
我怎么获取api返回的数据会高效一些   假定 api url 是   http://localhost/api/?key=xxx
用file_get_contents 函数  还是 用  curl ?

盼给个思路  打扰了

Lance

unread,
May 15, 2013, 11:33:33 PM5/15/13
to open...@googlegroups.com
curl 更灵活些,缺点是需要新启动一个进程,cpu多耗些

Lance


2013/5/16 瞿秋丰 <quqi...@gmail.com>

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
 
 

杜哲

unread,
May 16, 2013, 11:34:34 PM5/16/13
to open...@googlegroups.com
如果接口都是GET请求的话,  file_get_contents就行。
另外, 楼主说的curl应该是php的libcurl扩展吧, 这个应该不会起新进程的吧?


2013/5/16 Lance <lance...@gmail.com>



--
杜哲

瞿秋丰

unread,
May 16, 2013, 11:37:20 PM5/16/13
to open...@googlegroups.com
是php的 libcurl扩展   春哥说用 ngx_lua  直接替换php   那估计是要用这个了 http://leafo.net/lapis/


在 2013年5月17日星期五UTC+8上午11时34分34秒,杜哲写道:

Lance

unread,
May 17, 2013, 12:20:25 AM5/17/13
to open...@googlegroups.com
哦,我理解错了,但我实际使用中确实感觉单起curl进程更实用,因为它能异步处理,不过具体到你的需求就不合适了。

Lance


2013/5/17 瞿秋丰 <quqi...@gmail.com>

XinQi Yang

unread,
May 19, 2013, 8:25:00 PM5/19/13
to open...@googlegroups.com
curl ,  file_get_content,  readfile 都可以, 如果你想流试传输的话, 直接使用readfile也ok,如果json在php端会在包装的话,用哪个都无所谓, 如果想之前将 json 传输给客户端的话,就可以使用readfile,读取json输出,php只是一个中转了.
xinqiyang

瞿秋丰

unread,
May 20, 2013, 5:04:43 AM5/20/13
to open...@googlegroups.com
可以这样
location /
{
set_by_lua $data '
local res = ngx.location.capture("/mysql_data_api")
if res.status == 200 then
  return res.body
end
          return 0
   ';
   fastcgi_index  index.php;
   fastcgi_pass   unix:/tmp/php.sock;
            include           fastcgi_params;
   fastcgi_param  PATH_INFO $fastcgi_script_name;
   fascgi_param   data $data;
}
php 处理
<?php
$data = json_decode(getenv('data'));
print_r($data);

新问题 

ngx.location.capture_multi

并发请求的数据怎么传递给 php 呢   我发新贴问下

在 2013年5月20日星期一UTC+8上午8时25分00秒,xinqiyang写道:

瞿秋丰

unread,
May 20, 2013, 5:45:21 AM5/20/13
to open...@googlegroups.com

ngx.location.capture  不支持 set_by_lua 上下文  泪奔~

在 2013年5月20日星期一UTC+8下午5时04分43秒,瞿秋丰写道:

agentzh

unread,
May 20, 2013, 3:06:37 PM5/20/13
to openresty
Hello!

2013/5/20 瞿秋丰:
>
> ngx.location.capture 不支持 set_by_lua 上下文 泪奔~
>

为何不使用 rewrite_by_lua?

set_by_lua 本来就不允许进行(非阻塞)I/O 操作的。

Regards,
-agentzh

瞿秋丰

unread,
May 20, 2013, 10:10:37 PM5/20/13
to open...@googlegroups.com
多谢春哥   已经实现  谢谢大家

在 2013年5月21日星期二UTC+8上午3时06分37秒,agentzh写道:
Reply all
Reply to author
Forward
0 new messages