PHP的相关内容分享
代码如下,测试过可行~ 中文会被忽略,不能纯中文
参考阅读:
1 数据源见前面说明
2 另外可以参考更多内容:
http://posttopic.com/topic/google-voice-add-on-development
3 参考openID:http://code.google.com/intl/zh-CN/apis/accounts/docs/OpenID.html
<?PHP
/*
Google Voice Api
License :部分代码来自http://www.lostleon.com 增加了错误提示,返回信息,群发,中文转拼音 所需PHP扩展:curl
Author:threesky@gmail.com, http://www.heqee.com/
*/
class GoogleVoice
{
public $username;
public $password;
public $status;
private $lastURL;
private $login_auth;
private $inboxURL = 'https://www.google.com/voice/m/';
private $loginURL = 'https://www.google.com/accounts/ClientLogin';
private $smsURL = 'https://www.google.com/voice/m/sendsms';
public function __construct($username, $password)
{
$this->username = $username;
$this->password = $password;
}
public function getLoginAuth()
{
$login_param = "accountType=GOOGLE&Email={$this->username}&Passwd={$this->password}&service=grandcentral&source=lostleonATgmailDOTcom-GoogleVoiceTool";
$login_output = $this->curl($this->loginURL, $this->lastURL, $login_param);
$this->login_auth = $this->match('/Auth=([A-z0-9_-]+)/', $login_output, 1);
return $this->login_auth;
}
public function get_rnr_se()
{
$auth_param = "?auth=".$this->getLoginAuth();
$inbox_output = $this->curl($this->inboxURL.$auth_param, $this->lastURL);
$_rnr_se = $this->match('!<input.*?name="_rnr_se".*?value="(.*?)"!ms', $inbox_output, 1);
return $_rnr_se;
}
public function sms($to_phonenumber, $smstxt)
{
$_rnr_se = $this->get_rnr_se();
$sms_param = "id=&c=&number=".urlencode($to_phonenumber)."&smstext=".urlencode($smstxt)."&_rnr_se=".urlencode($_rnr_se);
$posturl = $this->smsURL."?auth=".$this->login_auth;
$sms_output = $this->curl($posturl, $this->lastURL, $sms_param);
$this->status = $sms_output;
return $sms_output;
}
private function curl($url, $referer = null, $post_param = null)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20");
if($referer)
curl_setopt($ch, CURLOPT_REFERER, $referer);
if(!is_null($post_param))
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_param);
}
$html = curl_exec($ch);
$this->lastURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
return $html;
}
private function match($regex, $str, $out_ary = 0)
{
return preg_match($regex, $str, $match) == 1 ? $match[$out_ary] : false;
}
}
$voiceApi = new GoogleVoice('cevin1991@gmail.com','*******');
echo $voiceApi->sms('8615022604794','testsadfasfasfa');
/*
Google Voice Api
License :部分代码来自http://www.lostleon.com 增加了错误提示,返回信息,群发,中文转拼音 所需PHP扩展:curl
Author:threesky@gmail.com, http://www.heqee.com/
*/
class GoogleVoice
{
public $username;
public $password;
public $status;
private $lastURL;
private $login_auth;
private $inboxURL = 'https://www.google.com/voice/m/';
private $loginURL = 'https://www.google.com/accounts/ClientLogin';
private $smsURL = 'https://www.google.com/voice/m/sendsms';
public function __construct($username, $password)
{
$this->username = $username;
$this->password = $password;
}
public function getLoginAuth()
{
$login_param = "accountType=GOOGLE&Email={$this->username}&Passwd={$this->password}&service=grandcentral&source=lostleonATgmailDOTcom-GoogleVoiceTool";
$login_output = $this->curl($this->loginURL, $this->lastURL, $login_param);
$this->login_auth = $this->match('/Auth=([A-z0-9_-]+)/', $login_output, 1);
return $this->login_auth;
}
public function get_rnr_se()
{
$auth_param = "?auth=".$this->getLoginAuth();
$inbox_output = $this->curl($this->inboxURL.$auth_param, $this->lastURL);
$_rnr_se = $this->match('!<input.*?name="_rnr_se".*?value="(.*?)"!ms', $inbox_output, 1);
return $_rnr_se;
}
public function sms($to_phonenumber, $smstxt)
{
$_rnr_se = $this->get_rnr_se();
$sms_param = "id=&c=&number=".urlencode($to_phonenumber)."&smstext=".urlencode($smstxt)."&_rnr_se=".urlencode($_rnr_se);
$posturl = $this->smsURL."?auth=".$this->login_auth;
$sms_output = $this->curl($posturl, $this->lastURL, $sms_param);
$this->status = $sms_output;
return $sms_output;
}
private function curl($url, $referer = null, $post_param = null)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20");
if($referer)
curl_setopt($ch, CURLOPT_REFERER, $referer);
if(!is_null($post_param))
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_param);
}
$html = curl_exec($ch);
$this->lastURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
return $html;
}
private function match($regex, $str, $out_ary = 0)
{
return preg_match($regex, $str, $match) == 1 ? $match[$out_ary] : false;
}
}
$voiceApi = new GoogleVoice('cevin1991@gmail.com','*******');
echo $voiceApi->sms('8615022604794','testsadfasfasfa');
参考阅读:
1 数据源见前面说明
2 另外可以参考更多内容:
http://posttopic.com/topic/google-voice-add-on-development
3 参考openID:http://code.google.com/intl/zh-CN/apis/accounts/docs/OpenID.html
点击这里查看webshell代码
webshell功能强大,学习的好代码,也提醒做运维安全的要有好的办法做预防啊!
预防措施我觉得有几点:
1 文件上传的限制
2 对php文件的检查
3 对于运行php进行路径限制
4 对有危险的php用单独用户运行
第一点是对于恶意的php代码而言的,有些代码可能是你从别人那里复制过来的,做的不好,安全性不够,导致php文件上传到web目录。第二点对于ftp上传,或者php文件被修改的。第三点防止文件上传的,可以不执行文件上传路径的php。第四点是使用另外的用户运行,防止恶意修改别人的文件。
顺便说一下,pastebin网站的分享很有趣,我觉得蛮好的,可以随便贴点代码分享,功能比较的简洁实在。
webshell功能强大,学习的好代码,也提醒做运维安全的要有好的办法做预防啊!
预防措施我觉得有几点:
1 文件上传的限制
2 对php文件的检查
3 对于运行php进行路径限制
4 对有危险的php用单独用户运行
第一点是对于恶意的php代码而言的,有些代码可能是你从别人那里复制过来的,做的不好,安全性不够,导致php文件上传到web目录。第二点对于ftp上传,或者php文件被修改的。第三点防止文件上传的,可以不执行文件上传路径的php。第四点是使用另外的用户运行,防止恶意修改别人的文件。
顺便说一下,pastebin网站的分享很有趣,我觉得蛮好的,可以随便贴点代码分享,功能比较的简洁实在。
今天发现phpMyadmin3.3版本有两个新功能很实用:
1 Synchronize是支持两个数据库同步的操作,这功能挺好的
2 管理replication
不过,同步操作似乎是经过本地web管理的服务器,如果是远端的数据库同步,可能不太合适了,未确认。除非把管理的web端放在数据库的局域网,那样应该挺快。
复制的管理也蛮好,把平时需要输入的命令行给做成点击几下就可以,很方便,这个实用。
去phpmyadmin官网下载最新版本吧!
点击这里查看官方关于同步功能的介绍
1 Synchronize是支持两个数据库同步的操作,这功能挺好的
2 管理replication
不过,同步操作似乎是经过本地web管理的服务器,如果是远端的数据库同步,可能不太合适了,未确认。除非把管理的web端放在数据库的局域网,那样应该挺快。
复制的管理也蛮好,把平时需要输入的命令行给做成点击几下就可以,很方便,这个实用。
去phpmyadmin官网下载最新版本吧!
点击这里查看官方关于同步功能的介绍
建立两个独立的fastcgi服务会有一些好处:
1 不会因为某个网站进程占用特别多而影响别的网站,因为进程占用满了就没法处理了
2 了解网站的压力
正常情况下,安装完fastcgi后,有如下文件:
我们启动的是 sbin/php-fpm start
看看文件内容:
看的出来,可以定义配置文件的路径,设置pid文件路径
这两个都修改后,就可以了。
做如下修改:
1 复制一个php-fpm为9001(这里方便区分原先的9000端口的服务而已,看自己喜欢)
2 修改php-fpm.9001.conf文件
3 启动即可 sbin/9001 start
1 不会因为某个网站进程占用特别多而影响别的网站,因为进程占用满了就没法处理了
2 了解网站的压力
正常情况下,安装完fastcgi后,有如下文件:
引用
etc/:
pear.conf php-fpm.conf php.ini
sbin/:
php-fpm
pear.conf php-fpm.conf php.ini
sbin/:
php-fpm
我们启动的是 sbin/php-fpm start
看看文件内容:
#! /bin/sh
php_fpm_BIN=/Data/apps/php-cgi/bin/php-cgi
php_fpm_CONF=/Data/apps/php-cgi/etc/php-fpm.conf
php_fpm_PID=/Data/apps/php-cgi/logs/php-fpm.pid
php_opts=""
# i.e. php_opts="--fpm-config $php_fpm_CONF"
wait_for_pid () {
try=0
.......
php_fpm_BIN=/Data/apps/php-cgi/bin/php-cgi
php_fpm_CONF=/Data/apps/php-cgi/etc/php-fpm.conf
php_fpm_PID=/Data/apps/php-cgi/logs/php-fpm.pid
php_opts=""
# i.e. php_opts="--fpm-config $php_fpm_CONF"
wait_for_pid () {
try=0
.......
看的出来,可以定义配置文件的路径,设置pid文件路径
这两个都修改后,就可以了。
做如下修改:
1 复制一个php-fpm为9001(这里方便区分原先的9000端口的服务而已,看自己喜欢)
#! /bin/sh
php_fpm_BIN=/Data/apps/php-cgi/bin/php-cgi
php_fpm_CONF=/Data/apps/php-cgi/etc/php-fpm.9001.conf
php_fpm_PID=/Data/apps/php-cgi/logs/php-fpm.9001.pid
#php_opts=""
php_opts="--fpm-config $php_fpm_CONF"
php_fpm_BIN=/Data/apps/php-cgi/bin/php-cgi
php_fpm_CONF=/Data/apps/php-cgi/etc/php-fpm.9001.conf
php_fpm_PID=/Data/apps/php-cgi/logs/php-fpm.9001.pid
#php_opts=""
php_opts="--fpm-config $php_fpm_CONF"
2 修改php-fpm.9001.conf文件
<value name="pid_file">/Data/apps/php-cgi/logs/php-fpm.9001.pid</value>
<value name="error_log">/Data/apps/php-cgi/logs/php-fpm.9001.log</value>
<value name="listen_address">127.0.0.1:9001</value>
<value name="error_log">/Data/apps/php-cgi/logs/php-fpm.9001.log</value>
<value name="listen_address">127.0.0.1:9001</value>
3 启动即可 sbin/9001 start
客户端需要UTF-16编码的文件,但转换时,同事说不对,要小尾序的,其实不太明白这个意思,查了一下资料。
PHP里的代码也就这样了:
从某篇文章发现这样的东西:
这说明了有其它的UTF-16的编码方式,我们看看维基的说明《UTF-16》:
PHP稍微修改一下就对了:
另外有这么个代码:UTF-8 to Code Point Array Converter in PHP
把字符都转换成代表的数字了,目前不知道什么用途。。
另外,需要转换PC格式为Unix格式时,需要在转换Utf-16之前处理,否则会出现好多乱码的哦
PHP里的代码也就这样了:
$content = iconv('UTF-8', 'UTF-16', $content);
从某篇文章发现这样的东西:
from_to($string, 'UTF-16le', 'UTF-8');
这说明了有其它的UTF-16的编码方式,我们看看维基的说明《UTF-16》:
引用
UTF-16是Unicode的其中一个使用方式。 UTF是 Unicode/UCS Transformation Format,即把Unicode转做某种格式的意思。
UTF-16的大尾序和小尾序储存形式都在用。一般来说,以Macintosh制作或储存的文字使用大尾序格式,以Microsoft或Linux制作或储存的文字使用小尾序格式。
字符“朱”:
UTF-16LE 小尾序 31 67
UTF-16BE 大尾序 67 31
UTF-16的大尾序和小尾序储存形式都在用。一般来说,以Macintosh制作或储存的文字使用大尾序格式,以Microsoft或Linux制作或储存的文字使用小尾序格式。
字符“朱”:
UTF-16LE 小尾序 31 67
UTF-16BE 大尾序 67 31
PHP稍微修改一下就对了:
$content = iconv('UTF-8', 'UTF-16le', $content);
另外有这么个代码:UTF-8 to Code Point Array Converter in PHP
把字符都转换成代表的数字了,目前不知道什么用途。。
另外,需要转换PC格式为Unix格式时,需要在转换Utf-16之前处理,否则会出现好多乱码的哦
$content = str_replace("\r\n", "\n", $content);
$content = iconv('UTF-8', 'UTF-16le', $content);
$content = iconv('UTF-8', 'UTF-16le', $content);
从大的编码转换到小的编码,是会产生字符缩减的,甚至有些字在目标字符集里不存在。
比如gb2312里,“囧”、“镕”都会产生转换失败,但很奇怪,gb2312的网页里是有这样的文字的,有点不解。
先看看官方的说明吧 iconv
看示例可知
在输出字符里添加//TRANSLIT可以得到相近的意思的字符
添加//IGNORE可以忽略不能转换的
不添加,则在第一个错误的地方停止转换,也就是被截断了
如何知道被截断了或者转换失败?
有个例子可以参考一下(单位是字节还是字符,这里不确定)
比如gb2312里,“囧”、“镕”都会产生转换失败,但很奇怪,gb2312的网页里是有这样的文字的,有点不解。
先看看官方的说明吧 iconv
引用
out_charset
The output charset.
If you append the string //TRANSLIT to out_charset transliteration is activated. This means that when a character can't be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character and an E_NOTICE is generated.
The output charset.
If you append the string //TRANSLIT to out_charset transliteration is activated. This means that when a character can't be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character and an E_NOTICE is generated.
看示例可知
在输出字符里添加//TRANSLIT可以得到相近的意思的字符
添加//IGNORE可以忽略不能转换的
不添加,则在第一个错误的地方停止转换,也就是被截断了
如何知道被截断了或者转换失败?
有个例子可以参考一下(单位是字节还是字符,这里不确定)
<?
//code from http://www.aslibra.com
//code by hqlulu @ 2010-1-25
$title_origin = "something";
$title = iconv('utf-8', 'gb2312//IGNORE', $title_origin);
$percent = round(strlen($title_origin)/strlen($title), 3);
//UTF-8汉字 3字节 gb2312汉字 2字节
//最大比例为1.5,如果丢失文字,那就是有转换失败,并且比例变大
//简单例子:“我”的urlencode值 = %E6%88%91(utf-8) = %CE%D2(gb2312)
if($percent > 1.5 ){
$error[] = array('str', $title_origin);
}
?>
//code from http://www.aslibra.com
//code by hqlulu @ 2010-1-25
$title_origin = "something";
$title = iconv('utf-8', 'gb2312//IGNORE', $title_origin);
$percent = round(strlen($title_origin)/strlen($title), 3);
//UTF-8汉字 3字节 gb2312汉字 2字节
//最大比例为1.5,如果丢失文字,那就是有转换失败,并且比例变大
//简单例子:“我”的urlencode值 = %E6%88%91(utf-8) = %CE%D2(gb2312)
if($percent > 1.5 ){
$error[] = array('str', $title_origin);
}
?>
nginx配合codeIgniter有点麻烦,没有apache和lighttpd那么简单。
在apache和lighttpd里面,判断 /index.php/sth 时,如果 /index.php是一个文件,就不会当做是文件路径了
我们可以这样简单的把某些路径rewrite,达到一样的效果:
1 保持地址格式是 /index.php/control/model
2 不产生404错误或者是no input的情况
我们还需要避免类似以下地址异常:
/book/?test 404 查询字符会查找test的control
/book/&test 错误 The URI you submitted has disallowed characters.
总体而言,是查询字符的问题,我们可以有两种方式处理:
方式一:在codeIgniter处理之前避免
我们定义系统使用REQUEST_URI来处理mvc参数,不修改系统的文件,我们修改自定义的文件
文件 application/config/config.php
指定为REQUEST_URI方式处理,不是用auto,auto方式会判断查询字符
文件 index.php
我们可以直接砍掉查询字符,保证参数不受影响
方式二:发生错误之后调整
发生404错误时,我们自动跳转到相应的地址,比如去掉后面 ?xxxx 和 &xxxx
文件 application/errors/error_404.php
在 error_general.php 里也可以这样加一样的代码。
参考资料:
1 不错的nginx配置方式,本文有参考: CodeIgniter Nginx Rewrite Rule
2 CodeIgniter URLs & Nginx
在apache和lighttpd里面,判断 /index.php/sth 时,如果 /index.php是一个文件,就不会当做是文件路径了
我们可以这样简单的把某些路径rewrite,达到一样的效果:
1 保持地址格式是 /index.php/control/model
2 不产生404错误或者是no input的情况
server {
index index.php index.htm;
server_name www.aslibra.com;
root /Data/webapps/www.aslibra.com;
location /book/ {
if (!-e $request_filename) {
rewrite ^/book/(.*)$ /book/index.php/$1 last;
}
}
location /book/index.php {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /Data/webapps/www.aslibra.com/book/index.php;
}
}
index index.php index.htm;
server_name www.aslibra.com;
root /Data/webapps/www.aslibra.com;
location /book/ {
if (!-e $request_filename) {
rewrite ^/book/(.*)$ /book/index.php/$1 last;
}
}
location /book/index.php {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /Data/webapps/www.aslibra.com/book/index.php;
}
}
我们还需要避免类似以下地址异常:
/book/?test 404 查询字符会查找test的control
/book/&test 错误 The URI you submitted has disallowed characters.
总体而言,是查询字符的问题,我们可以有两种方式处理:
方式一:在codeIgniter处理之前避免
我们定义系统使用REQUEST_URI来处理mvc参数,不修改系统的文件,我们修改自定义的文件
文件 application/config/config.php
指定为REQUEST_URI方式处理,不是用auto,auto方式会判断查询字符
| 'AUTO' Default - auto detects
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol'] = "REQUEST_URI";
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol'] = "REQUEST_URI";
文件 index.php
我们可以直接砍掉查询字符,保证参数不受影响
//处理查询字符
$url = explode('?', $_SERVER['REQUEST_URI']);
$url = explode('&', $url[0]);
$_SERVER['REQUEST_URI'] = $url[0];
/*
|---------------------------------------------------------------
| LOAD THE FRONT CONTROLLER
|---------------------------------------------------------------
|
| And away we go...
|
*/
require_once BASEPATH.'codeigniter/CodeIgniter'.EXT;
$url = explode('?', $_SERVER['REQUEST_URI']);
$url = explode('&', $url[0]);
$_SERVER['REQUEST_URI'] = $url[0];
/*
|---------------------------------------------------------------
| LOAD THE FRONT CONTROLLER
|---------------------------------------------------------------
|
| And away we go...
|
*/
require_once BASEPATH.'codeigniter/CodeIgniter'.EXT;
方式二:发生错误之后调整
发生404错误时,我们自动跳转到相应的地址,比如去掉后面 ?xxxx 和 &xxxx
文件 application/errors/error_404.php
<?php
$url = explode('?', $_SERVER['REQUEST_URI']);
$url = explode('&', $url[0]);
if($url[0] != $_SERVER['REQUEST_URI']){
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url[0]\">";
}
?>
</head>
$url = explode('?', $_SERVER['REQUEST_URI']);
$url = explode('&', $url[0]);
if($url[0] != $_SERVER['REQUEST_URI']){
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url[0]\">";
}
?>
</head>
在 error_general.php 里也可以这样加一样的代码。
参考资料:
1 不错的nginx配置方式,本文有参考: CodeIgniter Nginx Rewrite Rule
2 CodeIgniter URLs & Nginx




