2009-10-15 62 views
0

我用下面的代碼來發布更新到Twitter:Twitter的API後被切斷

// Set username and password for twitter API 
     $username = '***'; 
     $password = '***'; 

     // The twitter API address 
     $url = 'http://twitter.com/statuses/update.xml'; 

     // Alternative JSON version 
     // $url = 'http://twitter.com/statuses/update.json'; 

     // Set up and execute the curl process 
     $curl_handle = curl_init(); 

     curl_setopt($curl_handle, CURLOPT_URL, "$url"); 
     curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 10); 
     curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($curl_handle, CURLOPT_POST, 1); 
     curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$update"); 
     curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password"); 

     $buffer = curl_exec($curl_handle); 

     curl_close($curl_handle); 

     // check for success or failure 

     if (empty($buffer)) 
     { 
      echo 'error?!'; 
     } 
     else 
     { 
      // mark the song as tweetered 
      mysql_query("UPDATE `songs` SET `tweet` = '1' WHERE `id` = $songid "); 
     } 
    } 

    echo $update; 

的崗位職能然而,它被切斷出於某種原因。

例如上面的代碼將發佈到:Tiesto - Feel It In My Bones (Feat. Tegan到twitter。

當要張貼檢查:Tiesto - Feel It In My Bones (Feat. Tegan & Sara) - 160Kbps http://bit.ly/5mdCK4

我使用echo $更新;行末比較輸出到twitter的$update值和$update是正確的。

回答

5

您是否嘗試過其他輸入字符串?從你在那裏發佈的信息來看,我敢打賭你的問題是狀態中的&符號,因爲這是表單帖子中的元字符。我相信PHP有一些URL編碼字符串的功能,您可以在$update上使用它。

+0

我正在檢查那個權利。我認爲我的代碼以前曾用過&和其他符號,但我正在瀏覽所有帖子。 – ian 2009-10-15 14:45:16

+0

這就是它將重新發布如何正確編碼和感謝。 – ian 2009-10-15 14:46:40

+0

爲什麼轉發?這裏回答。 – ceejayoz 2009-10-15 14:59:34

1

Sixten Otto是對的。你需要做的:

$update =urlencode($update);

,以防止問題有特殊字符。

+0

哦,嘿,它甚至被命名爲合適的東西! :-) – 2009-10-15 15:37:07