2010-03-25 24 views
0
<?php 
function updateTwitter($status) 
{ 
    // Twitter login information 
    $username = 'xxxxx'; 
    $password = 'xxxxxx'; 
    // The url of the update function 
    $url = 'http://twitter.com/statuses/update.xml'; 
    // Arguments we are posting to Twitter 
    $postargs = 'status='.urlencode($status); 
    // Will store the response we get from Twitter 
    $responseInfo=array(); 
    // Initialize CURL 
    $ch = curl_init($url); 
    // Tell CURL we are doing a POST 
    curl_setopt ($ch, CURLOPT_POST, true); 
    // Give CURL the arguments in the POST 
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs); 
    // Set the username and password in the CURL call 
    curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password); 
    // Set some cur flags (not too important) 
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 
    curl_setopt($ch, CURLOPT_NOBODY, 0); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    // execute the CURL call 
    $response = curl_exec($ch); 
    // Get information about the response 
    $responseInfo=curl_getinfo($ch); 
    // Close the CURL connection curl_close($ch); 
    // Make sure we received a response from Twitter 
    if(intval($responseInfo['http_code'])==200){ 
     // Display the response from Twitter 
     echo $response; 
    }else{ 
     // Something went wrong 
     echo "Error: " . $responseInfo['http_code']; 
    } 
} 

updateTwitter("Just finished a sweet tutorial on http://brandontreb.com"); 

?> 

我得到以下輸出樣品Twitter應用

Error: 0 

請幫助。

回答

1

libcurl documentation說HTTP_CODE被設置爲0失敗(無服務器響應代碼)。調用curl_getinfo之前,您應該檢查response,並調用curl_error如果是假的。另外,在responseInfo中存儲空數組沒有意義。您可以將其設置爲null。

+0

響應是假,我碰到下面的錯誤; curl錯誤:無法連接到主機 – Bruce 2010-03-25 07:10:34

+0

我再添加一行代碼 curl_setopt($ ch,CURLOPT_PROXY,「localhost:80」); 現在,我得到錯誤:404 – Bruce 2010-03-25 07:51:47

+0

我刪除了這句話,現在使用的網絡託管服務來運行我的代碼,我收到以下錯誤捲曲錯誤:無法解析主機「api.twitter.com」錯誤:0 – Bruce 2010-03-26 09:37:32

0

可能是一個錯誤或者他忘了從崗位消滅它,但curl_close($ CH)永遠不會被調用。也許這是什麼阻止了迴應?當我回家時我會測試更多。

+0

不,缺少'curl_close'只會導致資源泄漏。當設置'CURLOPT_RETURNTRANSFER'時,應該從'curl_exec'返回響應。 – 2010-03-27 15:15:47