吴浩介绍我看看《nodejs》,在我的VPS上配好后,发现它解决了一个问题:如何在PHP里模拟调用javascript环境。《NodeJS探索》写的幽默风趣,较为经典的话莫过于“这对于JS程序员来说,真是一个莫大的福音,因为你们再也不用看着那帮做后台的孙子的脸色干活了,可以翻身做后端了”。
拿采集爱漫画的程序举例子,http://imanhua.com/comic/120/list_50567.html?p=3,打开源代码,找到如下代码:
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('5 d=c;5 e=["/3/2/1/0/g.f","/3/2/1/0/b.4","/3/2/1/0/7.4","/3/2/1/0/6.4","/3/2/1/0/8.4","/3/2/1/0/9.4","/3/2/1/0/a.4","/3/2/1/0/h.4","/3/2/1/0/p.4","/3/2/1/0/o.4","/3/2/1/0/q.4","/3/2/1/0/s.4","/3/2/1/0/r.4","/3/2/1/0/n.4","/3/2/1/0/j.4","/3/2/1/0/i.4","/3/2/1/0/k.4","/3/2/1/0/m.4"];5 l=0;',29,29,'50567|120|Images|Files|png|var|imanhua_004_095646468|imanhua_003_095646453|imanhua_005_095646468|imanhua_006_095646484|imanhua_007_095646484|imanhua_002_095646453|18|len|pic|jpg|imanhua_001_095646437|imanhua_008_095646500|imanhua_016_095646531|imanhua_015_095646531|imanhua_017_095646531|sid|imanhua_018_095646546|imanhua_014_095646515|imanhua_010_095646500|imanhua_009_095646500|imanhua_011_095646500|imanhua_013_095646515|imanhua_012_095646515'.split('|'),0,{}))
在netbeans里,换行、缩进之后,变成:
eval(function(p,a,c,k,e,d){ e=function(c){ return(c<a?"":e(parseInt(c/a))) + ((c=c%a)>35?String.fromCharCode(c+29):c.toString(36)) }; if(!''.replace(/^/,String)){ while(c--)d[e(c)]=k[c]||e(c); k=[function(e){return d[e]}]; e=function(){return'\\w+'};c=1; }; while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]); return p; }('5 d=c;5 e=["/3/2/1/0/g.f","/3/2/1/0/b.4","/3/2/1/0/7.4","/3/2/1/0/6.4","/3/2/1/0/8.4","/3/2/1/0/9.4","/3/2/1/0/a.4","/3/2/1/0/h.4","/3/2/1/0/p.4","/3/2/1/0/o.4","/3/2/1/0/q.4","/3/2/1/0/s.4","/3/2/1/0/r.4","/3/2/1/0/n.4","/3/2/1/0/j.4","/3/2/1/0/i.4","/3/2/1/0/k.4","/3/2/1/0/m.4"];5 l=0;', 29, 29, '50567|120|Images|Files|png|var|imanhua_004_095646468|imanhua_003_095646453|imanhua_005_095646468|imanhua_006_095646484|imanhua_007_095646484|imanhua_002_095646453|18|len|pic|jpg|imanhua_001_095646437|imanhua_008_095646500|imanhua_016_095646531|imanhua_015_095646531|imanhua_017_095646531|sid|imanhua_018_095646546|imanhua_014_095646515|imanhua_010_095646500|imanhua_009_095646500|imanhua_011_095646500|imanhua_013_095646515|imanhua_012_095646515'.split('|'), 0,{ }) );
定义一个匿名函数,同时传入参数,运行返回一个值。紧接着,返回值被eval函数接收,并运行。把上述代码抠出来,放到firefox的firebug控制台运行:
var s = function(p,a,c,k,e,d){ e=function(c){ return(c<a?"":e(parseInt(c/a))) + ((c=c%a)>35?String.fromCharCode(c+29):c.toString(36)) }; if(!''.replace(/^/,String)){ while(c--)d[e(c)]=k[c]||e(c); k=[function(e){return d[e]}]; e=function(){return'\\w+'};c=1;}; while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]); return p; } ( '5 d=c;5 e=["/3/2/1/0/g.f","/3/2/1/0/b.4","/3/2/1/0/7.4","/3/2/1/0/6.4","/3/2/1/0/8.4","/3/2/1/0/9.4","/3/2/1/0/a.4","/3/2/1/0/h.4","/3/2/1/0/p.4","/3/2/1/0/o.4","/3/2/1/0/q.4","/3/2/1/0/s.4","/3/2/1/0/r.4","/3/2/1/0/n.4","/3/2/1/0/j.4","/3/2/1/0/i.4","/3/2/1/0/k.4","/3/2/1/0/m.4"];5 l=0;',29,29,'50567|120|Images|Files|png|var|imanhua_004_095646468|imanhua_003_095646453|imanhua_005_095646468|imanhua_006_095646484|imanhua_007_095646484|imanhua_002_095646453|18|len|pic|jpg|imanhua_001_095646437|imanhua_008_095646500|imanhua_016_095646531|imanhua_015_095646531|imanhua_017_095646531|sid|imanhua_018_095646546|imanhua_014_095646515|imanhua_010_095646500|imanhua_009_095646500|imanhua_011_095646500|imanhua_013_095646515|imanhua_012_095646515'.split('|'),0,{} ); alert(s);
可以看到,匿名函数返回值为:
var len=18;var pic=["/Files/Images/120/50567/imanhua_001_095646437.jpg","/Files/Images/120/50567/imanhua_002_095646453.png","/Files/Images/120/50567/imanhua_003_095646453.png","/Files/Images/120/50567/imanhua_004_095646468.png","/Files/Images/120/50567/imanhua_005_095646468.png","/Files/Images/120/50567/imanhua_006_095646484.png","/Files/Images/120/50567/imanhua_007_095646484.png","/Files/Images/120/50567/imanhua_008_095646500.png","/Files/Images/120/50567/imanhua_009_095646500.png","/Files/Images/120/50567/imanhua_010_095646500.png","/Files/Images/120/50567/imanhua_011_095646500.png","/Files/Images/120/50567/imanhua_012_095646515.png","/Files/Images/120/50567/imanhua_013_095646515.png","/Files/Images/120/50567/imanhua_014_095646515.png","/Files/Images/120/50567/imanhua_015_095646531.png","/Files/Images/120/50567/imanhua_016_095646531.png","/Files/Images/120/50567/imanhua_017_095646531.png","/Files/Images/120/50567/imanhua_018_095646546.png"];var sid=50567;
三条js语句!
nodejs
------------
安装nodejs成功后,编辑test.js,写入如下代码:
var s = function(p,a,c,k,e,d){ e=function(c){ return(c<a?"":e(parseInt(c/a))) + ((c=c%a)>35?String.fromCharCode(c+29):c.toString(36)) }; if(!''.replace(/^/,String)){ while(c--)d[e(c)]=k[c]||e(c); k=[function(e){return d[e]}]; e=function(){return'\\w+'};c=1;}; while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]); return p; } ( '5 d=c;5 e=["/3/2/1/0/g.f","/3/2/1/0/b.4","/3/2/1/0/7.4","/3/2/1/0/6.4","/3/2/1/0/8.4","/3/2/1/0/9.4","/3/2 /1/0/a.4","/3/2/1/0/h.4","/3/2/1/0/p.4","/3/2/1/0/o.4","/3/2/1/0/q.4","/3/2/1/0/s.4","/3/2/1/0/r.4","/3/2/1/0/n.4","/3/2/1/0/j.4"," /3/2/1/0/i.4","/3/2/1/0/k.4","/3/2/1/0/m.4"];5 l=0;',29,29,'50567|120|Images|Files|png|var|imanhua_004_095646468|imanhua_003_095646 453|imanhua_005_095646468|imanhua_006_095646484|imanhua_007_095646484|imanhua_002_095646453|18|len|pic|jpg|imanhua_001_095646437|im anhua_008_095646500|imanhua_016_095646531|imanhua_015_095646531|imanhua_017_095646531|sid|imanhua_018_095646546|imanhua_014_0956465 15|imanhua_010_095646500|imanhua_009_095646500|imanhua_011_095646500|imanhua_013_095646515|imanhua_012_095646515'.split('|'),0,{} ); console.log(s);
在shell里,输入:
node test.js
输出上述值。
畅想
------
php正则获取上述代码,在服务器端生成test.js,那么,用PHP提供的函数exec获取路径地址,借助json_decode函数,就可以搞定了。
《学习服务器端的JavaScript_Node.js》忠实地记录了如何写一个twitter客户端程序,是非常好的howto~