Code snippets for symfony 1.x

Navigation

How to create a cacheable xml-rpc request

Recently I needed to use the xml-rpc lib with symfony. I created a function to make cache of xml-rpc requests using the symfony's sfFileCache and it work good.

I'm using the xml-rpc library http://phpxmlrpc.sourceforge.net/

public function callCache( $methodName, $params ){
 
  // generated a key to cache file
  $key = $methodName."_".implode('_', $params->getval() );
 
 
  $cache = new sfFileCache( array( 'cache_dir' => sfConfig::get('sf_cache_dir')."/xmlrpc/" ) );
 
  // check if already exist the cache file 
 
  if( ! $cache->has( $key ) ){
 
    //init xml-rpc message/
 
    $f = new xmlrpcmsg($methodName, $params);
 
    $f->addParam($params);
 
    $c = new xmlrpc_client("/xml-rpc", $this->host, 443, 'https' );
 
    $c->setCredentials($this->username, $this->password);
 
    $c->setDebug( $this->debug );
 
 
    $response = $c->send($f);
 
 
    $cache->set( $key , serialize( $response ), 3600);
 
 
  }else{
 
 
    $response = $cache->get( $key );
 
    $response = unserialize( $response );
 
 
  }
 
 
  if( ! $response->value() ){
 
    $this->erro( $response->errno, $response->faultString() );
 
  }
 
 
  $results = php_xmlrpc_decode( $response->value() );
 
 
  $this->rows = count($results);
 
 
  $this->data = $results;
 
}
 
by nei santos on 2009-07-02, tagged cache  xmlrpc