cURL dependant scripts running indefinitely

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I've had a couple of issues with scripts that depend on cURL hanging long enough that my host has had to kill the PHP processes and tell me about it. The first time it happened was with a script that was run via CRON job and simply posted something to Twitter every day. The most recent time though, was with a script that has an HTML user interface and requests images from a couple of servers in the background.

Luckily both times it's been something I can just disable and not worry about.

We're pretty sure it has something to do with my lazy cURL class and how I depended on default timeouts being present. So for debugging sake, I think I'll post my cURL class here, then fix it and post the result in a later post.

PHP Code: [ Select ]
<?php
 
class cURL
{
   protected $conn;
   public $opts = array(
      CURLOPT_HEADER       => 0,
      CURLOPT_RETURNTRANSFER  => 1,
      CURLOPT_FOLLOWLOCATION  => 1,
      CURLOPT_NOBODY       => 0
   );
   public $http_status;
   public function __construct($opts = array())
   {
      $this->conn = curl_init();
      foreach($opts as $key => $val)
      {
         $this->opts[$key] = $val;
      }
      curl_setopt_array($this->conn, $this->opts);
 
      return $this;
   }
   public function get($url, $opts = array())
   {
      curl_setopt($this->conn, CURLOPT_URL, $url);
      foreach($opts as $key => $val)
      {
         $this->opts[$key] = $val;
         curl_setopt($this->conn, $key, $val);
      }
      $returns = curl_exec($this->conn);
      $this->http_status = curl_getinfo($this->conn, CURLINFO_HTTP_CODE);
      return $returns;
   }
   public function post($url, $data)
   {
      return $this->get($url, array(
         CURLOPT_POST         => 1,
         CURLOPT_POSTFIELDS   => $data
      ));
   }
}
 
?>
  1. <?php
  2.  
  3. class cURL
  4. {
  5.    protected $conn;
  6.    public $opts = array(
  7.       CURLOPT_HEADER       => 0,
  8.       CURLOPT_RETURNTRANSFER  => 1,
  9.       CURLOPT_FOLLOWLOCATION  => 1,
  10.       CURLOPT_NOBODY       => 0
  11.    );
  12.    public $http_status;
  13.    public function __construct($opts = array())
  14.    {
  15.       $this->conn = curl_init();
  16.       foreach($opts as $key => $val)
  17.       {
  18.          $this->opts[$key] = $val;
  19.       }
  20.       curl_setopt_array($this->conn, $this->opts);
  21.  
  22.       return $this;
  23.    }
  24.    public function get($url, $opts = array())
  25.    {
  26.       curl_setopt($this->conn, CURLOPT_URL, $url);
  27.       foreach($opts as $key => $val)
  28.       {
  29.          $this->opts[$key] = $val;
  30.          curl_setopt($this->conn, $key, $val);
  31.       }
  32.       $returns = curl_exec($this->conn);
  33.       $this->http_status = curl_getinfo($this->conn, CURLINFO_HTTP_CODE);
  34.       return $returns;
  35.    }
  36.    public function post($url, $data)
  37.    {
  38.       return $this->get($url, array(
  39.          CURLOPT_POST         => 1,
  40.          CURLOPT_POSTFIELDS   => $data
  41.       ));
  42.    }
  43. }
  44.  
  45. ?>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

It looks like I should be good to go by simply adding these options to my default set of options. (options manual page)

Code: [ Select ]
CURLOPT_FORBID_REUSE => true,
CURLOPT_CONNECTTIMEOUT    => 3,
CURLOPT_LOW_SPEED_LIMIT    => 1024,
CURLOPT_LOW_SPEED_TIME    => 5,
CURLOPT_MAXREDIRS            => 10,
CURLOPT_TIMEOUT                => 30
  1. CURLOPT_FORBID_REUSE => true,
  2. CURLOPT_CONNECTTIMEOUT    => 3,
  3. CURLOPT_LOW_SPEED_LIMIT    => 1024,
  4. CURLOPT_LOW_SPEED_TIME    => 5,
  5. CURLOPT_MAXREDIRS            => 10,
  6. CURLOPT_TIMEOUT                => 30


I'm a little unsure about that LOW_SPEED pair though.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 78 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.