实现:
需要用到curl.so动态库,例如:
复制代码代码示例:
package.path = '/usr/local/share/lua/5.1/?.lua;/usr/local/openresty/lualib/resty/?.lua;'
package.cpath = '/usr/local/lib/lua/5.1/?.so;' require("curl") function build_w_cb(f) return function(s,len) ngx.print(s) ngx.flush(true) --local wlen = f:write(s) --if (wlen ~= len) then -- return len,"writeError" -- else return len,nil -- end end end file = io.open("/usr/local/openresty/nginx/html/me.ts", "w") if (file == nil) then ngx.say ("Create File error") return end ngx.header["Transfer-Encoding"] = 'chunked'; --Transfer-Encoding: chunked --download XML File xmlURL = "http://192.168.1.102/pxx/f1.ts" c = curl.easy_init() c:setopt(curl.OPT_URL, xmlURL) c:setopt(curl.OPT_WRITEFUNCTION, build_w_cb(file)) c:setopt(curl.OPT_FOLLOWLOCATION, 1) c:setopt(curl.OPT_USERAGENT, "ContentPreload-agent/1.0") c:setopt(curl.OPT_COOKIEFILE, "./curlpost.cookie") c:setopt(curl.OPT_CONNECTTIMEOUT, 5) c:setopt(curl.OPT_TIMEOUT, 3000) c:setopt(curl.OPT_NOSIGNAL, 1) ret,strerr = c:perform() file:close()
此例可以运行,不过有如下的缺点:
原因分析: |