tags:

views:

60

answers:

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

?>  

I get the following output

Error: 0 

Please help.

+1  A: 

The libcurl documentation says http_code is set to 0 on failure (no server response code). You should check response before calling curl_getinfo, and call curl_error if it is FALSE. Also, there's no point in storing an empty array in responseInfo. You can set it to null instead.

Matthew Flaschen
response is FALSE and I get the following error;Curl error: couldn't connect to host
Bruce
I added one more line to the codecurl_setopt($ch, CURLOPT_PROXY,"localhost:80");Now I get Error:404
Bruce
I removed that statement and now used a web hosting service to run my code and I get the following error Curl error: Couldn't resolve host 'api.twitter.com' Error: 0
Bruce
Sounds like you are having connection issues. Check your firewall settings and try pinging twitter.com from a command line.
abraham
A: 

Might be a mistake or he forgot to eliminate it from the post, but curl_close($ch) is never called. Maybe that is what is holding up the response? I'll test more when I get home.

mjboggess
No, missing `curl_close` will just cause a resource leak. when `CURLOPT_RETURNTRANSFER` is set, the response should be returned from `curl_exec`.
Matthew Flaschen