<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>陋室博客 &#187; malu8</title>
	<atom:link href="http://bolg.malu.me/html/author/malu8/feed" rel="self" type="application/rss+xml" />
	<link>http://bolg.malu.me</link>
	<description></description>
	<lastBuildDate>Fri, 17 Jul 2015 01:37:16 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>PHP飞信类的bug修复 phpfetion_v1.3.0.zip</title>
		<link>http://bolg.malu.me/html/2012/1858.html</link>
		<comments>http://bolg.malu.me/html/2012/1858.html#comments</comments>
		<pubDate>Tue, 17 Jul 2012 09:32:58 +0000</pubDate>
		<dc:creator>malu8</dc:creator>
		
		<guid isPermaLink="false">http://bolg.malu.me/html/2012/1858.html</guid>
		<description><![CDATA[前不久用了全恒壮的技术博客上的一个php飞信类，用它实现一个小程序。 不过最近发 &#8230; <a href="http://bolg.malu.me/html/2012/1858.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>前不久用了<a href="http://blog.quanhz.com/" target="_blank">全恒壮的技术博客</a>上的一个<a href="http://bolg.malu.me/html/tag/php" target="_blank">php</a>飞信类，用它实现一个小程序。</p>
<p>不过最近发现在向好友发短信的时候总是失败，于是查看程序源码：</p>
<p>源码放在服务器上没动过，以前用都好的，第一反应就是飞信官方对wap页面做了修改（因为该类是通过模拟登录wap飞信来实现）。</p>
<p>果然，在查看了wap飞信网页的相关源代码后发现如下内容:</p>
<p><img src="http://img.malu.me/bolg_img/images_2012/feixing_code.jpg" alt="" /></p>
<p><span id="more-1858"></span></p>
<p>而原来的代码中并没有csrfToken的值，如下所示：</p>
<pre>	/**
	 * 向好友发送飞信
	 * @param string $uid 飞信ID
	 * @param string $message 短信内容
	 * @return string
	 */
	protected function _toUid($uid, $message) {
		$uri = '/im/chat/sendMsg.action?touserid='.$uid;
		$data = 'msg='.urlencode($message);

		$result = $this-&gt;_postWithCookie($uri, $data);

		return $result;
	}</pre>
<p>找到了原因，解决办法就有了，我们只要把csrfToken的值提取出来，再传递给该内部函数，通过POST方式把msg和csrfToken一起提交给飞信wap端就可以了。</p>
<p>修改后的结果如下：</p>
<pre>	/**
	 * 向好友发送飞信
	 * @param string $uid 飞信ID
	 * @param string $message 短信内容
	 * @return string
	 */
	protected function _toUid($uid, $message) {
		$uri = '/im/chat/sendMsg.action?touserid='.$uid;
		$data = 'msg='.urlencode($message).'&amp;csrfToken='.$this-&gt;_getcsrfToken($uid);

		$result = $this-&gt;_postWithCookie($uri, $data);

		return $result;
	}</pre>
<p>其中用来获取csrfToken值的内部函数_getcsrfToken()如下：</p>
<pre>	/**
	 * 获取csrfToken值
	 * @param string $uid
	 * @return string
	 */
	protected function _getcsrfToken($uid) {
		$uri = 'im/chat/toinputMsg.action?touserid='.$uid;
		$host = 'f.10086.cn';
		$result = $this-&gt;_getWithCookie($uri, $host);
		// 匹配
		preg_match('/name="csrfToken" value="(\w+)"/', $result, $matches);
		return isset($matches[1]) ? $matches[1] : '';
	}</pre>
<p>获取时需要用到带cookie的GET方式，我用socket构建了一个内部函数如下：</p>
<pre>	/**
	 * 携带Cookie向f.10086.cn发送GET请求
	 * @param string $uri
	 * @param string $host
	 */
	protected function _getWithCookie($uri, $host){
		$fp = fsockopen($host, 80) or die("Open ". $uri ." failed");
		$header = "GET /".$uri ." HTTP/1.1\r\n";
		$header .= "Accept: */*\r\n";
		$header .= "Accept-Language: zh-cn\r\n";
		$header .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; InfoPath.1; .NET CLR 2.0.50727)\r\n";
		$header .= "Host: ". $host ."\r\n";
		$header .= "Connection: Keep-Alive\r\n";
		$header .= "Cookie: {$this-&gt;_cookie}\r\n";
		$header .= "Connection: Close\r\n\r\n"; 

		fwrite($fp, $header);
		while (!feof($fp)) {
			$contents .= fgets($fp, 8192);
		}
		fclose($fp);
		return $contents;
	}</pre>
<p>经过以上修改，这个飞信类又可以正常地向好友发送短信了。</p>
<p>更新文件点此下载：phpfetion_v1.3.0.zip</p>
<p><span style="color: #ff0000;"><strong>2012年7月18日：抱歉啊，下载链接出了点问题，各位请到本博客的 <a href="http://bolg.malu.me/down">工具|下载</a> 页面中的 源代码 文件夹中下载：</strong></span></p>
<p><a title="下载" href="http://bolg.malu.me/down"><img class="alignnone" title="php飞信下载" src="http://img.malu.me/bolg_img/images_2012/phpfetion.jpg" alt="" width="550" height="308" /></a></p>
<p>我也将向该类的原作者告知这一BUG，原来的内容参照这里：<a href="http://blog.quanhz.com/archives/171" target="_blank">PHP飞信发送类(PHPFetion)v1.2发布</a></p>
<p>历史版本：<a title="http://code.google.com/p/php-fetion/downloads/list" href="http://code.google.com/p/php-fetion/downloads/list">http://code.google.com/p/php-fetion/downloads/list</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bolg.malu.me/html/2012/1858.html/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>在线DNS查询工具（无污染）</title>
		<link>http://bolg.malu.me/html/2012/1855.html</link>
		<comments>http://bolg.malu.me/html/2012/1855.html#comments</comments>
		<pubDate>Tue, 10 Jul 2012 06:07:25 +0000</pubDate>
		<dc:creator>malu8</dc:creator>
		
		<guid isPermaLink="false">http://bolg.malu.me/html/2012/1855.html</guid>
		<description><![CDATA[在国内，由于某政策原因，网络上的一些域名会遭到劫持污染。要查询该域名正确的解析信 &#8230; <a href="http://bolg.malu.me/html/2012/1855.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>在国内，由于某政策原因，网络上的一些域名会遭到劫持污染。要查询该域名正确的解析信息往往要通过代理，或者登录到国外服务器上进行查询。这样做显然比较麻烦。于是我设想通过国外的服务器来建立一个在线的<a href="http://bolg.malu.me/html/tag/dns" target="_blank">DNS</a>查询工具，这样会方便不少：</p>
<p><a href="http://iiii.sinaapp.com/dns/" target="_blank"><img src="http://img.malu.me/bolg_img/images_2012/search_DNS.jpg" /></a> </p>
<p><span id="more-1855"></span>
<p>本工具基于<a href="http://bolg.malu.me/html/tag/php" target="_blank">PHP</a>的socket工作原理，在经实际测试后发现，在国内SAE服务上架设，只要通过TCP协议来查询DNS服务器就不会被污染（默认DNS查询是UDP协议）。那么我就放心把服务架在国内了。其实放国内还有一个好处，由于某墙还会过滤URL中的所谓不合法地址，也就是只要URL中出现不合法地址，你和该地址的TCP链接会被强制中断，不管你的主域是否合法，而放在国内的话，即使你的URL中包含了这些不合法地址，也不会经过该墙，同时也不会经过它的过滤，所以从访问稳定性来说国内查询会更加稳定。</p>
<p>需要的童鞋点这里：<a href="http://iiii.sinaapp.com/dns/">http://iiii.sinaapp.com/dns/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bolg.malu.me/html/2012/1855.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>利用新浪微博制作一个图床</title>
		<link>http://bolg.malu.me/html/2012/1851.html</link>
		<comments>http://bolg.malu.me/html/2012/1851.html#comments</comments>
		<pubDate>Fri, 08 Jun 2012 04:01:13 +0000</pubDate>
		<dc:creator>malu8</dc:creator>
		
		<guid isPermaLink="false">http://bolg.malu.me/html/2012/1851.html</guid>
		<description><![CDATA[前不久，在学习新浪微博授权机制OAuth2.0时，也同时看了一下API的普通鉴权 &#8230; <a href="http://bolg.malu.me/html/2012/1851.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>前不久，在学习新浪微博授权机制OAuth2.0时，也同时看了一下<a href="http://bolg.malu.me/html/tag/api" target="_blank">API</a>的普通鉴权(Basic Authentication)方式。借鉴了网上一位仁兄写的普通鉴权的类<a href="http://code.google.com/p/sinatopenapi/">http://code.google.com/p/sinatopenapi/</a> 后，写了一个<a href="http://bolg.malu.me/html/tag/%E5%BE%AE%E5%8D%9A" target="_blank">微博</a>图床的页面：</p>
<p><a title="点击进入微博图床" href="http://imgsina.sinaapp.com/images/" target="_blank"><img src="http://img.malu.me/bolg_img/images_2012/weibo_images_001.jpg" alt="" /></a></p>
<p><span id="more-1851"></span></p>
<p>该图床通过新浪微博以前的普通鉴权方式，在服务器端进行登录认证，从而实现用户打开网页就能直接发微博。而我做的就是利用那个鉴权类，来发布一条带图片的微博。</p>
<p>由于新浪微博的特殊性（微博由于有转发等功能，在图片读取上不会设置访问权限）而我也试过将微博里的图片删除，但该图片链接还是保存着；再加上新浪这家大型网络公司支持，读取速度应该不成问题。由此可见该微博图床具有很好的实用性，推荐大家使用。</p>
<p>核心代码：</p>
<p><code>require('SinaOpenApi.php');<br />
$openapi = new SinaOpenApi($source_key);<br />
$openapi-&gt;setUser($user, $passwd);</p>
<p>$time=time();<br />
$time=date("y-m-d H:i:s",$time);<br />
if(!empty($_SERVER['REMOTE_ADDR'])){<br />
$ip = $_SERVER['REMOTE_ADDR'];<br />
$upload_params = array(<br />
'status'=&gt;'IP:'.$ip.' <a href="http://iiii.sinaapp.com/?ip='">http://iiii.sinaapp.com/?ip='</a>.$ip.' Time:'.$time.' Name:'.$_FILES["file"]["name"],<br />
'pic'=&gt;$_FILES["file"]["tmp_name"],<br />
);<br />
}else{<br />
$ip = 'no';<br />
$upload_params = array(<br />
'status'=&gt;'Time:'.$time.' Name:'.$_FILES["file"]["name"].'<a href="http://malu.me/'">http://malu.me/'</a>,<br />
'pic'=&gt;$_FILES["file"]["tmp_name"],<br />
);<br />
}</p>
<p>$urls = array(<br />
array('url'=&gt;'statuses/upload','params'=&gt;$upload_params, 'method'=&gt;'POST'),<br />
);</code></p>
<p>图床地址：<a href="http://imgsina.sinaapp.com/images/" target="_blank">http://imgsina.sinaapp.com/images/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bolg.malu.me/html/2012/1851.html/feed</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>ubuntu 12.04 安装在VMware 8不能共享文件解决办法</title>
		<link>http://bolg.malu.me/html/2012/1848.html</link>
		<comments>http://bolg.malu.me/html/2012/1848.html#comments</comments>
		<pubDate>Wed, 28 Mar 2012 05:56:47 +0000</pubDate>
		<dc:creator>malu8</dc:creator>
		
		<guid isPermaLink="false">http://bolg.malu.me/html/2012/1848.html</guid>
		<description><![CDATA[Ubuntu社区已经推出了ubuntu 12.04 beat1版，最近安装在vm &#8230; <a href="http://bolg.malu.me/html/2012/1848.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a title="ubuntu 12.04 安装在VMware 8不能共享文件解决办法" href="http://bolg.malu.me/html/2012/1848.html"><img src="http://img.malu.me/bolg_img/images_2012/vmware8.jpg" alt="" /></a></p>
<p>Ubuntu社区已经推出了ubuntu 12.04 beat1版，最近安装在vmware里体验了一把。不过在设置文件共享的时候，发现ubuntu 12.04中不能正常挂载出共享目录。</p>
<p>网上查了些资料，说要安装补丁，照上面做了，在运行补丁脚本的那一步出现了问题，报错信息大概是提示我vmware-tools没有安装。可是我明明安装过了。为什么会报这个错误呢，通过查看脚本文件，发现里面有个命令我本地vmware-tools并不存在；整了好久还是不能解决。</p>
<p><span id="more-1848"></span></p>
<p>最后发现vmware workstation 8.0.2好像修复了该bug，问题是出在内核版本上，ubuntu 12.04用的是3.2.0内核，而vm 8.0.0还不支持该内核：</p>
<pre>root@malu:~# uname -a
Linux malu 3.2.0-20-generic-pae #32-Ubuntu SMP Thu Mar 22 02:43:40 UTC 2012 i686 i686 i386 GNU/Linux</pre>
<p>难道要重装VMware workstation（我的版本是8.0.0）？我可不想这么麻烦（况且还跑着另外一个Linux呢），于是找来vmware workstation 8.0.2 tools中的linux.iso来，重新安装vmware-tools：</p>
<pre>root@malu:~# cd vmware-tools-distrib/
root@malu:~/vmware-tools-distrib# ./vmware-install.pl</pre>
<p>解压后运行安装脚本就可以了。</p>
<p>回到Windows下重新添加共享目录，发现ubuntu 12.04中出现共享了，用mount查看挂载情况：</p>
<pre>.host:/ on /mnt/hgfs type vmhgfs (rw,ttl=1)</pre>
<p>至此，VMware workstation 8.0.0下安装ubuntu 12.04不能共享目录的问题解决了。</p>
<p>可以推测，在以后用新内核的linux出现后，基本都可以用该方法解决。</p>
<p>本文需要用到的linux.iso下载地址：<a href="http://115.com/file/dp7gwhkv">http://115.com/file/dp7gwhkv</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bolg.malu.me/html/2012/1848.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ubuntu 12.04 下载 (beat 1 和 Alpha 2下载)</title>
		<link>http://bolg.malu.me/html/2012/1845.html</link>
		<comments>http://bolg.malu.me/html/2012/1845.html#comments</comments>
		<pubDate>Tue, 27 Mar 2012 09:31:31 +0000</pubDate>
		<dc:creator>malu8</dc:creator>
		
		<guid isPermaLink="false">http://bolg.malu.me/html/2012/1845.html</guid>
		<description><![CDATA[Ubuntu 12.04的Alpha预测版版本已经到2，并已经提供了公开下载，U &#8230; <a href="http://bolg.malu.me/html/2012/1845.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a title="ubuntu 12.04" href="http://bolg.malu.me/html/2012/1845.html"><img src="http://img.malu.me/bolg_img/images_2012/Ubuntu_12_04_install.jpg" alt="" /></a></p>
<p>Ubuntu 12.04的Alpha预测版版本已经到2，并已经提供了公开下载，<a href="http://bolg.malu.me/html/tag/ubuntu">Ubuntu</a> 12.04(代号Precise Pangolin)是下一个LTS长期支持版本，计划于2012年的4月26日发布。</p>
<p>而时隔不久，Ubuntu 12.04的beat 1版本也放出，相比Ubuntu 12.04 Alpha 2 更新了很多，包括桌面环境Unity 和Unity-2d 、新增隐私功能、HUD功能、新的Ubuntu One 。</p>
<p><span id="more-1845"></span></p>
<ul>
<li>Unity 5.2和Unity-2d 5.4</li>
<li>新的登录界面</li>
<li>QT重构的Ubuntu One</li>
<li>NotifyOSD 背景根据桌面背景的颜色改变</li>
<li>新的Dash</li>
<li>HUD</li>
<li>隐私设置</li>
<li>常按 Win 键可以显示快捷键列表。</li>
</ul>
<p><img src="http://img.malu.me/bolg_img/images_2012/Ubuntu_12_04_n1.jpg" alt="" /></p>
<p><img src="http://img.malu.me/bolg_img/images_2012/Ubuntu_12_04_n2.jpg" alt="" /></p>
<p><img src="http://img.malu.me/bolg_img/images_2012/Ubuntu_12_04_n3.jpg" alt="" /></p>
<p>官方Beat 1 下载页面：<a href="http://cdimage.ubuntu.com/releases/12.04/beta-1/">http://cdimage.ubuntu.com/releases/12.04/beta-1/</a></p>
<p>官方Alpha 2下载页面：<a href="http://cdimage.ubuntu.com/releases/12.04/alpha-2/">http://cdimage.ubuntu.com/releases/12.04/alpha-2/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bolg.malu.me/html/2012/1845.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用PHP实现 腾讯+新浪+Twitter 三款微博同时发布展现</title>
		<link>http://bolg.malu.me/html/2012/1841.html</link>
		<comments>http://bolg.malu.me/html/2012/1841.html#comments</comments>
		<pubDate>Mon, 30 Jan 2012 04:00:30 +0000</pubDate>
		<dc:creator>malu8</dc:creator>
		
		<guid isPermaLink="false">http://bolg.malu.me/html/2012/1841.html</guid>
		<description><![CDATA[今天在首页完成了腾讯微博+新浪微博+twitter三款微博的实时更新，实现了3款 &#8230; <a href="http://bolg.malu.me/html/2012/1841.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>今天在首页完成了腾讯微博+新浪微博+twitter三款<a href="http://bolg.malu.me/html/tag/%E5%BE%AE%E5%8D%9A" target="_blank">微博</a>的实时更新，实现了3款微博能同时聚集在一起，并即时更新内容。使用了sqlite轻型数据库保存同步的数据，并用<a href="http://bolg.malu.me/html/tag/xml" target="_blank">xml</a>远程调用接口，涉及的技术下文会讲到，首先来看一下效果（<a href="http://malu.me/" target="_blank">点击进入预览</a>）：</p>
<p><a href="http://malu.me/" target="_blank"><img src="http://img.malu.me/bolg_img/images_2012/3_weibo_malu_me.jpg" alt="" /></a></p>
<p><span id="more-1841"></span></p>
<p>在本文之前我已经对各个微博的数据调用做了介绍，本文将讨论如何将3个不同微博的api数据整合到一起，并能实时更新显示：</p>
<p>原理是这样的：</p>
<p>1.首先要把获取到的微博消息汇总并保存起来，这里我用到了轻型数据库sqlite，基本语法和mysql相似，且不需要复杂的配置。</p>
<p>2.接下来用<a href="http://bolg.malu.me/html/tag/php" target="_blank">php</a>来把sqlite中的数据读取出来，并格式成xml格式，方便远程传输。（这里要说明一下，因为我服务器本身不可访问Twitter数据，所以用了国外的一台可以访问Twitter的服务器作为微博数据汇总）</p>
<p>3.负责显示微博汇总信息的页面，也就是<a href="http://malu.me/">http://malu.me/</a> ，解析出远程的xml，并输出到页面。</p>
<p>4.为了实时更新，并且不影响页面打开速度，我用了另外一个php页面来专门进行xml更新，并把获取到的数据缓存到本地，然后用计划任务定时去访问这个<a href="http://bolg.malu.me/html/tag/php" target="_blank">php</a>页面，模拟实时更新的效果。</p>
<p>根据此原理，开发思路就清晰了，下面来看一下Twitter是如何进行数据获取并写入sqlite的：</p>
<pre>//写入sqlite
function my_sqlite($type,$content,$time){

	$db = sqlite_open("php_weibo.db",0666,$sqlite_error);

        if(trim($type) != "" &amp;&amp; trim($content) != "" &amp;&amp; trim($time) != ""){

	  	$sql="INSERT INTO test (id,type, content, time) VALUES (NULL,'$type','$content',$time)";
  		$result = sqlite_exec($db,$sql);

        if($result){
            echo "据添加成功!ok!";
        }else{
            echo '据添加不成功<a href="?install=1">!error!</a>';
        }
  	}
}
//把数据库中时间统一调用到数组中
$db = sqlite_open("php_weibo.db");
$sql = "select * from test";
$query=sqlite_query($db,$sql);
while($row = sqlite_fetch_array($query)){
	$time_arr[] = $row[time];
}
$type = "twitter";
if($arr){
        foreach ($arr as $tweet) {
                $datetime = $tweet[created_at];
                $time = strtotime($datetime);
                $content = str_replace("'","''",$tweet[text]);
		if (in_array((string)$time, $time_arr, true)) {
			echo "---------未更新|不写入----------";
		}else{
			echo "$type | $content | $time ";
		my_sqlite($type,$content,$time);
		}
 	}
}else{
echo "twitter 数据获取失败！";
}</pre>
<p>以上就是Twitter微博写入sqlite的源码，腾讯微博、新浪微博与之类似。一并写入sqlite数据库php_weibo.db中。</p>
<p>然后用另外一个php读取sqlite的值，并格式成xml：</p>
<pre>//查询sqlite
$db = sqlite_open("php_weibo.db");
$sql = "select * from test order by time desc LIMIT 0 , 20";
$query=sqlite_query($db,$sql);

//建立xml
while($row = sqlite_fetch_array($query)){
	  echo '&lt;weibo type="'.$row[type].'" time="'.$row[time].'"&gt;&lt;![CDATA['.$row[content].']]&gt;&lt;/weibo&gt;';
}</pre>
<p>在输出数据时，我只调用了后20条数据，也就是显示最近更新的20条微博，这样就不用考虑哪一种微博显示出来，从而能实现3种微博混搭显示（像犀利哥）。</p>
<p>下面只要解析这xml就能显示了，为了保证xml文件的正确性，我写了一段代码进行判断，如果有故障（xml文件不完整）则发邮件通知我：</p>
<pre>$xml = new DOMDocument();
$xml-&gt;load('weibolist.xml');
foreach($xml-&gt;getElementsByTagName('weibo') as $weibo){
	$content = $weibo-&gt;firstChild-&gt;nodeValue;
	$type = $weibo-&gt;getAttribute('type');
	$time = $weibo-&gt;getAttribute('time');
	if($content){
		echo "-----------------本地XML数据 一切正常！------------------";
	}else{
		echo "---+----+---------本地XML文件有错误---------+----+---";
		if(@mail("malu@malu.me","我的首页微博的xml文件出现故障","这封邮件是马路的监控系统发出的，请不要回复.请检查http://malu.me/网站首页")){
			echo "successful send mail to malu.me!";
		}else{
			echo "error,no mail()";
		}
	}
}</pre>
<p>三款微博同时发布显示就介绍到这里，由于能力有限，文章写的还不够完善，如果有疑问或有兴趣的读者欢迎在<a href="http://bolg.malu.me/" target="_blank">陋室博客</a>里留言与我讨论。</p>
]]></content:encoded>
			<wfw:commentRss>http://bolg.malu.me/html/2012/1841.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>PHP调用Twitter微博数据json</title>
		<link>http://bolg.malu.me/html/2012/1839.html</link>
		<comments>http://bolg.malu.me/html/2012/1839.html#comments</comments>
		<pubDate>Fri, 27 Jan 2012 05:12:00 +0000</pubDate>
		<dc:creator>malu8</dc:creator>
		
		<guid isPermaLink="false">http://bolg.malu.me/html/2012/1839.html</guid>
		<description><![CDATA[前面两篇文章提到了国内两大微博的json数据调用，之所以分开写，是因为它们的调用 &#8230; <a href="http://bolg.malu.me/html/2012/1839.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://bolg.malu.me/html/2012/1839.html"><img src="http://img.malu.me/bolg_img/images_2012/new_bird_twitter.jpg" alt="" /></a></p>
<p>前面两篇文章提到了国内两大<a href="http://bolg.malu.me/html/tag/%E5%BE%AE%E5%8D%9A" target="_blank">微博</a>的json数据调用，之所以分开写，是因为它们的调用方式有点不同。今天要讲到我们最伟大的微博Twitter的json数据调用，它的数据调用却是结合了国内2大微博的优点，也可以说是国内微博模仿了Twitter。不管谁模仿谁，调用Twitter的json数据是今天的正题，下面看源码：</p>
<p>Twitter官方给出的接口调用非常简单，几乎可以10行代码搞定，不过为了更高效，我使用了缓存，方式刷新次数过猛而对数据调用产生影响：</p>
<p><span id="more-1839"></span></p>
<pre>$username = 'username';
$tweet_count = '20';
$getjson = 'https://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&amp;count=' . $count . '&amp;include_entities=1&amp;include_rts=1';
$expire = 120; //过期时间,单位秒,2分钟
$cache_file = './sqlite_twitter.dat';  //缓存文件

$arr = array();
clearstatcache();
    if (file_exists($cache_file) &amp;&amp; time() &lt;= (filemtime($cache_file) + $expire)) {
        //缓存文件存在且未过期则直接读取
        $arr = unserialize(file_get_contents($cache_file));
		echo "********缓存存在且未过期，直接读取缓存数据********";
	$timelost = $expire - (time() - filemtime($cache_file));
		echo "***********Twitter距离下次更新还剩 $timelost 秒***********";
    }else{
        //缓存文件不存在或已过期则读取json数据处理并缓存
	$content = file_get_contents($getjson); //远程调用
        $arr = json_decode($content, true);
	if($arr) {
        	file_put_contents($cache_file, serialize($arr)); //写入缓存文件
		echo "*********读取Twitter接口数据成功*********";
	}else{
		echo "**error**读取Twitter接口数据失败**error**";
        	$arr = unserialize(file_get_contents($cache_file));
		echo "******************|*******************";
		echo "******************|*******************";
		echo "******************|*******************";
		echo "******************|*******************";
		echo "******************V*******************";
		echo "*********调用缓存数据数据成功*********";
	}
    }</pre>
<p>以上代码就可以直接调用到Twitter的数据，并保存在数组$arr中，接下来只要包数组中的数据调用显示就完成了：</p>
<pre>if($arr){
        foreach ($arr as $tweet) {
                $datetime = $tweet[created_at];
                $time = strtotime($datetime);
                $content = str_replace("'","''",$tweet[text]);
		echo "$content | $time &lt;br&gt;";
	}
}else{
echo "twitter 数据获取失败！";
}</pre>
<p>通过<a href="http://bolg.malu.me/html/tag/php" target="_blank">php</a>循环输出数组，把Twitter的内容就全部的输出显示了。</p>
<p>其中$content = str_replace(&#8220;&#8216;&#8221;,&#8221;&#8221;&#8221;,$tweet[text]); 是把字符串中带单引号的字符转换成2个单引号，这是因为在写入sqlite是会用到，不写入sqlite的朋友可以把该转换去掉。</p>
<p>演示：<a href="http://malu.me/">http://malu.me/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bolg.malu.me/html/2012/1839.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>使用PHP调用新浪微博json</title>
		<link>http://bolg.malu.me/html/2012/1832.html</link>
		<comments>http://bolg.malu.me/html/2012/1832.html#comments</comments>
		<pubDate>Wed, 25 Jan 2012 05:14:35 +0000</pubDate>
		<dc:creator>malu8</dc:creator>
		
		<guid isPermaLink="false">http://bolg.malu.me/html/2012/1832.html</guid>
		<description><![CDATA[上一篇文章写到用PHP调用腾讯微博json，解释了如何用腾讯官方提供的json数 &#8230; <a href="http://bolg.malu.me/html/2012/1832.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://bolg.malu.me/html/2012/1832.html"><img src="http://img.malu.me/bolg_img/images_2012/xinglang_weibo_com.jpg" alt="" /></a></p>
<p>上一篇文章写到<a href="http://bolg.malu.me/html/2012/1826.html">用PHP调用腾讯微博json</a>，解释了如何用腾讯官方提供的json数据接口，通过<a href="http://bolg.malu.me/html/tag/php" target="_blank">php</a>来调用。这篇文章继续来讨论如何用php来调用新浪微博的json数据。</p>
<p>与腾讯微博json数据调用不同，新浪<a href="http://bolg.malu.me/html/tag/%E5%BE%AE%E5%8D%9A" target="_blank">微博</a>的json调用，需要用户认证。我们讨论最简单的认证方式，通过如下代码实现：</p>
<p><span id="more-1832"></span></p>
<pre>$user = array();
$arr = array();
$user['username'] = array('username'=&gt;'xxxxxx','pwd'=&gt;'xxxxxx','key'=&gt;'xxxxxxxxxxx',);//这里可以添加多用户调用，每行一个
$count = isset($_GET['count']) &amp;&amp; $_GET['count'] ? intval($_GET['count']) : 20;//调用条数
$count = $count ? $count : 20;

foreach($user as $u){
	$getjson = 'https://'.$u['username'].':'.$u['pwd'].'@api.weibo.com/2/statuses/user_timeline.json?source='.$u['key'].'&amp;count='.$count;//新浪api
	$content = file_get_contents($getjson); //自制函数,远程调用
	$weibo = json_decode($content, true);
	$arr = $weibo['statuses'];
}</pre>
<p>&nbsp;</p>
<p>其中username数组后面的xxxxxx用新浪微博的用户名代替，pwd数组后面的xxxxxx用登录密码代替，key后面的xxxxxxxxxx去新浪微博开放平台：http://open.weibo.com/apps 去这里建立一个应用，然后在应用里可以看到App key 获得一串数字，填入即可。</p>
<p>通过上面代码，把新浪微博的json数据处理成了数组，储存在数组变量$arr中，然后接下来的就可以对数组$arr进行调用：</p>
<pre>for($i=0;$i&lt;$count;$i++){
	$arr1 = $arr["$i"];
	if($arr1) {
		$content='&lt;a title="点击查看这条微博" href="http://weib.sinaapp.com/show/id-'.sprintf('%1.0f', $arr1['id']).'" target="_blank"&gt;'.replace_url($arr1['text']).'&lt;/a&gt;';
		echo $content.'&lt;br/&gt;';
	}
}</pre>
<p>通过循环调用数组中的数据，把每一条微博消息通过php输出到页面上。</p>
<p>新浪微博json数据调用就这么简单，剩下的工作就是结合div+<a href="http://bolg.malu.me/html/tag/css" target="_blank">css</a>给微博加上合适的样式。</p>
<p>演示：<a href="http://malu.me/">http://malu.me/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bolg.malu.me/html/2012/1832.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用PHP调用腾讯微博json</title>
		<link>http://bolg.malu.me/html/2012/1826.html</link>
		<comments>http://bolg.malu.me/html/2012/1826.html#comments</comments>
		<pubDate>Fri, 20 Jan 2012 11:06:05 +0000</pubDate>
		<dc:creator>malu8</dc:creator>
		
		<guid isPermaLink="false">http://bolg.malu.me/html/2012/1826.html</guid>
		<description><![CDATA[首先祝大家龙年快乐，高高兴兴过大年；最近闲在家里看着首页位置很空旷，想把微博消息 &#8230; <a href="http://bolg.malu.me/html/2012/1826.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>首先祝大家龙年快乐，高高兴兴过大年；最近闲在家里看着首页位置很空旷，想把微博消息直接调用到首页上去，这样以后有实时消息就可以发微博并直接在首页上看到了。经过3天的改进，首页已经可以把腾讯微博、新浪微博的广播消息同步出来。</p>
<p><a title="腾讯微博" href="http://bolg.malu.me/html/2012/1826.html"><img class="alignnone" title="腾讯微博" src="http://img.malu.me/bolg_img/images_2012/weibo_tenxun.jpg" alt="腾讯微博" width="467" height="195" /></a></p>
<p>腾讯微博官方提供了API给我们调用，我们就用它提供的json数据接口来实现微博调用：</p>
<p><span id="more-1826"></span></p>
<p>首先要用到我之前用<a href="http://bolg.malu.me/html/tag/php" target="_blank">php</a>编写的<a href="http://bolg.malu.me/html/2011/1782.html" target="_blank">远程获取类</a>：</p>
<pre>function vita_get_url_content($getjson) {
	if(function_exists('curl_init')) {
		$ch = curl_init();
		$timeout = 5;
		curl_setopt ($ch, CURLOPT_URL, $getjson);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$file_contents = curl_exec($ch);
		curl_close($ch);
	} else {
		$ctx = stream_context_create(array(
		   'http' =&gt; array(
		       'timeout' =&gt; 5 //设置一个超时时间，单位为秒
		       )
		   )
		);
		$file_contents = file_get_contents($getjson, 0,$ctx);
	}
return $file_contents;
}</pre>
<p>然后把获取到的变量通过json_decode()转换为数组：</p>
<pre>$weibo = json_decode($content, true);
$arr = $weibo['data'];</pre>
<p>然后使用下面的关键代码，进行数组处理：</p>
<pre>    if($arr) {
	    //array_reverse倒序数组
        foreach(array_reverse($arr) as $item) {
	$time=$item['timestamp'];
	if (in_array((string)$time, $time_arr, true)) {
			echo "---------未更新|不写入----------";
		}else{
			$type="tenxun";
            switch ((int)$item['type']) {
                case 1:
			$content=replace_url($item['content']);
//			my_sqlite($type,$content,$time);
                    break;
                case 2:
			$content='转播:'.replace_url($item['content']).''.$item['source']['nick'].''.replace_url($item['source']['content']);
                    break;
                case 4:
			$content=$item['nick'].' 对'.$item['source']['nick'].' 说: '.replace_url($item['content']).''.$item['source']['nick'].' '.replace_url($item['source']['content']);
                    break;
                case 5:
			$content='说:'.replace_url($item['content']);
                    break;
                default:
                    break;
            }	//switch end
			my_sqlite($type,$content,$time);
		}	//else end
        }	//forsearch end
    }else{
        echo '暂时无法读取腾讯微博数据';
    }</pre>
<p>上面函数首先对处理好的数组进行判断，如果数组正常，则对数组循环调用，把每个调用值输出到变量$item中，变量$item包含了所以需要输出的值：微博用户名、微博内容、发布的时间等。</p>
<p>再加上一些css样式处理，首页实时更新微博的功能就实现了。</p>
<p>来看一下预览效果（<a href="http://malu.me/">http://malu.me/</a> ）：</p>
<p><a href="http://malu.me/"><img src="http://img.malu.me/bolg_img/images_2012/weibo_malu.me.jpg" alt="" /></a></p>
<p>下一篇文章将介绍<a href="http://bolg.malu.me/html/2012/1832.html">新浪微博的数据处理</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://bolg.malu.me/html/2012/1826.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>PHP把时间处理成多少分钟前</title>
		<link>http://bolg.malu.me/html/2012/1815.html</link>
		<comments>http://bolg.malu.me/html/2012/1815.html#comments</comments>
		<pubDate>Wed, 11 Jan 2012 08:57:32 +0000</pubDate>
		<dc:creator>malu8</dc:creator>
		
		<guid isPermaLink="false">http://bolg.malu.me/html/2012/1815.html</guid>
		<description><![CDATA[在微博上发布消息的时候，一定会看到一种情况：某某人多少分钟前发布了一条消息： 这 &#8230; <a href="http://bolg.malu.me/html/2012/1815.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>在<a href="http://bolg.malu.me/message" target="_blank">微博</a>上发布消息的时候，一定会看到一种情况：某某人多少分钟前发布了一条消息：</p>
<p><img src="http://img.malu.me/bolg_img/images_2012/tenxunweibo_time.jpg" alt="" /></p>
<p>这种即时处理的人性化方式下面来介绍在<a href="http://bolg.malu.me/html/tag/php" target="_blank">PHP</a>下是如何实现这一功能的。</p>
<p>在看源码前，我们先要了解，在php中系统的时间函数：time() 该函数能调出当前的时间（该时间为unix时间戳，关于Unix时间戳,是从1970年0时0分0秒开始到现在的秒数）；下面还需要了解一个能处理该时间戳的函数：date() 。</p>
<p><span id="more-1815"></span></p>
<p>为了更好解释以上函数的用法，我举个例子:</p>
<pre>$time=time();
$time=date("y-m-d H:i:s",$time);  
echo $time;</pre>
<p>第一行把当前时间戳给变量$time</p>
<p>第二行用函数date()对变量$time进行处理，并把值输出到变量$time上</p>
<p>第三行输出变量$time</p>
<p>了解了以上用法，下面来看把时间处理成多少分钟前的一个类函数：</p>
<pre>function time_ago($cur_time){ 
$agoTime = time() - $cur_time; 
    if ( $agoTime &lt;= 60 ) {
        return $agoTime.'秒前';
    }elseif( $agoTime &lt;= 3600 &amp;&amp; $agoTime &gt; 60 ){
        return intval($agoTime/60) .'分钟前';
    }elseif ( date('d',$cur_time) == date('d',time()) &amp;&amp; $agoTime &gt; 3600){
	return '今天 '.date('H:i',$cur_time);
    }elseif( date('d',$cur_time+86400) == date('d',time()) &#038;&#038; $agoTime < 172800){
	return '昨天 '.date('H:i',$cur_time);
    }else{
        return date('Y年m月d日 H:i',$cur_time);
    }
}</pre>
<p>类函数的用法：</p>
<pre>$weibotime='1326168340';
echo time_ago($weibotime);</pre>
<p>变量$weibotime赋值uinx时间戳。</p>
<p>该类可以用在任意需要输出时间的地方，2个函数都为php自带函数。可以判断多少秒内，多少分钟内，对超过1小时的给出 "今天 10:39"这种类型的时间，对超过1天的给出 "昨天 3:10"类型的时间；对于超过这些时间的都以输出 "2012年1月11日 5:30" 这种类型的时间。</p>
]]></content:encoded>
			<wfw:commentRss>http://bolg.malu.me/html/2012/1815.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
