2009年01月10日 存档

用rawurldecode和iconv解决php的编码问题

2009年01月10日,星期六

我在给wordpress加上记录Combined格式的时候,也加了个从referer里获取搜索关键字的功能(参考javaeye的“您正在搜索”加关键字高亮的功能)。
由于google的url里是标准的UTF-8编码,而百度的url里,一般却是GBK编码的。
当时实现这个功能的时候,导致rawurldecode只对google 的url得到了正确的汉字。
今天在网上重新搜索了相关信息,发现其实是有办法解决的。
解决的方法很简单,代码如下:
[code]
$lzb_searchkeyword=rawurldecode($lzb_searchkeywordstr);
$lzb_searchkeyword=iconv("GBK","UTF-8",$lzb_searchkeyword) ;//
通过这一行就把百度来的GBK汉字转成了UTF-8的,在页面上就可以正常显示了。
$lzb_searchurl='http://www.baidu.com/s?wd='.$lzb_searchkeywordstr;
[/code]

以后文章正文下方的recent related 1 searches的地方,基本不会再出现乱码了。

Tags: iconv, php, rawurldecode, 乱码

Related posts

基于apache mina 2.0.0 M4和google Protocol Buffers 2.0.3的java RPC实例(1.准备工作)

2009年01月10日,星期六

基于apache mina 2.0.0 M4和google Protocol Buffers 2.0.3的java RPC实例(1.准备工作)

1.最新的google Protocol Buffers 下载地址为:

http://protobuf.googlecode.com/files/protobuf-2.0.3.zip
http://protobuf.googlecode.com/files/protoc-2.0.3-win32.zip
来源:http://code.google.com/p/protobuf/downloads/list

2.Apache MINA 2.0.0-M4下载地址为:
http://apache.freelamp.com/mina/2.0.0-M4/mina-2.0.0-M4.zip
(或:http://www.eu.apache.org/dist/mina/2.0.0-M4/mina-2.0.0-M4.zip)

3.protobuf-rpc的代码:
通过svn取protobuf-rpc的代码到本地。源代码地址在:
http://protobuf-rpc.googlecode.com/svn/trunk/
来源:
http://code.google.com/p/protobuf-rpc/
不过这个例子太简单,并且没有java版本的。

4.建立目录D:/Java/protoc/javasrc,
运行命令:
protoc.exe  -I=”D:/Java/protoc/protobuf-2.0.3/src/google/protobuf” –java_out=”D:/Java/protoc/javasrc” D:/Java/protoc/protobuf-2.0.3/src/google/protobuf/descriptor.proto

很奇特,protoc.exe -–java_out=./ D:\Java\protoc\protobuf-2.0.0beta\src\google\protobuf\descriptor.proto 运行出错了。(参考:http://618119.com/archives/2008/07/08/100.html

5.复制D:\Java\protoc\javasrc\com\google\protobuf下的DescriptorProtos.java到
D:\Java\protoc\protobuf-2.0.3\java\src\main\java\com\google\protobuf。
建立java工程到E:\mywork\protobuf,将D:\Java\protoc\protobuf-2.0.3\java\src\main\java\下的文件复制到E:\mywork\protobuf\src。

(以下步骤其实是我绕的弯路)
6.E:\mywork\protobuf\src>svn checkout http://protobuf-rpc.googlecode.com/svn/trunk
/ protobuf-rpc-read-only
A    protobuf-rpc-read-only\protoc.sh
A    protobuf-rpc-read-only\protocol
A    protobuf-rpc-read-only\protocol\protobufrpc.proto
A    protobuf-rpc-read-only\LICENSE
A    protobuf-rpc-read-only\protobufrpc
A    protobuf-rpc-read-only\protobufrpc\common.py
A    protobuf-rpc-read-only\protobufrpc\tx.py
A    protobuf-rpc-read-only\protobufrpc\__init__.py
A    protobuf-rpc-read-only\setup.py
A    protobuf-rpc-read-only\setup.sh
A    protobuf-rpc-read-only\examples
A    protobuf-rpc-read-only\examples\test.proto
A    protobuf-rpc-read-only\examples\txproxy.py
A    protobuf-rpc-read-only\examples\txserver.py
取出版本 13。

7.运行命令:
D:\Java\protoc>protoc.exe  -I=”E:\mywork\protobuf\src\protobuf-rpc-read-only\protocol” –java_out=”D:/Java/protoc/javasrc” E:\mywork\protobuf\src\protobuf-rpc-read-only\protocol\protobufrpc.proto

8.将D:\Java\protoc\javasrc下的pbrpc目录复制到E:\mywork\protobuf\src

9将E:\mywork\protobuf\src\protobuf-rpc-read-only\examples下的test.proto改名为:TestRpc.proto
内容改为:

package test;

message EchoRequest {
required string text = 1;
}

message EchoResponse {
required string text = 1;
}

message PingRequest {
required int32 ping = 1;
}

message PingResponse {
required int32 pingRs = 1;
}

message MathBinaryOperationRequest {
required float first = 1;
required float second = 2;
}

message MathResponse {
required float result = 1;
}

service Test {
rpc Ping( PingRequest ) returns( PingResponse );
rpc Echo( EchoRequest ) returns( EchoResponse );
}

service Math {
rpc Add( MathBinaryOperationRequest ) returns( MathResponse );
rpc Multiply( MathBinaryOperationRequest ) returns( MathResponse );
}

10.再运行:
D:\Java\protoc>protoc.exe  -I=”E:\mywork\protobuf\src\protobuf-rpc-read-only\examples” –java_out=”D:/Java/protoc/javasrc” E:\mywork\protobuf\src\protobuf-rpc-read-only\examples\TestRpc.proto

否则因为包名和类名的冲突,会出现如下错误信息:
D:\Java\protoc>protoc.exe  -I=”E:\mywork\protobuf\src\protobuf-rpc-read-only\examples” –java_out=”D:/Java/protoc/javasrc” E:\mywork\protobuf\src\protobuf-rpc-read-only\examples\test.proto
–java_out: test.proto: Cannot generate Java output because the file’s outer class name, “Test”, matches the name of one of the types declared inside it.  Please either rename the type or use the java_outer_classname option to specify a different outer class name for the .proto file.

然后就可以编写这个例子相关接口的实现类了。

下面开始实际例子:.定义service的proto文件

先假设需求为实现一个用户查看并修改自己的个人信息的功能接口。

在D:\Java\protoc\javasrc建立qquserinfo.proto

内容如下:
[code]
package lizongbo.protobuf;
option java_package = "com.lizongbo.mobileqq";
option java_outer_classname = "QQUserInfoProtos";

option optimize_for = CODE_SIZE;// 也可以设置成 SPEED;

//用户信息定义
message QQUser {
required int32 qqNo = 1 [default = 9999];
required string nickNmae = 2 [default = "mobileq"];
required string signName = 3 [default = "手机QQ"];
required string postCode = 4 [default = "618119"];
required string address = 5 [default = "618119.com"];
}

//获取个人信息的请求
message GetUserInfoRequest {
required int32 qqNo = 1;
}
//获取个人信息的应答结果
message GetUserInfoResponse {
required int32 qqNo = 1;
required int32 result = 2;
optional QQUser userInfo = 3;
}

//更新个人信息的请求
message UpdateUserInfoRequest {
required int32 qqNo = 1;
required QQUser userInfo = 2;
}
//更新个人信息的应答结果
message UpdateUserInfoResponse {
required int32 qqNo = 1;
required int32 result = 2;
optional string message = 3;
}

//个人信息操作的service接口
service UserInfoService {

//获取个人信息
rpc getUserInfo( GetUserInfoRequest  ) returns( GetUserInfoResponse );
//与hessian不同,service里直接限制了不能有同名重载方法。
// 比如这样与下面的方法冲突 rpc updateUserInfo( GetUserInfoRequest ) returns( GetUserInfoResponse );
//出错信息为:qquserinfo.proto:45:9: "updateUserInfo" is already defined in "lizongbo.protobuf.UserInfoService".

//更新个人信息
rpc updateUserInfo( UpdateUserInfoRequest ) returns( UpdateUserInfoResponse );

}

//获取个人信息的请求
message QueryOplogRequest {
required int32 adminId = 1;//管理员id
required int32 qqNo = 2;
}
//获取个人信息的应答结果
message QueryOplogResponse {
//用户操作日志
message Oplog {
required int32 qqNo = 1;
required string opName = 2 [default = "get"];
required string opContent = 3 [default = ""];
}
required int32 qqNo = 1;
required int32 result = 2;
repeated Oplog oplogs=3;

}

//供管理员操作的service 接口
service OplogService {
rpc queryOplog(QueryOplogRequest ) returns(QueryOplogResponse);
}

[/code]

运行命令生成相关代码:
protoc.exe  -I=”D:\Java\protoc\javasrc” –java_out=”D:/Java/protoc/javasrc” D:\Java\protoc\javasrc\qquserinfo.proto
复制D:\Java\protoc\javasrc\com\lizongbo\QQUserInfoProtos.java到E:\mywork\protobuf\src\com\lizongbo\mobileqq。
协议定义的源代码就生成好了。

Tags: apache mina, google protobuf, Hessian, Java, RPC

Related posts