PHP

浏览模式: 标准 | 列表 | 图文
笔记:IOS发送图片的时候,还是用的$_FILES

笔记:IOS发送图片的时候,还是用的$_FILES

纯笔记:
在与IOS交互的时候,采用了POST发送数据,同事说需要发送一张图片,于是我直接用$_POST进行获取,发现该字段无值。打印整个$_POST的时候,连这个key都没有找到
于是我在想,php://input有没有值呢?试了一下也没有,其实想想也是,如果真是用Base64处理后的发给我。我也应该是可以通过 $_POST收到的。
不过,还没有尝试这种:

preg_match('/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i', $_SERVER['HTTP_CONTENT_DISPOSITION'], $info)

这种是HTML5上传了,不过我没有试过。。。
我看了一下同事以前的代码,发现是用$_FILES的,于是很方便的套了几行代码就一切都OK了。。
看来,脑子还是不能乱。

QQ互联登录应用开发逻辑思路

QQ互联登录应用开发逻辑思路

摘抄这篇文章不是说它的思路有多好,事实上,我们在用微博登录后,如果用getUserInfo方法获取个人信息的时候,里面已经包含了openId的信息了。所以,如果支持微博登录的话,有些信息也可以利用了。。

原文来自:http://lab.wehefei.com/posts/394

当qq登陆成功后,QQ会给我们返回一个唯一的用户标识,也就是openId。

cef6c98bef0e5686e794b6a229199cbc

这个openId是绝对唯一确定用户的标识,你可以把它理解成QQ号。当取得openId的时候,你就需要修改原网站的user表 或 重新建立一个新表,这样做的目的是为了把openId和网站原用户进行绑定。

当qq用户登陆的时候,就需要通过openId去user表查询,如果有查询到,那么就直接修改session登陆成功;

如果没有查询到,就证明是第一次登陆,需要跳转到绑定页面,让用户绑定一个用户名(也就是说创建一个新用户),然后在修改session登陆成功。

例:
1.首先修改用户表(user)增加一个openId字段 或者重新建立一个新表

2.前面说不是用户登录后能得到openId吗?

openId是绝对唯一确定用户的标识 ,所以我们就根据这个(openId)判断

第一步:查询用户表里是否有 openId == 用户登录的openId
if(qq登陆用户openId如果用户表里查出来有的话){
根据openId取得用户信息 跳转到登陆后的用户。
}else if(qq登陆用户openId如果在户表里有没有的话) {
跳转到绑定页面,也就是创建一个新的用户(具体可以参照我的网站:http://ppptuan.com)
//创建的过程中你还可以加判断的
if(如果注册用已经有帐号了){
直接绑定 跳转到登陆后的页面。
}else if(没有帐号){
跳转到注册页面 然后绑定 跳转到登陆后的页面
}
}

思路大概就是这个样子。

当然第二种方法:

可以直接在我们member表添加一个openId的字段就OK了

----------
其实我想表达的是这里面的伪代码想法不错,在开发中,有时候就需要写这样的伪代码来先整理自己的思路。所以其实这篇文章想推荐给我的团队其他同事,在开发前一定要先把思路理清。不想写流程图就写伪代码吧。

mysql_pconnect的水挺深,apache下的数据库长连接

PHP中的mysql_pconnect一直是大家所拿过来做比较的,但对于很多开发者来说,只知道一件事情,那就是mysql_pconnect是长连接,而mysql_connect是普通的连接。既然PHP没有所谓的连接池,那为什么又会有mysql_pconnect的功能呢?
本文是网上而来的,也是我难得看到一篇关于mysql_pconnect的文章。所以摘抄下来
原文来自:mysql_pconnect的水挺深,apache下的数据库长连接

  php的mysql持久化连接,美好的目标,却拥有糟糕的口碑,往往令人敬而远之。这到底是为啥么。近距离观察后发现,这家伙也不容易啊,要看apache的脸色,还得听mysql指挥。

  对于做为apache模块运行的php来说,要实现mysql持久化连接,首先得取决于apache这个web服务器是否支持Keep-Alive。

  Keep-Alive是什么东西?它是http协议的一部分,让我们复习一下没有Keep-Alive的http请求,从客户在浏览器输入一个 有效url地址开始,浏览器就会利用socket向url对应的web服务器发送一条tcp请求,这个请求成功一次就得需要来回握三次手才能确定,成功以 后,浏览器利用socket tcp连接资源向web服务器请求http协议,发送以后就等着web服务器把http返回头和body发送回来,发回来后浏览器关闭socket连接, 然后做http返回头和body的解析工作,最后呈现在浏览器上的就是漂亮的页面了。这里面有什么问题呢?tcp连接需要三次握手,也就是来回请求三次方 能确定一个tcp请求是否成功,然后tcp关闭呢?来回需要4次请求才能完成!每次http请求就3次握手,4次拜拜,这来来回回的不嫌累啊,多少时间和 资源都被浪费在socket连接关闭上了,能不能一次socket tcp连接发送多次http请求呢?于是Keep-Alive就应运而生,http/1.0里需要客户端自己在请求头加入 Connection:Keep-alive方能实现,在这里我们只考虑http1.1了,只需要设置一下apache,让它默认就是Keep- Alive持久连接模式(apache必须1.2+才能支持Keep-Alive).在httpd.conf里找到KeepAive配置项,果断设置为 On,MaxKeepAliveRequests果断为0(一个持久tcp最多允许的请求数,如果过小,很容易在tcp未过期的情况下,达到最大连接,那 下次连接就又是新的tcp连接了,这里设置0表示不限制),然后对于mysql_pconnect最重要的选项KeepAliveTimeout设置为 15(表示15秒).

  好了,重启apache,测试一下,赶紧写行东西

<?php
echo "Apache进程号:".getmypid();
?>

很简单,获取当前php执行者(apache)的进程号,用浏览器浏览这个页面,看到什么?对,有看到一串进程号数字,15秒内,连续刷新页面,看 看进程号有无变化?木有吧?现在把手拿开,交叉在胸前,度好时间,1秒,2秒,3,...15,16。好,过了15秒了,再去刷新页面,进程号有没有变 化?变了!又是一个新的apache进程了,为什么15秒后就变成新的进程了?记得我们在apache里设置的KeepAliveTimeout吗?它的 值就是15秒.现在我们应该大致清楚了,在web服务器默认打开KeepAlive的情况下,客户端第一次http成功请求后,apache不会立刻断开 socket,而是一直监听来自这一客户端的请求,监听多久?根据KeepAliveTimeout选项配置的时间决定,一旦超过这一时间,apache 就会断开socket了,那么下次同一客户端再次请求,apache就会新开一个进程来相应。所以我们之前15内不停的刷新页面,看到的进程号都是一致 的,表明是浏览器请求给了同一个apache进程。

  浏览器是怎么知道不需要tcp连接就可以直接发送http请求呢?因为http返回头里就会带上Connection:keep- alive,Keep-alive:15两行,意思就是让客户端浏览器明白,这次socket连接我这边还没关闭呢,你可以在15内继续使用这个连接,并 发送http请求,于是乎浏览器就知道应该怎么做了.

  那么,php的mysql连接资源是怎么被hold住的呢,这需要查看php的mysql_pconnect的函数代码,我看了下,大概的做法 就是mysql_pconnect根据当前apache进程号,生成hash key,找hash表内有无对应的连接资源,没有则推入hash表,有则直接使用。有些代码片段可以说明(具体可查看php5.3.8源码 ext/mysql/php_mysql.c文件690行php_mysql_do_connect函数)

#1.生成hash key
user=php_get_current_user();//获取当前php执行者(apache)的进程唯一标识号
hashed_details_length = spprintf(&hashed_details, 0, "mysql__%s_", user);//hashed_details就是hash key
#2.如果未找到已有资源,就推入hash表,名字叫persistent_list,如果找到就直接使用
/* try to find if we already have this link in our persistent list */
if (zend_hash_find(&EG(persistent_list), hashed_details, hashed_details_length+1, (void **) &le)==FAILURE) { /* we don't */
...
...
/* hash it up(推入hash表) */
Z_TYPE(new_le) = le_plink;
new_le.ptr = mysql;
if (zend_hash_update(&EG(persistent_list), hashed_details, hashed_details_length+1, (void *) &new_le, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
...
...
}

}else{/* The link is in our list of persistent connections(连接已在hash表里)*/
...
...
mysql = (php_mysql_conn *) le->ptr;//直接使用对应的sql连接资源
...
...

}

zend_hash_find比较容易看明白,原型是zend_hash_find(hash表,key名,key长,value);如果找到,value就有值了。

  说完Keep-Alive,该到mysql家串串门了,说的是mysql_pconnect,怎么能绕开mysql的设置。

  影响mysql_pconnect最重要的两个参数就是wait_timeout和interactive_timeout,它们是什么东西?先撇一边,首先让我们把上面的代码改动一下php代码

<?php
$conn = mysql_pconnect("localhost","root","123456") or die("Can not connect to mysql");
echo "Mysql线程号:".mysql_thread_id($conn)."<br/>";
echo "Apache进程号".getmypid();
?>
以上的代码没啥好解释的,让我们用浏览器浏览这个页面,看到什么?看到两个显眼的数字。一个是mysql线程号,一个是apache进程号,好 了,15秒后再刷新这个页面,发现这两个id都变了,因为已经是新的apache进程了,进程id是新的,hash key就变了,php只好重新连接mysql,连接资源推入persistent list。如果15内刷新呢?apache进程肯定不变,mysql线程号会变吗?答案得问mysql了。首先这个mysql_thread_id是什么 东西?shell方式登录mysql后执行命令'show processlist;',看到了什么?
mysql> show processlist;
+-----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+------+-----------+------+---------+------+-------+------------------+
| 348 | root | localhost | NULL | Query | 0 | NULL | show processlist |
| 349 | root | localhost | NULL | Sleep | 2 | | NULL |
+-----+------+-----------+------+---------+------+-------+------------------+

,发现了很重要的信息,这个processlist列表就是记录了正在跑的线程,忽略Info列为show processlist那行,那行是你当前shell登录mysql的线程。php连接mysql的线程就是Id为349那行,如果读者自己做测试,应该 知道这个Id=349在你的测试环境里是另外一个值,我们把这个值和网页里输出的mysql_thread_id($conn)做做比较,对!他们是一样 的。接下来最重要的是观察Command列和Time列,Command = Sleep,表明什么?表明我们mysql_pconnect连接后就一直在sleep,Time字段就告诉我们,这个线程Sleep了多久,那么 Sleep了多久这个线程才能作废呢?那就是wait_timeout或者interactive_timeout要做的工作了,他们默认的值都是8小 时,天啊,太久了,所以如果说web服务器关掉KeepAlive支持,那个这个processlist很容易就被撑爆,就爆出那个Too many connections的错误了,max_connectiosns配置得再多也没用。为了观察这两个参数,我们可以在mysql配置文件my.cnf里 设置这两个值,找到[mysqld]节点,在里面设置多两行

interactive_timeout = 60
wait_timeout = 30
配置完后,重启mysql,shell登录mysql,这时候show processlist可以发现只有当前线程。然后运行那个带有mysql_pconnect的php页面,再回来mysql端show processlist可发现,多了一个Commond为Sleep的线程,不停的show processlist(方向键上+enter键)观察Time列的变化2,5,10..14!,突然那个Sleep线程程被kill掉了,咋回事,还没 到30秒呢,噢!忘了修改一下apache keepalive的参数了,把KeepAliveTimeOut从15改成120(只为观察,才这么改),重启apache.刷新那个页面,好,开始不 停的show processlist,2..5..10..14,15,..20...26....28,29!线程被kill,这次是因为wait_timeout 起了作用,浏览器那边停了30秒,30内如果浏览器刷新,那这个Time又会从0开始计时。这种连接不属于interactive connection(mysql shell登录那种连接就属于interactive connection),所以采用了wait_timeout的值。如果mysql_pconnect的第4个参数改改呢
<?php
$conn = mysql_pconnect('localhost','root','123456',MYSQL_CLIENT_INTERACTIVE);
echo "Mysql线程号:".mysql_thread_id($conn)."<br/>";
echo "Apache进程号:".getmypid();
?>
刷新下页面,mysql那边开始刷show processlist,这回Time > 30也不会被kill,>60才被kill了,说明设置了MYSQL_CLIENT_INTERACTIVE,就会被mysql视为 interactive connection,那么这次php的mysql连接在60秒内未刷新的情况下,何时作废将取决于mysql的interactive_timeout 的配置值。

  总结:php的mysql_pconnect要达到功效,首先必须保证apache是支持keep alive的,其次KeepAliveTimeOut应该设置多久呢,要根据自身站点的访问情况做调整,时间太短,keep alive没啥意义,时间太长,就很可能为一个闲客户端连接牺牲很多服务器资源,毕竟hold住socket监听进程是要消耗cpu内存的.最后 apache的KeepAliveTimeOut配置得和mysql的time out配置要有个平衡点,联系以上的观察,假设mysql_pconnect未带上第4个参数,如果apache的KeepAliveTimeOut设置 的秒数比wait_timeout小,那真正对mysql_pconnect起作用的是apache而不是mysql的配置.这时如果mysql的 wait_timeout偏大,并发量大的情况下,很可能就一堆废弃的connection了,mysql这边如果不及时回收,那就很可能Too many connections了.可以如果KeepAliveTimeOut太大呢,又回到之前的问题,所以貌似Apache.KeepAliveTimeOu 比Mysql.wait_timeout 稍大,或者相等是比较好的方案,这样可以保证keep alive过期后,废弃的mysql连接可以及时被回收.

--------------

文章不算太长,看看即可,毕竟现在大多数项目 都开始用PDO了,但这样深究的文章还是有值得学习的地方的

三步,临时解决新浪微博接口ID超长的BUG

三步,临时解决新浪微博接口ID超长的BUG

其实,不久前就知道新浪微博的ID的长度发生了变化,所以,原来的无符号长整形的长度是肯定不够了,超出后就变成了科学计数法。这样的URL可是没有办法在浏览器中打开的。
但服务器上一直没有这个问题,运行了uname -a后发现是64位系统,但测试机是32位的,怎么解决这个问题呢?
检查了一下oauth的代码,发现问题出在了json_decode函数上,因为取回来的数据都是字符串,只是在经过json_decode后,数字才超出长度了。知道这个问题后就好办了。
搜索json_decode,在这上面加上一行:
$response = preg_replace('@"id":(\d+)@','"id":"\\1"',$response);
为ID强制加上引号,变成字符串,而不是int型,所以在经过json_decode后,就仍然是字符串了。NND,新浪真偷懒,全部搞成字符串不就结了?到现在还在那里折腾整形不整形的。。

所幸,问题解决了。

转:用XML-RPC协议读写WordPress上的文章

转:用XML-RPC协议读写WordPress上的文章

这是一篇很小的例子,事实上网上这些例子是超多的,不过由于我是要模拟metaweblog或者是wordpress的一些操作,那我就必须要先了解很多相关的背景。
不过这里只是模拟一些发布和读取的接口,稍作了解即可。
原文来自:用XML-RPC协议读写WordPress上的文章我这里只简要的摘取一部分。

WordPress提供了XML-RPC接口使得第三方的博客写作软件(如windows live writer等)可以与之通信来发布和修改博客。我们也可以在网站上使用这个接口做一些工作,例如:在网站首页显示最新的博客、在博客之外的页面上发表博 客文章、把博客与其他内容管理系统结合,等等。

一、开启XML-RPC支持

XML-RPC协议默认是未开启的,需要到WordPress管理后台的“设置–>撰写”里的“远程发布”给XML-RPC打上勾。其说明文 字为:启用 WordPress,Movable Type,MetaWeblog和Blogger 的 XML-RPC发布协议。

二、获取接口的信息

我们查看博客首页的源代码,在head中可以发现类似这样的link项

<link title="RSD" rel="EditURI" type="application/rsd+xml" href="http://blog.bluesky.cn/xmlrpc.php?rsd" />

其中的"RSD"是“Really Simple Discovery”的意思,也就是说这是一个“简单的发现博客编辑接口”的地址。我们用浏览器直接访问这个地址就可以在其返回XML中获知博客的 ID(blogID,一般这个的值是“1”)以及它所支持的接口类型和接口地址。

三、API接口选择

WordPress提供了对Blogger API、metaWeblog API、Movable Type API以及其自身的WordPress API的支持。其中WordPress API最为丰富,提供了包括操作评论文章在内的各种各样的支持。Blogger API功能较少,基本上淘汰不用了;而metaWeblog API提供了常用的接口且参数比较简洁,用得较多一些。

四、例子代码

以下是使用Incutio的XML-RPC库和Blogger API发布文章的例子,其中IXR库也是Wordpress自身所用的库,可以在“wp-includes/class-IXR.php”找到。

<?php
$xmlrpcurl='http://blog.bluesky.cn/xmlrpc.php';
$blogid='1';
$username='admin';
$password='pass';
$postTitle='标题';
$postContent='正文内容';

require('class-IXR.php'); // /wordpress/wp-includes/class-IXR.php
$client = new IXR_Client($xmlrpcurl);

$params=array(
'',
'blog_ID'=>$blogid,
'user_login'=>$username,
'user_pass'=>$password,
'post_content'=>'' . $postContent,
'publish'=>true
);
$params=array_values($params);

$client->query("blogger.newPost",$params);
$response=$client->getResponse();

if ($response['faultCode']==0){
print 'Success';
} else {
print 'Fail:' . $response['faultString'];
}
?>
以下是用XML-RPC for PHP库和metaWeblog接口发布文章的例子。要先下载phpxmlrpc库取出其中的xmlrpc.inc来用。
<?php
include("xmlrpc.inc"); // 这里要改成xmlrpc.inc所在的路径
$GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';

define ('DOMAIN', 'blog.bluesky.cn'); // 博客的域名
define ('BLOGID', 1); // 博客ID,一般为1
define ('USER', 'admin'); // 博客登录的用户名
define ('PASSWORD', 'pass'); // 博客登录的密码

// 创建 xml-rpc client
$cl = new xmlrpc_client ( "/xmlrpc.php", DOMAIN, 80);

// 准备请求
$req = new xmlrpcmsg('metaWeblog.newPost');
// 逐个列出请求的参数:
$req->addParam ( new xmlrpcval ( BLOGID, 'int')); // 博客ID
$req->addParam ( new xmlrpcval ( USER, 'string' )); // 用户名
$req->addParam ( new xmlrpcval ( PASSWORD, 'string' )); // 密码
$struct = new xmlrpcval (
array (
"title" => new xmlrpcval ( '标题', 'string' ), // 标题
"description" => new xmlrpcval ( '正文内容', 'string'), // 内容
), "struct"
);
$req->addParam ( $struct );
$req->addParam ( new xmlrpcval (1, 'int')); // 立即发布

// 发送请求
$ans = $cl->send($req);

var_dump ( $ans );
?>
注意一定要加上:$GLOBALS['xmlrpc_internalencoding'] = ‘UTF-8′;这一句,否则不能正确处理中文。 另一段也是用phpxmlrpc库和metaWeblog接口发表文章的例子代码,该代码使用了返回类型为“phpvals”,这样可以直接用返回结果 的$r->val来获取文章ID号。
<?php
include("xmlrpc.inc");
$GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';

$c = new xmlrpc_client("/xmlrpc.php", "blog.bluesky.cn", 80);
$content['title']="标题"; //标题
$content['description']="正文内容"; //内容
$content['mt_keywords']="标签1,标签2"; //标签
$content['wp_password']=""; //文章密码,输入后显示加密
$content['categories'] = array("分类1","分类2"); //分类名
$x = new xmlrpcmsg("metaWeblog.newPost",
array(php_xmlrpc_encode("1"), //BLOG ID
php_xmlrpc_encode("admin"), //用户名
php_xmlrpc_encode("pass"), //密码
php_xmlrpc_encode($content),
php_xmlrpc_encode("1"))); //立即发表

$c->return_type = 'phpvals';
$r =$c->send($x);
if ($r->errno=="0")
echo "发表成功,文章序号为:".$r->val;
else {
echo "出错了";
print_r($r);
}
?>

以下是读取最新一篇文章的例子代码:

<?php
include("xmlrpc.inc");
$GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
$c = new xmlrpc_client("/xmlrpc.php", "blog.bluesky.cn", 80);

$msg = new xmlrpcmsg('metaWeblog.getRecentPosts',
array(php_xmlrpc_encode("1"),
php_xmlrpc_encode("admin"),
php_xmlrpc_encode("pass"),
php_xmlrpc_encode("1")) );
$res = $c->send($msg);
$val = $res->value();

$val->arraymem(0)->structreset();
while (list($key, $v) = $val->arraymem(0)->structeach())
{
echo "Element: $key; Type: ".$v->kindOf()."; Value:".$v->scalarval()."\n";
}
?>
原文地址:http://blog.bluesky.cn/archives/466/using-xml-rpc-protocol-to-read-and-write-articles-on-wordpress.html

RFC: MetaWeblog API

来自xmlrpc网站的资料:http://www.xmlrpc.com/metaWeblogApi
关于这些api还有一些背景资料在这里的:http://www.xmlrpc.com/stories/storyReader$2509,多看看啦。。。

Document status

This document was updated on 8/8/03, to incorporate all the RFCs related to the MetaWeblog API. The earlier version of the document is archived here. It has been reviewed by members of the MetaWeblog API mail list, and feedback has been incorporated.

On 8/24/03, I posted a last call for comments, and received several and incorporated some.

As of 8/26/03, this document is deployable. There may be changes, but they will be clearly documented, and will only clarify the spec, in no way will they change the format or protocol. It is now safe to deploy applications based on this spec.

What is the MetaWeblog API?

The MetaWeblog API (MWA) is a programming interface that allows external programs to get and set the text and attributes of weblog posts. It builds on the popular XML-RPC communication protocol, with implementations available in many popular programming environments.

Relationship between MetaWeblog API and the Blogger API

The MetaWeblog API is designed to enhance the Blogger API, which was limited in that it could only get and set the text of weblog posts. By the time MWA was introduced, in spring 2002, many weblog tools had more data stored with each post, and without an API that understood the extra data, content creation and editing tools could not access the data.

At the time of this writing, summer 2003, most popular weblog tools and editors support both the Blogger API and the MetaWeblog API.

Relationship between MetaWeblog API and RSS 2.0

The MetaWeblog API uses an XML-RPC struct to represent a weblog post. Rather than invent a new vocabulary for the metadata of a weblog post, we use the vocabulary for an item in RSS 2.0. So you can refer to a post's title, link and description; or its author, comments, enclosure, guid, etc using the already-familiar names given to those elements in RSS 2.0. Further since RSS 2.0 is extensible, so is the MetaWeblog API. We have designed conventions for representing attributes and namespaces in MWA.

Basic entry-points

There are three basic entry-points in the API:

metaWeblog.newPost (blogid, username, password, struct, publish) returns string

metaWeblog.editPost (postid, username, password, struct, publish) returns true

metaWeblog.getPost (postid, username, password) returns struct

The blogid, username, password and publish params are as in the Blogger API. newPost returns a string representation of the post id, again as defined by the Blogger API. The struct is where the juice is.

The struct

In newPost and editPost, content is not a string, as it is in the Blogger API, it's a struct. The defined members of struct are the elements of <item> in RSS 2.0, providing a rich variety of item-level metadata, with well-understood applications.

The three basic elements are title, link and description. For blogging tools that don't support titles and links, the description element holds what the Blogger API refers to as "content."

Where an element has attributes, for example, enclosure, pass a struct with sub-elements whose names match the names of the attributes according to the RSS 2.0 spec, url, length and type.

For the source element, pass a struct with sub-elements, url and name.

For categories, pass an array of strings of names of categories that the post belongs to, named categories. On the server side, it's not an error if the category doesn't exist, only record categories for ones that do exist.

In getPost, the returned value is a struct, as with the Blogger API, but it contains extra elements corresponding to the struct passed to newPost and editPost.

The server must ignore all elements that it doesn't understand.

In a call to metaWeblog.newPost or metaWeblog.editPost, if the struct contains a boolean named flNotOnHomePage, then the post does not appear on the home page, and only appears on the specified category pages.

Request and response

Here's an example of a request and a response.

Here's the post that this request is getting info about.

metaWeblog.newMediaObject

metaWeblog.newMediaObject (blogid, username, password, struct) returns struct

The blogid, username and password params are as in the Blogger API.

The struct must contain at least three elements, name, type and bits.

name is a string, it may be used to determine the name of the file that stores the object, or to display it in a list of objects. It determines how the weblog refers to the object. If the name is the same as an existing object stored in the weblog, it may replace the existing object.

type is a string, it indicates the type of the object, it's a standard MIME type, like audio/mpeg or image/jpeg or video/quicktime.

bits is a base64-encoded binary value containing the content of the object.

The struct may contain other elements, which may or may not be stored by the content management system.

If newMediaObject fails, it throws an error. If it succeeds, it returns a struct, which must contain at least one element, url, which is the url through which the object can be accessed. It must be either an FTP or HTTP url.

metaWeblog.getCategories

metaWeblog.getCategories (blogid, username, password) returns struct

The struct returned contains one struct for each category, containing the following elements: description, htmlUrl and rssUrl.

This entry-point allows editing tools to offer category-routing as a feature.

metaWeblog.getRecentPosts

metaWeblog.getRecentPosts (blogid, username, password, numberOfPosts) returns array of structs

Each struct represents a recent weblog post, containing the same information that a call to metaWeblog.getPost would return.

If numberOfPosts is 1, you get the most recent post. If it's 2 you also get the second most recent post, as the second array element. If numberOfPosts is greater than the number of posts in the weblog you get all the posts in the weblog.

Transmitting elements with attributes

The members of the struct passed in newPost and editPost come from the elements of items in RSS 2.0. The most commonly used core elements have no attributes, so it's clear how to include them in the struct. However, some elements, such as source, enclosure and category, may have attributes and a value. Here are a simple set of rules for elements that have attributes and a value. Note that these rules do not apply to enclosure and source, which are provided for specifically above.

1. If an element has attributes, then represent the element with a struct, and include the attributes as sub-elements of the struct.

2. If an element has both attributes and a value, make the element a struct, include the attributes as sub-elements, and create a sub-element for the value with the name _value. Note that this means that no element can be passed through the API that has an attribute whose name is _value.

Transmitting elements from namespaces

RSS 2.0 allows for the use of namespaces. If you wish to transmit an element that is part of a namespace include a sub-struct in the struct passed to newPost and editPost whose name is the URL that specifies the namespace. The sub-element(s) of the struct are the value(s) from the namespace that you wish to transmit.

Comments

The Blogger API provides a parameter called appkey that allows vendors to assign a key to developers so they can track and possibly limit usage of the API for certain tools. The MetaWeblog API doesn't specifically provide a parameter for an appkey. Applications that wish to transmit an appkey should add an element to the struct called appkey and set its value to the appkey that should be associated with the call.

Applications should use the fault-response scheme defined by XML-RPC. For example, trying to create, get, or edit a post without a valid username-password should generate a fault. Client applications should display the error string, as appropriate, to the user, for example, in a dialog, or in a server log.

Thanks

Thanks to Michael Bernstein for help editing this spec in summer 2003.

References

RSS 2.0; Dave Winer; 9/02.

RFC: MetaWeblog API; Dave Winer; 3/02.

Blogger API; Evan Williams; 8/01.

ManilaRPC; Andre Radke, Brent Simmons, Dave Winer; 1999.

XML-RPC; Dave Winer; 1998

资料:WordPress的四种远程XML-RPC发布协议

资料:WordPress的四种远程XML-RPC发布协议

没有什么好说的,学习一下这些资料。然后折腾中。
想用word发布博客,这些就必须要看。
原文来自:http://m2009.org/?p=998

WordPress支持四种远程发布协议,他们是 WordPress,Movable Type,MetaWeblog和Blogger 的 XML-RPC发布协议。

WordPress发布协议

WordPress 发布协议值wordpress自己的文章发布协议,他的接口最为丰富,提供了包括操作评论文章在内的各种各样的支持

WordPress发布协议文档:http://codex.wordpress.org/XML-RPC_wp

Movable Type发布协议

Movable Type,简称MT,是由位于美国加州的Six Apart公司推出的网志(blog)发布系统。它是全球最受欢迎的网志系统之一,包含多用户,评论,引用(TrackBack),主题等功能,并广泛的支持各种第三方插件。

Movable Type不仅可以应用于个人的网志系统,而且可以应用于商业、教育等领域。Movable Type于2007年12月12日正式宣布以GPLv2的协议开源。

Movable Type发布协议文档:http://www.movabletype.org/documentation/

Movable Type 文件集: http://mtbook.org/

MetaWeblog发布协议

The MetaWeblog API is an application programming interface created by software developer Dave Winer that enables weblog entries to be written, edited, and deleted using web services.

The API is implemented as an XML-RPC web service with three methods whose names describe their function: metaweblog.newPost(), metaweblog.getPost() and metaweblog.editPost(). These methods take arguments that specify the blog author’s username and password along with information related to an individual weblog entry.

The impetus for the creation of the API in 2002 was perceived limitations of the Blogger API, which serves the same purpose. Another weblog publishing API, the Atom Publishing Protocol became an IETF Internet standard (RFC 5023) in October 2007.

Many blog software applications and content mangement systems support the MetaWeblog API, as do numerous desktop clients.

MetaWeblog文档:http://www.xmlrpc.com/metaWeblogApi

Blogger发布协议

The Blogger Data API allows client applications to view and update Blogger content in the form of Google Data API feeds.

文档(墙外):http://www.blogger.com/developers/api/1_docs/

google:http://code.google.com/intl/zh-CN/apis/blogger/

比较详细的api文档

API参考文档:http://www.sixapart.com/developers/xmlrpc/

转:网站优化 更小的静态资源

转:网站优化 更小的静态资源

这是来自perfgeeks.com的一篇文章,介绍了一些常见的工具,还有一个脚本。
在用YII框架的时候,我用的是hightman写的一个cssmin的插件,可以直接把css和js进行合并到一个文件。对于图片,我都是把这个艰巨的任务交给前端,由他们完成,至于他们用什么样的png或者gif之类的优化软不不是我关心的了。OK,先上文章
来源网址是是:http://www.perfgeeks.com/?p=660
内容如下:

更小的静态资源(js、css、png、gif),意味着更少的网络传送时间。构建的时候,可以把这些静态资源进行压缩优化(不像gzip/deflate压缩),使之更小化。有很多相应的开源工具帮助你完成这项工作。

javascript

  • Google Closure Compiler
  • UglifyJS
  • YUI Compressor
  • ShrinkSafe
  • 其它,比如JSMIN

Node.js、jQuery1.5开始使用UglifyJS,UglifyJS压缩比YUI Compressor更小、比Google Closure Compiler更安全。尽管如此,但UglifyJS需要部署NodeJS环境,所以我们还是选择使用Google Closure Compiler

style(css)

  • CSSTidy
  • YUI Compressor
  • Yslow/Google Page Speed

CSSTidy和YUI Compressor都很棒,我们还是选择老牌的YUI Compressor,因为我们更熟悉它,它也能够满足我们的需求。

png8/gif图片

  • Optipng
  • AdvanceCOMP(advpng、advdef)
  • ImageMagic(mogrify、identify、convert)
  • Pngcrush
  • Pngout
  • gifsicle
  • jpegtran

任何大一点的网站页面都会使用到不少图片,压缩优化图片很有必要。选择什么样的图片格式,决定了怎么去压缩图片。一般而言,只要是非动画图片,我们 推荐png8,即便是颜色很少的小图片(尽管这样的图片gif有更高压缩性,但应该使用css sprites)。Pngout没有开放源码,仅能在Window NT平台使用,所以我们并不考滤使它。Pngcrush虽然很好用,但是optipng、advpng以及advdef结合使用能把图片压缩得更小,所以 我们选择optipng、advpng以及advdef压缩优化PNG图片。 Optipng压缩优化图片、而advdef则优化压缩算法。

构建脚本

发布产品的时候,我们希望构建前端资源,包括压缩优化、合并等等。构建应该尽量满足:
1.整个过程是自动的,不需要人工介入
2.所有的操作都是安全的
3.免费的命令行工具

我们这里应用bash写了一个简版的部署脚本,能够简单地应付中小型网站静态资源发布。

#!/bin/sh
#filename:build.sh
IN_FILE="/var/www.imgwell.com/themes/ocean/misc"
OUT_FILE="/var/www.imgwell.com/misc"
EXCLUDE_FILES="jquery.min.js LAB.min.js"
GOOGLE_COMPILER="/opt/build/bin/compiler.jar"
YUI_COMPRESSOR="/opt/build/bin/yuicompressor-2.4.6.jar"
OPTIPNG="/usr/local/bin/optipng -quiet -o3 "
ADVPNG="/usr/local/bin/advpng -q -z -4 "
ADVDEF="/usr/local/bin/advdef -q -z -4 "


function mt_ver_code() {
local MATRIX="23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz"
local LENGTH=12
while [ "${n:=1}" -le "$LENGTH" ]; do
local PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
let n+=1
done
echo -n ${PASS}
}

function mt_file_ext() {
local FILE=`basename -- "$1"`
echo -n "${FILE##*.}"
}

function mt_file_size() {
if [ -f "$1" ]; then
echo -n `ls -l -- "$1" |awk '{print $5}'`
else
echo -n 0
fi
}

function mt_has_exclude() {
if [ -z "$EXCLUDE_FILES" ]; then
echo -n 0
return 0
fi
echo "$EXCLUDE_FILES" |grep -q -w -- "${1}"
if [ $? -eq 0 ]; then
echo -n "1"
else
echo -n "0"
fi
}

function mt_google_compile() {
java -jar "$GOOGLE_COMPILER" --js $1 --js_output_file $2
}

function mt_yui_compressor() {
java -jar "$YUI_COMPRESSOR" $1 -o $2 --charset utf-8
}

function mt_png_opti() {
[ -f "`echo ${OPTIPNG} |awk '{print $1}'`" ] && ${OPTIPNG} "${1}"
[ -f "`echo ${ADVPNG} |awk '{print $1}'`" ] && ${ADVPNG} "${1}"
[ -f "`echo ${ADVDEF} |awk '{print $1}'`" ] && ${ADVDEF} "${1}"
}

function __main__() {
[ -d "$IN_FILE" ] || exit 1
local VER_CODE=`mt_ver_code`
local FILE_STATUS="!"
mkdir -p "${OUT_FILE}/${VER_CODE}"

for f in `ls -1 "$IN_FILE"`; do
if [ -d "${IN_FILE}/${f}" ] ; then
continue
fi

local HAS_EXCLUDE=`mt_has_exclude "${f}"`
local FILE_SRC_SIZE=`mt_file_size "${IN_FILE}/${f}"`

if [ "`mt_file_ext "${f}"`" = "js" -a "$HAS_EXCLUDE" = "0" ]; then
mt_google_compile "${IN_FILE}/${f}" "${OUT_FILE}/${VER_CODE}/${f}"
FILE_STATUS="G"
elif [ "`mt_file_ext "${f}"`" = "css" -a "$HAS_EXCLUDE" = "0" ]; then
mt_yui_compressor "${IN_FILE}/${f}" "${OUT_FILE}/${VER_CODE}/${f}"
FILE_STATUS="Y"
elif [ "`mt_file_ext "${f}"`" = "png" -a "$HAS_EXCLUDE" = "0" ]; then
cp "${IN_FILE}/${f}" "${OUT_FILE}/${VER_CODE}"
[ -f "${OUT_FILE}/${VER_CODE}/${f}" ] && {
mt_png_opti "${OUT_FILE}/${VER_CODE}/${f}"
FILE_STATUS='O'
}
else
cp "${IN_FILE}/${f}" "${OUT_FILE}/${VER_CODE}"
if [ $? -eq 0 ]; then
FILE_STATUS="-"
else
FILE_STATUS="D"
fi
fi

local FILE_DST_SIZE=`mt_file_size "${OUT_FILE}/${VER_CODE}/${f}"`
echo "${FILE_STATUS} ${HAS_EXCLUDE} ${f} ${FILE_SRC_SIZE}/${FILE_DST_SIZE}"
done
echo "===========/" $VER_CODE "\=========="
}

__main__

执行结果如下

[root@www-avatar misc]# ./build.sh
O 0 6N9FQPpTHCy.png 820/258
Y 0 base.css 40171/35530
O 0 FSEB6oLTK3I.png 10362/10362
- 0 GsNJNwuI-UM.gif 522/522
O 0 heart.png 921/807
O 0 IJYgcESal33.png 5771/5771
O 0 _IKHHfAgFQe.png 2635/2302
G 0 jquery.elastic.js 4988/1665
- 1 jquery.min.js 85260/85260
G 0 jquery.ui.dialog.js 10074/5274
G 0 jquery.ui.pview.js 4565/2878
- 1 LAB.min.js 5537/5537
O 0 lFahQXTaTNO.png 90/90
G 0 mutfa.js 36958/21777
O 0 nCItFQafRu8.png 452/288
O 0 p13yZ069LVL.png 792/219
- 0 plupload.flash.swf 18537/18537
G 0 plupload.full.js 48277/46682
Y 0 position.css 7737/7440
O 0 star.png 3292/283
G 0 stars.js 6333/2622
Y 0 ui_plugin.css 12794/12079
G 0 up.js 6230/3991
- 0 uVR6w3wRHEJ.gif 54/54
O 0 WSQ2wnhSG-F.png 245/229
- 0 _ZWZupdaAgS.gif 827/827
===========/ LruQcmx4Zi84 \==========

总结

1.UglifyJS压缩比YUI Compressor更小,比Google Closure Compiler更安全。不想冒险,还是应该选择UglifyJS。若想最小化,可以选择Google Closure Compiler
2.YUI Compressor压缩css文件。但CSSTidy也很不错
3.optipng -o3 *.png |advpng -z -4 *.png |advdef -z -4 *.png 将最大化压缩优化png图片
4.网页尽量使用png格式图片,并且压缩优化它,使之最优

-------------------------------

纠结,原文中的代码是有配色的,但是COPY过来就没有配色了,而自带的配色没有bash的配色。所以。。。将就点看了,或者看原文吧:http://www.perfgeeks.com/?p=660

郁闷,又在纠结swfupload的302问题了

郁闷,又在纠结swfupload的302问题了

这是一个非常让人郁闷的问题。问题也知道根源在哪里,但TMD就是纠结。
因为是SWF,你懂的,有些包抓不到的。但不搞又不行,准备用smartsniff来抓数据看了。

其实就是一个session的问题,但还没有彻底搞定,真烦人。
yii框架中应用swfupload还是有点纠结的(其实以前都没有问题,不知道什么时候,突然间就开始有这个BUG了。。。)

仔细排查了一下,其实,大家都知道,session原本就是用来处理登录这种玩意的。果然排查后发现,新开的上传进程之所以是302,就是因为跳转到登录页面去了。晶啊。每次都是这个原因,我靠

来自coolshell:重构代码的7个阶段

来自coolshell:重构代码的7个阶段

代码重构是所有人的噩梦。怎么样重构一系列代码,是一件非常重要的事情。
在没转这篇文章之前,我的重构都是先从部分非关键代码开始重构,而且把这一部分慢慢的独立成一个模块,在采用新的架构的时候,这一块也可以被嵌入的新的架构中。
当然在其中也会遇到一系列的问题,但这样来说会轻松一点。
OK,让我们来看原文吧。
原文是来自:http://coolshell.cn/articles/5201.html

你曾去想重构一个很老的模块,但是你只看了一眼你就恶心极了。文档,奇怪的函数和类的命名,等等,整个模块就像一个带着脚镣的衣衫褴褛的人,虽然能 走,但是其已经让人感到很不舒服。面对这种情况,真正的程序员会是不会认输的,他们会接受挑战认真分析,那怕重写也在所不惜。最终那个模块会被他们重构, 就像以前和大家介绍过的那些令人销魂的编程方式中的屠宰式编程一样。下面是重构代码的几个阶段,文章来自:The 7 stages of refactoring,下面的翻译只是意译。

第一阶段 - 绝望

在你开始去查看你想要重构的模块的,你会觉得好像很简单,这里需要改一个类,那里需要改两到三个函数,重写几个 函数,看上去没什么大不了的,一两天就搞定了。于是你着手开始重构,然后当你调整重构了一些代码,比如改了一些命名,修理了一些逻辑,渐渐地,你会发现这 个怪物原来体型这么大,你会看到与代码不符甚至含糊不清的注释,完全摸不着头脑的数据结构,还有一些看似不需要方法被调了几次,你还会发现无法搞清一个函 数调用链上的逻辑。你感到这个事可能一周都搞不定,你开始绝望了。

第二阶段 – 找最简单的做

你承认你要重构的这个模块就是一个可怕的怪物,不是一两下就可以搞定的,于是你开始着干一些简单的事,比如重新命名一下几个函数,移除一些代码的阻碍,产生几个常量来消除magic number,等等,你知道这样做至少不会让代码变得更糟糕。

第三阶段 – 再次绝望

但是接下来的事会让你再次撞墙。你会发现那些代码的瑕疵是些不痛不痒的事,改正这些事完全于事无补,你应该要做 的事就是重写所有的东西。但是你却没有时间这么干,而这些代码剪不乱理还乱,耦合得太多,让你再一次绝望。所以,你只能部分重写那些不会花太多时间的部 分,这样至少可以让这些老的代码能被更多的重用。虽然不完美,但是至少可以试试。

第四阶段 – 开始乐观

在你试着部分重构这个模块几天之后,随着重构了几个单元后,虽然你发现改善代码的进度太慢了,但此时,你已知道代码应该要被改成什么样,你在痛苦之 后也锁定了那些那修改的类。是的,虽然你的时间预算已经超支,虽然要干的事比较多,但你还是充满希望,觉得那是值得的。你胸中的那团火又被点燃了。

第五阶段 - 快速了结

在这个时候,你发现你已花了太多的时间,而情况越来越复杂,你感到你所面对的情况越来越让你越到不安,你明白你自己已经陷入了困境。你原本以为只需 要一次简单的重构,然而现在你要面对的是重写所有的东西。你开始意识到原因是因为你是一个完美主义者,你想让代码变得完美。于是你开始在怠慢你文档,并想 找到一个捷径来重写老的代码,你开始采用一些简单而粗暴,快速而有点肮脏的方法。虽然不是很完美,但你就是这样去做了。然后,你开始运行测试做UT,发现 UT报告上全是红色,几乎全都失败了,你恐慌了,于是快速地fix代码,然后让UT 能工作。此时,你拍拍自己胸口,说到,没问题 ,于是就把代码提交了。

第六阶段 – 修改大量的Bug

你的重写并不完美,虽然其过了测试,但是那些UT测试对于你的新的代码有点不太合适,虽然他们都没有报错,但是他们测试得范围太小了,没有覆盖到所 有的情况和边界。所以,在这以后,你还需要几周或是更长的时间不得不来修正越来越多的bug,这使得你的设计和代码在每一次quick-fix后就变得越 来越难看。此时,代码已经不像你所期望的那样完美了,但你依然觉得他还是比一开始要好一些。这个阶段可能历经几个月。

第七阶段 - 觉悟

经过了6个月,你重写的模块又出了一个比较严重的bug。这让你重构的那个模块变得更难堪。你发现出的这个问题是和当初的设计不一致,你还发现被你 重构掉的那段老的代码并不是当初看上去的那么坏,那段老的代码确实考虑到了一些你未曾考虑到的事情。这个时候,你团队里有人站出来说这个模块应该被重构或 是重写,而你却不动声色地一言不发,并希望那个站出来的人能在几个月后能觉悟起来。

——————

不知道这是不是你的经历,我经历过很多次这样的事。对于很多维护性质的项目,我犯过的错误让我成了一个实实在在的保守派,我几乎不敢动,那怕看到代 码很不合口味。当然,那些从来没有写过代码的敏捷咨询师一定会说用TDD或是UT可以让你的重构更有效也更容易,因为这样会让他们显得更我价值,但我想告 诉你,这种脱离实际的说法很不负责任,这就好比说—— 我在杀猪的时候遇到了一些麻烦,因为我对猪的生理结构不清楚,或是这本来就是一头畸形的猪,导致我杀的猪很难看,而伟大的敏捷咨询师却告诉我,要用一把更快更漂亮的刀。软件开发永远不是那么简单的事,杀猪也一样。

-----------------------
不过我想,更多的时候,安排你重构一份代码的时候,你可能想到的就是我情愿重写一份也不愿意重构,对着数据库结构和一些业务逻辑,然后就开始重写了。只是这样的写法,后面遇到的问题会非常非常多。你呢?也是这样的吗?