2012-08-12 23 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對象。

相關問題