手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:vps

微信菜单的实现代码

 不多说,上代码。有几个要注意的(这段代码是我从我的系统里剥出来的。写了很久了,只是最近有人一直在问,我想,这也不是什么特别的代码,还是开源吧。)

1、请求微信菜单需要token,所以要事先生成。这个token其实可以用不少时间的,看官方接口(token调用生成,每天是有次数的,所以节约着用,能存本地就存本地吧,记录下过期时间即可)
2、子菜单的数组顺序就是微信的子菜单:从上至下的顺序 。这个不要搞错
3、子菜单是有数量限制的
4、主菜单是有字数限制的。不能超过4个中文字。
 
上代码吧:
PHP代码
  1. <?php  
  2. /** 
  3.  * @category menu.php 
  4.  * @author   gouki <gouki.xiao@gmail.com> 
  5.  * @created  2013-10-16 15:14 
  6.  * @since 
  7.  */  
  8. $menudata = array(  
  9.     'button' => array(  
  10.         array(  
  11.             'name'       => '菜单一',  
  12.             'sub_button' => array(  
  13.                 array(  
  14.                     'type' => 'click',  
  15.                     'name' => '最上方子菜单1',  
  16.                     'key'  => 'R:4:1',  
  17.                 ),  
  18.                 array(  
  19.                     'type' => 'click',  
  20.                     'name' => '下方菜单',  
  21.                     'key'  => 'R:3:1',  
  22.                 ),  
  23.             )  
  24.         ),  
  25.         array(  
  26.             'name'       => '菜单2',  
  27.             'sub_button' => array(  
  28.                 array(  
  29.                     'type' => 'click',  
  30.                     'name' => '第一个',  
  31.                     'key'  => 'R:2:1',  
  32.                 ),  
  33.                 array(  
  34.                     'type' => 'click',  
  35.                     'name' => '第二个',  
  36.                     'key'  => 'R:8:1',  
  37.                 ),  
  38.                 array(  
  39.                     'type' => 'click',  
  40.                     'name' => '第三个',  
  41.                     'key'  => 'R:9:1',  
  42.                 ),  
  43.                 array(  
  44.                     'type' => 'click',  
  45.                     'name' => '第四个',  
  46.                     'key'  => 'R:10:1',  
  47.                 ),  
  48.             )  
  49.         ),  
  50.         array(  
  51.             'type' => 'click',  
  52.             'name' => '菜单3',  
  53.             'key'  => 'M:7',  
  54.         ),  
  55.     )  
  56. );  
  57. $menujson = json_encode($menudata, JSON_UNESCAPED_UNICODE);  
  58. define('APPKEY'''); //这里是APPKEY  
  59. define('SECRET'''); //SECRET ....  
  60. $getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";  
  61. $getTokenUrl = sprintf($getTokenUrl, APPKEY, SECRET);  
  62. $result = json_decode(file_get_contents($getTokenUrl), true);  
  63. $token = '';  
  64. if (isset($result['access_token'])) {  
  65.     $token = $result['access_token'];  
  66. }  
  67. if (!$token) {  
  68.     throw new Exception('token can not empty');  
  69. }  
  70.   
  71. $createMenuUrl = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $token;  
  72. $opts = array(  
  73.     'http' => array(  
  74.         'method'  => 'POST',  
  75.         'header'  => "Content-type: application/x-www-form-urlencodedContent-Length: " . strlen($menujson) . "" . "Connection: keep-alive" . "Keep-Alive: " . 300 . "",  
  76.         'content' => $menujson,  
  77.         'timeout' => 120,  
  78.     ),  
  79. );  
  80.   
  81. $context = stream_context_create($opts);  
  82. $response = json_decode(file_get_contents($createMenuUrl, false, $context),true);  
  83.   
  84. if(!$response['errcode'] ){  
  85.     echo "success";  
  86. }else{  
  87.     echo $response['errmsg'];  
  88. }  
  89.   
  90. echo "<pre>";  
  91. print_r($response);  
  92. echo "</pre>";  
记得。如果返回成功,你又看不到效果,可以将公众号先取消关注,再加为关注,立刻就能看到效果了。
否则,你要等24小时左右 才能看到效果。
 
顺便,我也可以承接微信开发(请不要咨询我微信开发怎么做,网上教程很多。。。)

Yii自定义控制器

如果你的项目中有控制器需要共用,其实只需要在config/main.php中加入一小段代码即可实现:

XML/HTML代码
  1. 'controllerMap'=>array(  
  2.     'xxx'=>'ext.XxxController'  
  3. ),  

然后就可以直接调用了:index.php?r=xxx/index,即可以调用XxxController类中的activeIndex方法

这种适合在什么情况下呢?比如统一的出错处理类,这个就相对会比较方便,因为这样的代码会几乎一样。还有那种工具控制器,比如将数据导出成xls,可以写一个XlsController来处理它。

这样的控制器,一般来说适合用于不经权限控制的情况。

IFTTT

刚才在随便看看的时候,发现有人感慨。其实在互联网上的ifttt,在编程也是这样。
试想从第一行代码开始到编码结束,本身就是在执行着不同的ifttt,无非只不过ifttt的环境是在本地,而不是互联网应用了。
然后此人就想,如果我将每一个ifttt进行了封装处理。那么,软件就会象积木一样,一点点的堆起来。然后剩下的问题就是从第一个ifttt开始,怎么样将每一个结果和条件向下传递。

说了半天,ifttt究竟是什么?IFTTT的全称为:IF This Then That。很容易理解。
而且网上还有人举了个小例子:

http://www.shaduruanjian8.com/20110630_ifttt
  1. 它事实上就是我们很熟悉的MUD时代就有的Trigger,WOW里面也有,在Office里面叫Macro(当然,这个就复杂了)。简单说,就是把你的整个网络社交圈当作一个巨大的触发器,一旦特定的事件发生,就会触发IFTTT中设置好的Task的Trigger。而一旦Trigger被处罚,IFTTT就会相应地作出Action。  
  2. 比如说:只要在Twitter上有人Mention我,就给我发一条SMS(手机短信)。  
  3. 这个在IFTTT中就表现为:Trigger设置为Twitter的Mention Me,Action设置为SMS。结果就是如果有人在Twitter上Mention了你,你就会收到一条手机短信,内容为“你被@了。”  
  4. 是不是很有意思?还有更有意思的:  
  5. Task1:如果老婆的Twitter上出现“加班了”三个字,就给我的邮箱发一封Email,标题为“你懂的”;  
  6. Task2:如果我收到一份标题为“你懂的”的Email,就自动向EmailList中“吃喝委员会”一栏的所有人发一份EMail,标题为:老地方搓一顿;  
  7. Task3:如果我收到一份标题为“你懂的”的Email,就自动向黄浦江大酒店发一份EMail,标题为“预订”,内容为“今天晚上7点,4人桌,老菜式,你懂的。”  
  8. Task4:如果晚上我的Twitter没有被Mention,自动发送短信到老婆的手机,内容为:今晚加班,安。  
  9. 于是,结果就是,如果你老婆在Twitter上发言说“今天要加班了,真倒霉!”那你自动收到一份Email,然后自动发出一份订餐Email,一系列的邀请聚餐Email,然后晚上你来黄浦江大酒店,饭菜桌位都已经准备齐全了,一帮借口晚上加班的朋友们嬉皮笑脸地过来和你一顿胡吃猛喝,多惬意啊。然后,如果你和朋友们喝多了,没能在自己的Twitter上用一个小号Mention自己一下,IFTTT就会自动发短信给老婆说你其实是在加班,不是在和朋友们乱喝酒。  
  10. 多么自动化智能化。  
  11. 绝对是以后老婆管老公的首选利器啊!  
  12. 当然,你也可以建立如下Task:  
  13. IF:凌晨04:01,Then:偷菜。(假定偷菜开放时间是凌晨四点)  
  14. 所以,IFTTT是偷菜利器,神码半夜起床,都是浮云……  

ifttt上其实有网站,上面的创意其实就是根据 ifttt的网站上的内容来实现的。不过我在想,大量的trigger,对系统的性能影响怎么样?
那么我开始说的那件事情,你觉得能成吗?将代码写成积木式,不停的堆?我是觉得玄,咱先不谈效率,效率在强大的CPU下面都暂时可以忽略。真的可以将所有的ifttt都能写出来吗?依赖环境怎么办?
我还是随便扯扯吧。不过,简单的还是可以考虑一下下的

Tags: ifttt

apns 二三事

今天要說的還是推送,NND,這個推送這玩意,折騰死人
1、aps中是否只能有badge/alert/sound等參數?
答:不是,這個只是最基本的參數,可以有很多參數,但aps標記需要一定存在,其他參數可以自定義,比如:

JavaScript代码
  1. {  
  2.     "aps" : {  
  3.     },  
  4.     "acme2" : [ 5,  8 ]  
  5. }  

aps参数可以是空,但一定要有,上面这个可不是我杜撰的,这是官方的例子:http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW12

2、aps中的content-available是什么意思
答:这个是有特殊需要的时候才需要用,比如用来后台下载?说实话,我在官方没找到文档,反而在adobe找到一个小的介绍 :
第一个介绍:http://www.adobe.com/devnet/digitalpublishingsuite/articles/dps-push-notification.html

Table 3. Parameters for APNS messages

Property value description
alert <string> The text message to display to the user. No localization arguments are supported.
badge <number> If non-zero, it displays the number on the icon of the application. If zero, no number is displayed.
content-available 0 | 1 Non-zero indicates a Newsstand push message. The receipt of this will wake the viewer to begin a background download. See Notes.
sound "default" Only the default notification sound is supported.
productID <string> Product.

Notes about Newsstand background downloading

  1. Background downloads are limited to once every 24 hours, +/- 1 hour (or so). You can send as many alert messages you like, but only background downloading will be throttled.
  2. Background downloading will only happen over WiFi connections. If the user is on 3G and the push is received, the background download is lost.
  3. A valid subscription via Apple or direct entitlement (CDS, TCS, PCD, Dovetail, or other) must exist.
  4. Only the most recent folio will be background-downloaded. The user must be entitled to this content.
  5. Apple attempts a number of times to deliver the push payload. As such, the actual download may begin a significant amount of time after it was originally sent or scheduled.

用了content-available参数后,下一次接收一定是在24小时后。看上面的notes

还有一个介绍在这里:http://www.viggiosoft.com/blog/blog/2011/10/17/ios-newsstand-tutorial/,这篇博客很详细,还告诉你在APP中怎么设置,内容过长,我就不一一贴出来了。
它这么说:
The push notification message has the same syntax as standard push notifications with the only difference that the payload must contain the content-available key set to 1:

JavaScript代码
  1. {  
  2.   "aps":{  
  3.     "content-available":1,  
  4.         },  
  5.    "device_tokens": ["E9623F5CFDE92B40DA4AA90B97B70428BCD8FFCBA067BE17A6EF5102651E66E9"]  
  6. }  

There is one importation limitation related to the frequency of pushes: to limit the consumption of resources due to background downloading (which, by the way, we’ll happen only if the device is connected to a Wi-Fi network) Newsstand push notifications are coalesced and only one background download is permitted per day. For testing purposes you can remove this limit by setting an appropriate key in the user defaults (do this at startup in your App Delegate)
大小: 96.37 K
尺寸: 251 x 376
浏览: 1993 次
点击打开新窗口浏览全图

Tags: apns, apple

让CRONTAB精确到秒的执行任务

众所周知,crontab的最小粒度是分,即当第一位是“*/1”时,即最小单位是每分钟执行。
但是,有时候我们要按秒执行,当然这是比较夸张的了,但每半分钟执行一次还是有可能的,这时候就没有办法了,所以后 来 找啊找,就找到了一篇说明:

XML/HTML代码
  1. */1 * * * * sh /root/cron.sh  
  2. */1 * * * * sleep 30 && sh /root/cron.sh  

执行的时候先sleep 30秒,变相的达到每半分钟执行此命令,果然很变态

windows下面也有模拟crontab的软件,如crontabs,pycrontab等,都是注册成一个服务,然后再执行,但是没有一个软件是比较好用的。总有小问题。

Tags: crontab

Records:812