SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cURL - HTTP Not Responding

    Hi folks,
    I have cURL running some calls for the Twitter API, and something isn't working locally. I've ran the same code on my own server, and on a HostGator server, so this isn't a Twitter problem, it's something wrong with my server, I just can't work out what! The problem is, that nothing at all is being returned to me when I run a call. It actually carried out the XML request, as the new Tweet appears in my stream, but doesn't call back to me. This is an example of how it will call back:
    Code:
    Array
    (
        [url] => http://twitter.com/statuses/update.xml?status=Watching+iPhone+OS3.0+Preview+for+second+time.+%2Ageek%2A
        [http_code] => 0
        [header_size] => 0
        [request_size] => 254
        [filetime] => -1
        [ssl_verify_result] => 0
        [redirect_count] => 0
        [total_time] => 5.243655
        [namelookup_time] => 5.053544
        [connect_time] => 5.243596
        [pretransfer_time] => 5.243602
        [size_upload] => 0
        [size_download] => 0
        [speed_download] => 0
        [speed_upload] => 0
        [download_content_length] => 0
        [upload_content_length] => -1
        [starttransfer_time] => 5.243652
        [redirect_time] => 0
    )
    Any ideas?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you post your cURL implementation?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure:
    PHP Code:
        $host "http://twitter.com/statuses/update.xml?status=".urlencode(stripslashes(urldecode($message)));

        
    $ch curl_init();
        
    curl_setopt($chCURLOPT_URL$host);
        
    curl_setopt($chCURLOPT_VERBOSE1);
        
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
        
    curl_setopt($chCURLOPT_USERPWD"$username:$password");
        
    curl_setopt($chCURLOPT_HTTP_VERSIONCURL_HTTP_VERSION_1_1);
        
    curl_setopt($chCURLOPT_POST1);

        
    $result curl_exec($ch);
        
    // Look at the returned header
        
    $resultArray curl_getinfo($ch);
        echo 
    '<pre>';
        
    print_r($resultArray);
        echo 
    '</pre>';
        if(
    $resultArray['http_code'] == "200"){
             
    $twitter_status='Your message has been sended! <a href="http://twitter.com/'.$username.'">See your profile</a>';
        } else {
             
    $twitter_status="Error posting to Twitter. Retry";
        }
        return 
    $twitter_status;
        
    curl_close($ch); 

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    From what I can see, you don't use the returned data which should be contained in the $result var in your implementation.

    On a side note, you return before you close your cURL handle, so the close request would never be actioned.

    You don't appear to following the API, of course I maybe wrong!

    My interpretation would be...

    PHP Code:
    <?php
    function updateTwitterStatus($sMessage$sUsername$sPassword)
    {
        
    $rCurl curl_init('http://twitter.com/statuses/update.xml');
        
        
    $aPostData = array(
            
    'status'    =>    urlencode($sMessage)
        );
        
        
    $aCurlOptions = array(
            
    CURLOPT_VERBOSE            =>    true,
            
    CURLOPT_RETURNTRANSFER    =>    true,
            
    CURLOPT_POST            =>    true,
            
    CURLOPT_POSTFIELDS        =>    http_build_query($aPostData),
            
    CURLOPT_USERPWD            =>    sprintf('%s:%s'$sUsername$sPassword)
        );
        
        
    $aReturn = array(
            
    'response'    =>    curl_exec($rCurl),
            
    'info'        =>    curl_getinfo($rCurl)
        );
        
        
    curl_close($rCurl);
        
        return 
    $aReturn;
    }

    $aTwitterUpdate updateTwitterStatus('Alright!''anthonysterling''password');

    echo (
    $aTwitterUpdate['info']['http_code'] == 200) ? 'Success!' 'Boo, failure awaits you.' ;
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How does that all work! I can't see where the array's are getting called in by the cURL at all. In a similar vain, my password doesn't seem to authenticate! I do know it's correct, as I just logged into Twitter with it!

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Eeek! It's because the arrays aren't set.

    My bad.

    PHP Code:
    <?php
    function updateTwitterStatus($sMessage$sUsername$sPassword)
    {
        
    #initialise curl handle
        
    $rCurl curl_init('http://twitter.com/statuses/update.xml');
        
        
    #data to send to twitter
        
    $aPostData = array(
            
    'status'    =>    urlencode($sMessage)
        );
        
        
    #curl options
        
    $aCurlOptions = array(
            
    CURLOPT_VERBOSE            =>    true,
            
    CURLOPT_RETURNTRANSFER    =>    true,
            
    CURLOPT_POST            =>    true,
            
    CURLOPT_POSTFIELDS        =>    http_build_query($aPostData),
            
    CURLOPT_USERPWD            =>    sprintf('%s:%s'$sUsername$sPassword)
        );
        
        
    #apply curl options
        
    curl_setopt_array($rCurl$aCurlOptions);
        
        
    #build return values
        
    $aReturn = array(
            
    'response'    =>    curl_exec($rCurl),
            
    'info'        =>    curl_getinfo($rCurl)
        );
        
        
    #close curl handle
        
    curl_close($rCurl);
        
        
    #return
        
    return $aReturn;
    }

    $aTwitterUpdate updateTwitterStatus('Alright!''anthonysterling''password');

    echo (
    $aTwitterUpdate['info']['http_code'] == 200) ? 'Success!' 'Boo, failure awaits you.' ;
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank's very much. Also goes back and re-reads Twitter API.

  8. #8
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, either I've managed to mess something up, or there's something wrong on Twitter's end, but anything I pass is still URL encoded! Obviously, I can't give this to Twitter in any way other than URL encoded, so I have no idea!

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Try the following Adam.

    PHP Code:
    <?php
    function updateTwitterStatus($sMessage$sUsername$sPassword)
    {
        
    #initialise curl handle
        
    $rCurl curl_init('http://twitter.com/statuses/update.xml');
        
        
    #curl options
        
    $aCurlOptions = array(
            
    CURLOPT_VERBOSE            =>    true,
            
    CURLOPT_RETURNTRANSFER    =>    true,
            
    CURLOPT_POST            =>    true,
            
    CURLOPT_POSTFIELDS        =>    http_build_query(array('status' => $sMessage)),
            
    CURLOPT_USERPWD            =>    sprintf('%s:%s'$sUsername$sPassword)
        );
        
        
    #apply curl options
        
    curl_setopt_array($rCurl$aCurlOptions);
        
        
    #build return values
        
    $aReturn = array(
            
    'response'    =>    curl_exec($rCurl),
            
    'info'        =>    curl_getinfo($rCurl)
        );
        
        
    #close curl handle
        
    curl_close($rCurl);
        
        
    #return
        
    return $aReturn;
    }

    $aTwitterUpdate updateTwitterStatus('Alright!''anthonysterling''password');

    echo (
    $aTwitterUpdate['info']['http_code'] == 200) ? 'Success!' 'Boo, failure awaits you.' ;
    ?>
    It's my suspicion that http_build_query also adds urlencoding after we manually do it, which makes sense I guess!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You were right ;-)

    Thanks very much for your help with this whole situation.

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Great news!

    I guess the lesson to both of us is to read the provided documentation.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  12. #12
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    It might also be useful to note that the CURLOPT_POSTFIELDS option will happily accept an associative array.

    Also there are a whole bunch of CURLE_* constants which are returned from curl_errno() and the more descriptive curl_error() function; one or both of which can be used to see if an error occurred and what that error was.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •