开放的编程资料库

当前位置:我爱分享网 > PHP教程 > 正文

使用 PHP 测试 Twitter 友谊

Twitter 提供的另一个很棒的功能是能够测试两个人之间的友谊。我的意思是测试两个人是否互相关注,而不是一个人是否用垃圾推文向世界发送垃圾邮件。与所有其他 Twitter API 调用一样,使用 PHP 可以轻松执行此测试。

PHP

/* persons to test */
$person1 = 'davidwalshblog';
$person2 = 'mootools';

/* send request to twitter */
$url = 'http://twitter.com/friendships/exists';
$format = 'xml';

/* check */
$persons12 = make_request($url.'.'.$format.'?user_a='.$person1.'&user_b='.$person2);
$result = get_match('/<friends>(.*)<\/friends>/isU',$persons12);
echo '<pre>Am I friends with @mootools?'.$result.'</pre>'; // returns "true" or "false"

/* makes the request */
function make_request($url) {
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}

/* gets the match */
function get_match($regex,$content)
{
	preg_match($regex,$content,$matches);
	return $matches[1];
}

就是这样。由于返回的 XML 很小,我没有在示例中使用 SimpleXML。太多的额外开销。这是 XML 示例响应:

<friends>true</friends>

Twitter API 很有趣,不是吗?

未经允许不得转载:我爱分享网 » 使用 PHP 测试 Twitter 友谊

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏