2012-08-12 46 views
0

我想用cURL阅读twitter时间表,出于某种原因我无法使用preg_match。这是我目前的代码,你有没有看到任何问题?如何从用户的Twitter时间表中读取所有<status>标签?

$feed = "http://twitter.com/statuses/user_timeline/antonpug.xml?count=3"; 

function parse_feed($feed) { 
    //$matches = Array(); 
    preg_match_all("/<status>(.*?)<\/status>/", $content[0], $matches); 

    return $matches[0]; 

    //$stepOne = explode("<content type=\"html\">", $feed); 
    //$stepTwo = explode("</content>", $stepOne[1]); 
    //$tweet = $stepTwo[0]; 
    //$tweet = htmlspecialchars_decode($tweet,ENT_QUOTES); 
    //return $tweet; 
} 

//Initialize the Curl session 
$ch = curl_init(); 
//Set curl to return the data instead of printing it to the browser. 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
//Set the URL 
curl_setopt($ch, CURLOPT_URL, $feed); 
//Execute the fetch 
$twitterFeed = curl_exec($ch); 
//Close the connection 
curl_close($ch); 

//$twitterFeed = file_get_contents($feed); 
echo(parse_feed($twitterFeed)); 
+0

请不要使用正则表达式解析HTML/XML,因为它会[驱动你į̷̷͚̤̤̖̦͍͗̒̈̅̄n̨͖͓̹͍͎͔͈̝͐ͪ͛̄͛ṣ̷̵̞̦ͤ̅̉̋ͪ͑͛ͥ͜a̷̘͖̮͔͎͛̇̏̒͆̆͘n͇͔̤̼͙̩͖̭ͤ͋̉͌͟eͥ͒͆ͧͨ̽͞҉̹͍̳̻͢](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)。改为使用[HTML/XML解析器](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php)。 – 2012-08-12 21:17:20

回答

0

我想更好的主意是使用simplexml与对象一起使用XML。 然后你的功能就像

function parse_feed($feed) { 
    $xml = simplexml_load_string($feed); 
    if(isset($xml->status)) { 
     return $xml->xpath('status'); 
    } else { 
     return false; 
    } 
} 

它会返回simplexml对象。

相关问题