SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cURL Loop Leaving Connections Open on Apache

    Hello Guys,

    My host is getting mad at me because they say I have "over 80 open connections" to the IP address that is being used in my cURL application.

    my cURL loop, which runs about 25 times a page load, then refresh's itself 30 seconds later, uses both curl_init($ch) and curl_close($ch).

    Is there a reason why cURL is not closing the connection? Or would it be better to remove curl_close($ch) and leave the (Handler?) open.

    I don't seem to understand why it is creating a connection on apache and if so why it is keeping it open once I close it and the page closes.


    Any insight please?
    BKerr

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    private function makeRequest($path){                    
                    
    //Use cURL
                    
                    
    $ch curl_init($path);
                    
    curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
                    
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
                    
    curl_setopt($chCURLOPT_HEADERfalse);
                    
    curl_setopt($chCURLOPT_FORBID_REUSEtrue);
                    
    curl_setopt($chCURLOPT_MAXCONNECTS1);
                    
    $result curl_exec($ch);
                    
                    if(!
    $path || (isset($ch) && curl_getinfo($chCURLINFO_HTTP_CODE) != 200)){
                    
    //echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
                    
    throw new Exception('transfer failed');
                }

                
    curl_close($ch);
                                
                
    $result json_decode($result);
                if(!
    $result){
                    throw new 
    Exception('json_decode failed');
                }
                    
                return 
    $result;
            } 
    BKerr

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Try adding

    PHP Code:
    curl_setopt(CURLOPT_FORBID_REUSEtrue);
    curl_setopt(CURLOPT_HTTPHEADER, array('Connection: close')); 
    Why do you need 25 curl connections in one request btw? Seems a bit extensive (depending on what you're doing, of course). Any chance you could reduce that number, or maybe put some caching in place somewhere?

    PS. Have a look at curl_setopt_array; it's a lot nicer than curl_setopt if have you have a lot of options to set (IMHO).
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Connections will take time to close, regardless of whether you explicitly close them or not. It's part of the negotiating process.

    That said, why do you open 25 curl connections per page load???? Definitely agree with Scallio, caching will save you (and your host) a lot of headaches...
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because it is a site that connects to the Bungie API loading Halo: Game Stats from there site.

    Each page loads 25 games, which need to be requested one a time.

    Cache is not an option.

    But no one even touched on the issue here?
    BKerr

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    well it sounds like their servers are terribly slow. have you tested that?
    If that's the case there is nothing you can do. well, except for caching, but you said you don't want that.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •