使用SimpleXML获取twitter头像网址(怪异)

mrp*_*atg 1 php xml twitter

我使用simplexml从xml状态页面获取twitter个人资料头像网址.

这是我正在使用的代码

<?
$username = twitter;
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml");
echo $xml->user->profile_image_url;
?>
Run Code Online (Sandbox Code Playgroud)

当我访问它时加载xml页面,但由于某种原因没有任何回应.没有错误.没有.

当我在浏览器中访问它时,我得到了这个:

<?xml version="1.0" encoding="UTF-8"?>
<user>
  <id>783214</id>
  <name>Twitter</name>
  <screen_name>twitter</screen_name>
  <location>San Francisco, CA</location>
  <description>Always wondering what everyone's doing.</description>    
  <profile_image_url>http://a1.twimg.com/profile_images/75075164/twitter_bird_profile_normal.png</profile_image_url>
  <url>http://twitter.com</url>.....
  (the rest is long and irrelevant to the question)
Run Code Online (Sandbox Code Playgroud)

数据在那里,为什么不回声呢?

Fer*_*yer 6

加载XML文档后,根元素userSimpleXMLElement保存的对象表示$xml.因此$xml->user不存在.

这应该工作:

<?
$username = "twitter";  // <-- You did not use quotes here?! Typo?
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml");
echo $xml->profile_image_url;  // <-- No $xml->user here!
?>
Run Code Online (Sandbox Code Playgroud)