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
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
));
}
}
?>
- <?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
- ));
- }
- }
-
- ?>
Strong with this one, the sudo is.