示例Twitter应用程序

时间:2010-03-25 06:57:11

标签: php twitter

<?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");

?>  

我得到以下输出

Error: 0 

请帮忙。

2 个答案:

答案 0 :(得分:1)

libcurl documentation表示http_code在失败时设置为0(无服务器响应代码)。您应该在致电response之前检查curl_getinfo,如果错误则调用curl_error。此外,在responseInfo中存储空数组没有意义。您可以将其设置为null。

答案 1 :(得分:0)

可能是一个错误,或者他忘了从帖子中删除它,但是curl_close($ ch)永远不会被调用。也许这就是阻碍回应的原因?我回家后会测试更多。