Submitted by gouki on 2012, April 17, 9:11 AM
关于苹果的推送服务,网上的相关资料非常多,最简单的就是组合成一个数组,类似:
XML/HTML代码
- $arr['aps'] = array(
- 'badge'=>1,
- 'sound'=>'',
- 'alert'=>'xxx'
- );
然后用json_encode处理一下之后,用ssl的方式发送给苹果:
XML/HTML代码
- chr(0) . pack("n", 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n", strlen($payload)) . $payload;
$devideToken是设备的token,$payload就是上述的数组json_encode之后的数据。
测试的话是发给:ssl://gateway.sandbox.push.apple.com:2195,正式的话,将sandbox去掉即OK
于是这代码就好写了:
PHP代码
-
-
-
-
-
-
-
-
-
- class ApnsService {
- public $token;
- public $message;
- public $badge = 1;
- public $sound;
- public $ispad;
- public function __construct($token, $message, $badge = null, $sound = null, $ispad = 0) {
- $this->token = $token;
- $this->message = $message;
- $this->badge = $badge;
- $this->sound = $sound;
- $this->ispad = (int)$ispad;
- }
- public function send() {
- return $this->sendPushInfo();
- }
- public function sandboxSend() {
- return $this->sendPushInfo(true);
- }
- private function getPayload() {
- $body = array();
- if ($this->badge) {
- $body['aps']['badge'] = $this->badge;
- }
- if ($this->sound) {
- $body['aps']['sound'] = $this->sound;
- }
- $body['aps']['alert'] = $this->message;
- return $body;
- }
- private function getCertFile() {
- return Yii::getPathOfAlias("application") . ($this->ispad ? "/hdDis.pem" : "/iphoneDis.pem");
- }
- private function getSandboxCertFile() {
- return Yii::getPathOfAlias("application") . ($this->ispad ? "/hdDev.pem" : "/iphoneDev.pem");
- }
- private function getApplePushUrl($isSandbox = false) {
- return ($isSandbox == true ? "ssl://gateway.push.apple.com:2195" : "ssl://gateway.sandbox.push.apple.com:2195");
- }
- private function sendPushInfo($isSandbox = false) {
- $ctx = stream_context_create();
- stream_context_set_option($ctx, 'ssl', 'local_cert',
- ($isSandbox == true
- ? $this->getSandboxCertFile()
- : $this->getCertFile())
- );
-
- $fp = stream_socket_client($this->getApplePushUrl($isSandbox), $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
- if (!$fp) {
- print "Failed to connect $err $errstr\n";
- return false;
- } else {
-
- }
- $payload = json_encode($this->getPayload());
-
- $msg = chr(0) . pack("n", 32) . pack('H*', str_replace(' ', '', $this->token)) . pack("n", strlen($payload)) . $payload;
- fwrite($fp, $msg);
- fclose($fp);
- return true;
- }
- }
上面的代码非常简单只是作了一个简单的处理和封装。不过有部分路径是基于yii的,所以要改一下就OK了。
主要是自己的记录。
Tags: 推送, badge, json
苹果相关 | 评论:0
| 阅读:20563
Submitted by gouki on 2012, April 16, 1:24 PM
freetds连接sqlserver是没问题,在连接的时候dsn用的是dblib,而不是mssql。
连接和普通的查询都没有问题,但一旦涉及到大字段,你就会发现问题多多了。比如你的字段类型是nvarchar(max),你在处理的时候你会发现,返回结果都是空。
我晶啊。看来php连接sqlserver还是用windows自己的方案吧:sqlsrv这个组件,但它只能在windows下面使用。
太纠结了
Tags: freetds, sqlserver, 大字段
PHP | 评论:1
| 阅读:16145
Submitted by gouki on 2012, April 16, 8:54 AM
1、yii在linux下面连接mssql时,connectString一般是dblib:xxxxx,而不是mssql,这是因为用freetds的原因
2、用gii生成mssql的数据表结构时,如果表的字段是类似"Post Time"(即,字段中间有空格),则直接会报错
3、当生成好Model时,使用xxx::model()->findAll()时,如果最初的表结构里有允许某个字段为空,即允许isnull的话,在数据库中,该字段无值时是NULL,findAll就会报错:
XML/HTML代码
- CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 4004 General SQL Server error: Check messages from the SQL Server [4004] (severity 16) [(null)]. The SQL statement executed was: SELECT TOP 1 * FROM [dbo].[DatabaseLog] [t]
4、无论是mysql还是sqlserver,当在components中设置columnCase值的时候,都会报错,这是因为在 MysqlSchema中都是采用了大小写敏感的方式(如$column['IsIdenfy'])之类的,当大小写敏感后,如果在attributes 中设置了PDO的ATTR_CASE,都会报错
Tags: yii, mssql
PHP Framework | 评论:0
| 阅读:19060
Submitted by gouki on 2012, April 15, 10:57 AM
其实本不想说什么,但是觉得很意外,所以我还是说说了。
一两句而已:
经过某便利店买了瓶啤酒,在收银的时候,一个大妈笑着对我说,送你一个上网软件,扫了一下光盘上的字,立刻就明白了是什么样的软件(XX门,你懂的)。于是我说不需要,我也不想举报你啥的。大妈说,人总要听听外面的声音才是。
我震精了。
---------
现在某些一块、5块的纸币上面多了一些印刷体,委实让人痛苦,扔了吧,可惜,不扔吧,又要传播。TMD,这算是一个什么样的世道??或许你真的有理,但你这样做,就真的没理。
---------
牢骚发完了,继续折腾
Misc | 评论:2
| 阅读:14459
Submitted by gouki on 2012, April 14, 8:37 PM
yii的urlmanager可以让项目在访问的时候隐藏index.php,也可以以更优雅的urlrewrite方式来显示,但这一切需要一些配置,在apache上的配置上就相对比较简单,直接参考wordpress的官方配置就完了,但其实很久以来,一直都没有人写过nginx下的配置。大家都是在根据wordpress配置来更改的,比如lnmp项目中,就是:
XML/HTML代码
- location / {
- if (-f $request_filename/index.html){
- rewrite (.*) $1/index.html break;
- }
- if (-f $request_filename/index.php){
- rewrite (.*) $1/index.php;
- }
- if (!-f $request_filename){
- rewrite (.*) /index.php;
- }
- }
不过,这两天在看官方guide的文档,原来这些问题,官方已经提供方案了:http://yii.neatcn.com/doc/guide/1.1/en/quickstart.apache-nginx-config#nginx
1. Apache
Yii is ready to work with a default Apache web server configuration. The .htaccess files in Yii framework and application folders restrict access to the restricted resources. To hide the bootstrap file (usually index.php) in your URLs you can add mod_rewrite instructons to the .htaccess file in your document root or to the virtual host configuration:
XML/HTML代码
- RewriteEngine on
-
- # if a directory or a file exists, use it directly
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteCond %{REQUEST_FILENAME} !-d
- # otherwise forward it to index.php
- RewriteRule . index.php
2. Nginx
You can use Yii with Nginx and PHP with FPM SAPI. Here is a sample host configuration. It defines the bootstrap file and makes yii catch all requests to unexisting files, which allows us to have nice-looking URLs.
XML/HTML代码
- server {
- set $host_path "/www/mysite";
- access_log /www/mysite/log/access.log main;
-
- server_name mysite;
- root $host_path/htdocs;
- set $yii_bootstrap "index.php";
-
- charset utf-8;
-
- location / {
- index index.html $yii_bootstrap;
- try_files $uri $uri/ $yii_bootstrap?$args;
- }
-
- location ~ ^/(protected|framework|themes/\w+/views) {
- deny all;
- }
-
- #avoid processing of calls to unexisting static files by yii
- location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
- try_files $uri =404;
- }
-
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- location ~ \.php {
- fastcgi_split_path_info ^(.+\.php)(.*)$;
-
- #let yii catch the calls to unexising PHP files
- set $fsn /$yii_bootstrap;
- if (-f $document_root$fastcgi_script_name){
- set $fsn $fastcgi_script_name;
- }
-
- fastcgi_pass 127.0.0.1:9000;
- include fastcgi_params;
- fastcgi_param SCRIPT_FILENAME $document_root$fsn;
-
- #PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
- fastcgi_param PATH_INFO $fastcgi_path_info;
- fastcgi_param PATH_TRANSLATED $document_root$fsn;
- }
-
- location ~ /\.ht {
- deny all;
- }
- }
Using this configuration you can set cgi.fix_pathinfo=0 in php.ini to avoid many unnesessary system stat() calls.
Tags: yii, apache, nginx, wordpress, urlrewrite
PHP Framework | 评论:0
| 阅读:22805