PHP 分类中的最新日记

我在 twitter 和 sina 都有帐号,用 sina 实在是迫不得已,原因大家都知道了吧。我在想要是没有所谓的"墙",国内一些 IT 公司还能活吗?我甚至怀疑,国内的 IT 公司是不是帮凶,或是始作俑者!

两个帐号或是多个帐号怎么同步呢?一个个发累死了,follow5 有同步功能,这也是它的卖点,但是都要提供用户名和密码,为什么不申请 twitter Connections applications 呢?让人费解。谁都号称自己不作恶,但谁放心呢!特别是这些还在测试中的网站。

twitter 和 sina 都有提供 api ,sina 的 api 基本跟 twitter 是一样的。

twitter API 文档

新浪微博 API 文档

请求信息之类的就简单了,这里不说,看着文档自己测试吧。新浪微博 API 文档基本就是 twitter API 文档的中文版,如果不懂英文的就看新浪的文档也能搞出个一二三。

下面分享一段代码,是如何发新微博的。系统要求:php,curl,json支持。

试用 twitter 和 sina 微博的 api 服务

  1. <?php
  2. header('Content-type: text/html; charset=utf-8');
  3. /* 所有都是utf-8字符的 */
  4.  
  5. $d = getApi('http://twitter.com/users/show/wangblog.json');
  6. /* wangblog的用户信息 */
  7.  
  8. //$d = getApi('http://twitter.com/statuses/friends_timeline.json', 'user:pass');
  9. /* wangblog的所有Following用户的最新信息 */
  10.  
  11. //$d = getApi('http://twitter.com/statuses/update.json', 'user:pass', 'status='.urlencode('test twitter\'s api.'));
  12. /* 发表微博给twitter */
  13.  
  14. //$d = getApi('http://api.t.sina.com.cn/statuses/update.json', 'user:pass', 'source=2924220432&status='.urlencode('test sina\'s api.'));
  15. /* 发表微博给sina,其中 source 是用的分享按钮的 */
  16.  
  17. function getApi($url, $userpass=null, $data=null){
  18. $curl = curl_init();
  19. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  20. if($userpass){
  21. curl_setopt($curl, CURLOPT_USERPWD, $userpass);
  22. }
  23. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  24. curl_setopt($curl, CURLOPT_HEADER, 0);
  25. if($data){
  26. curl_setopt($curl, CURLOPT_POST, 1);
  27. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  28. }
  29. curl_setopt($curl, CURLOPT_URL, $url);
  30. $cdata = curl_exec($curl);
  31. $cheaders = curl_getinfo($curl);
  32. if(200 == $cheaders['http_code']){
  33. return $cdata;
  34. }else{
  35. print_r($cheaders);
  36. exit;
  37. }
  38. }
  39.  
  40. print_r(json_decode($d));
  41. ?>

View this snippet on OpenTags.org


Facebook 就直接安装这个应用程序 http://www.facebook.com/apps/application.php?id=2231777543 同步 twitter 到涂鸦墙即可。

这几天有个网站服务器从新加坡迁移到美国,迁移之后就发生了问题,一个新加坡的用户反映说不能登陆,那位新加坡的用户很配合,我反复测试发现,原来是IP地址出错,那个程序的用户验证中有个IP对应的条件,而服务器如果在新加坡之外的国家就无法正确的获取IP地址,在新加坡的服务器就可以。

该问题应该是新加坡的国家局域网防火墙的原因。前段时间很多国外用户无法访问QQ农场,可能问题的原因也在此。我那个用户说,那段时间新加坡很多用户都上不了QQ农场,怎么解决都不行,QQ也没有办法解决,后来莫名其妙的就好了,我估计取消IP地址验证就ok了!

# cd /usr/ports/graphics/pear-Image_Barcode && make install clean

<?php
require_once('Image/Barcode.php');
Image_Barcode::draw('RR651943036CN', 'Code39', 'gif');
exit;
?>

安装 GeoIP

| 暂无评论 | 暂无引用通告
FreeBSD 7.0-RELEASE-i386 + PHP5 + Apache2.2
# cd /usr/ports/net/pecl-geoip && make install clean
# apachectl restart
# cd ~
# fetch http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz
# gunzip GeoIP.dat.gz
# mv GeoIP.dat /usr/local/share/GeoIP/GeoIP.dat
# fetch http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
# gunzip GeoLiteCity.dat.gz
# mv GeoLiteCity.dat /usr/local/share/GeoIP/GeoIPCity.dat

<?php
echo geoip_database_info(GEOIP_COUNTRY_EDITION);

$country = geoip_record_by_name('wangblog.org');
if($country){
	echo "\nThis host is located in: \n";
	print_r($country);
}
?>

可以写个定时更新IP地址库的程序:
cd ~
/usr/bin/fetch http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz
/usr/bin/gunzip GeoIP.dat.gz
mv GeoIP.dat /usr/local/share/GeoIP/GeoIP.dat
/usr/bin/fetch http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
/usr/bin/gunzip GeoLiteCity.dat.gz
mv GeoLiteCity.dat /usr/local/share/GeoIP/GeoIPCity.dat

以上保存为:update-ipdata-cron.sh

crontab -e
添加以下一行,每月5日0点10分运行:
10 0 5 * * /bin/csh /path/to/update-ipdata-cron.sh
Mr Wang

王炜,现在福建泉州从事 B2C 电子商务。爱好编程,业余时间在 FreeBSD / Apache / PHP / PostgerSQL 平台开发 B/S 架构的免费软件。

订阅Feed 订阅 王炜's Blog

Follow Google Buzz Twitter Facebook LinkedIn Sina

收藏王炜's Blog

关于此归档

这里是分类PHP中的最新日记。

上一个分类OpenID

下一个分类PostgreSQL

首页归档页可以看到最新的日记和所有日记。