• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I cannot cURL to local URLs in CodeIgniter 3

#1
I have recently upgraded an application and environment from PHP5 to PHP7 and Codeigniter 2 to Codeigniter 3.1.11. However the application needs to curl to itself, and what used to work now gives me the following error:

Quote: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.
Code:
    $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:
Code:
<?php

class Andrew extends CI_Controller
{
    public function index()
    {
        echo '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

Code:
class Andrew extends CI_Controller
{
    public function __construct()
    {
        die('test'); // <-- cURL request resolves fine with this, $output = 'test'
        parent::__construct();
    }
    public function index()
    {
        echo '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?

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.

Code:
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`
}
Reply

#2
What is the exception that CI_Loader throws?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.