0

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`
}
AndFisher
  • 633
  • 1
  • 5
  • 18
  • **[You should not switch off `CURLOPT_SSL_VERIFYHOST` or `CURLOPT_SSL_VERIFYPEER`](https://paragonie.com/blog/2017/10/certainty-automated-cacert-pem-management-for-php-software)**. It could be a security risk! [Here is how to get the certificate bundle if your server is missing one](https://stackoverflow.com/a/32095378/1839439) – Dharman Oct 02 '19 at 22:38
  • You seem to have neglected in showing us ( and more importantly yourself) what $fullURL is when run on your local system... Somewhere just before you use it, a var_dump($fullURL); will work for us all :) – TimBrownlaw Oct 03 '19 at 00:07
  • @Dharman, it is not a security risk, since it is only deployed locally on local URLs – AndFisher Oct 03 '19 at 08:26
  • @TimBrownlaw I've added an example url but I have already checked these both in debug and manual requests. – AndFisher Oct 03 '19 at 08:35
  • I've added further details, the fact that as soon as the `CI_controller` construct is called the problem appears leads me to believe it is CodeIgniter related. – AndFisher Oct 03 '19 at 08:36
  • So what do you get for $fullURL. I am thinking config setting or something. – TimBrownlaw Oct 03 '19 at 08:45
  • For example; `http://localhost/andrew/index`, which resolves via curl if the parent constructor is overriden, so I don't think it's a connection issue – AndFisher Oct 03 '19 at 08:46
  • 1
    Your simple controller tests prove nothing about the viability of the cURL request. Try `class Andrew extends CI_Controller { public function index() { echo 'hello world'; } }` and see what happens. – DFriend Oct 04 '19 at 00:53
  • @DFriend I have already tried this, which is why it is in the question – AndFisher Oct 04 '19 at 11:31
  • @AndFisher, In the question you're using `die` not `echo`. – DFriend Oct 04 '19 at 13:38
  • Your **EDIT** comment isn't clear to me, When and where does loading a config file happen? There's nothing in your question showing that kind of call. – DFriend Oct 04 '19 at 13:43
  • It is happening CI_Controller::__construct – AndFisher Oct 04 '19 at 15:52

0 Answers0