2017-10-06 133 views
2

我想緩存HttpSocket->post調用的CakePHP 2如何使用緩存::與HttpSocket-記>後的CakePHP

我打電話以下代碼:

$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, 
     ) 
    )); 
}); 

但我發現了這個錯誤:

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

不要任何人知道的方式如何使用HttpSocket類的匿名函數或致電CakePHP中或以其他方式?

回答

0

由於錯誤消息說,你必須

[...] 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

類定義爲HttpSocketResponse類型的serilaized對象不可用時,它已經被反序列化,這樣的事情就不會工作。或者調用Cache::remember()之前添加對應App::uses語句,以便CakePHPs自動加載機可以加載所需的類定義

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

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

或切換到使用作曲家,使得所需的類可以沒有任何App::uses()呼叫自動載入。或者,也可以使用unserialize_callback_func ini指令來定義加載所需類定義的回調。

+0

我嘗試這樣做,但didn't看到工作。 –