2
votes

I want to cache HttpSocket->post calling in CakePHP 2.

I'm calling the following code:

$content = Cache::remember($key, function () use ($rateUrl, $xml, $uid) {
    App::uses('HttpSocket', 'Network/Http');
    $HttpSocket = new HttpSocket(array('ssl_verify_peer' => false));
    return $HttpSocket->post($rateUrl, $xml->asXML(), array(
        'header' => array(
            'Content-Type' => 'text/xml',
            'RestUid' => $uid,
        )
    ));
});

But I'm getting this error:

The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "HttpSocketResponse" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition

Do anybody know the way how to use the HttpSocket class in an anonymous function or call in CakePHP or otherwise?

1

1 Answers

0
votes

As the error message says, you have to

[...] ensure that the class definition "HttpSocketResponse" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition

The class definition for the serilaized object of type HttpSocketResponse isn't available when it has been deserialized, so things won't work. Either add a corresponding App::uses statement before invoking Cache::remember(), so that CakePHPs autoloader can load the required class definition

App::uses('HttpSocketResponse', 'Network/Http');

$content = Cache::remember(/* ... */);

or switch to using Composer, so that the required class can be autoloaded without any App::uses() calls. Alternatively it's also possible to use the unserialize_callback_func ini directive to define a callback that loads the required class definition.

See also