I have recently upgraded an application and environment from PHP5 to PHP7 and Codeigniter 2 to Codeigniter 3. However the application needs to curl to itself, and what used to work now gives me the following error:
Operation timed out after 30000 milliseconds with 0 bytes received
Here's the code, which I have tried on an external URL and works fine.
$fullURL = 'http://localhost/andrew/index';
// create curl resource
$ch = curl_init();
$curlOpts = [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_COOKIE => session_name() . '=' . session_id(),
CURLOPT_COOKIESESSION => true,
CURLOPT_REFERER => $domain.$_SERVER['REQUEST_URI'],
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'], // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => $timeout, // time-out on connect
CURLOPT_TIMEOUT => $timeout, // time-out on response,
CURLOPT_HTTPHEADER => [],
CURLOPT_SSL_VERIFYSTATUS => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSLVERSION => 3,
CURLOPT_URL => $fullURL,
];
if ($ajax) {
$curlOpts[CURLOPT_HTTPHEADER][] = 'X-Requested-With: XMLHttpRequest';
}
curl_setopt_array($ch, $curlOpts);
// $output contains the output string
$output = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$errors = curl_error($ch);
// close curl resource to free up system resources
curl_close($ch);
I assumed it was an application problem, so I made the most basic of controllers to also test:
<?php
class Andrew extends CI_Controller
{
public function index()
{
die ('hello world');
}
}
...but had the same problem.
However.
If I override the constructor and die before it calls its parent constructor the problem goes away
class Andrew extends CI_Controller
{
public function __construct()
{
die('test'); // <-- cURL request resolves fine with this, $output = 'test'
parent::__construct();
}
public function index()
{
die ('hello world');
}
}
The fact that as soon as the CI_controller
construct is called the problem appears leads me to believe it is CodeIgniter related.
How do I resolve this?
EDIT =============
In digging, what I have found is that an Exception is thrown in CI_Loader
. When a cURL request, get_instance()->config
is a stdClass
rather than an instance of CI_Config
like it is in a regular request and so has no load()
method.
public function config($file, $use_sections = FALSE, $fail_gracefully = FALSE)
{
return get_instance()->config->load($file, $use_sections, $fail_gracefully); // <-- When a cURL request, get_instance()->config is a stdClass rather than an instance of `CI_Config`
}