PHP cURL获取最后一条Tweet

PHP cURL获取最后一条Tweet,php,twitter,curl,Php,Twitter,Curl,我基本上是想在我的推特页面上获取最后一条推特,并将其回显出来。我遇到了这样一个代码,它表面上应该做我想做的事情,但它给了我一个错误: $user = 'TwitterUserName'; $pass = 'password'; $tweet = curl_init("https://twitter.com/statuses/user_timeline.xml"); curl_setopt($tweet, CURLOPT_USERPWD, $user.':'.$pass); curl_seto

我基本上是想在我的推特页面上获取最后一条推特,并将其回显出来。我遇到了这样一个代码,它表面上应该做我想做的事情,但它给了我一个错误:

$user = 'TwitterUserName';
$pass = 'password';

$tweet = curl_init("https://twitter.com/statuses/user_timeline.xml");

curl_setopt($tweet, CURLOPT_USERPWD, $user.':'.$pass);
curl_setopt($tweet, CURLOPT_RETURNTRANSFER,1);

$out = curl_exec($tweet);

$out = new SimpleXMLElement($out);

echo $out->status[0]->text;
错误:

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in H:\xampp\htdocs\tests\twit.php:13 Stack trace: #0 H:\xampp\htdocs\tests\twit.php(13): SimpleXMLElement->__construct('') #1 {main} thrown in H:\xampp\htdocs\tests\twit.php on line 13
你知道是什么导致了这个错误吗?或者有没有更好的方法


谢谢。

返回的XML字符串无效。验证获取的字符串实际上是XML(并且有效!)。

不能对twitter的RESTAPI使用basic auth,必须对其API使用OAuth。有关twitter oauth上的一些基本资源:

由于您使用的是PHP,我建议您看看abraham的oauth库,它是第一个被广泛使用的库:但是对于许多不同的语言,有许多非常有用的库:

下面是一个关于从basic auth转换到oauth的好文档:

除此之外,我相信JSON已经成为API响应更广泛使用的标准。只要您的PHP版本具有所有JSON函数(>=5.2.0,IIRC),那么我会将您的url从*.xml更改为*.JSON