XmlRpcClient.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Anton E. Lebedevich                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: XmlRpcClient.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class XmlRpcClient
00017     {
00018         private $url        = null;
00019         private $timeout    = null;
00020         
00021         public function __construct($url = null)
00022         {
00023             $this->url = $url;
00024         }
00025         
00029         public static function create($url = null)
00030         {
00031             return new self($url);
00032         }
00033         
00034         public function getUrl()
00035         {
00036             return $this->url;
00037         }
00038         
00042         public function setUrl($url)
00043         {
00044             $this->url = $url;
00045             return $this;
00046         }
00047         
00048         public function getTimeout()
00049         {
00050             return $this->timeout;
00051         }
00052         
00056         public function setTimeout($timeout)
00057         {
00058             $this->timeout = $timeout;
00059             return $this;
00060         }
00061         
00062         public function query($method, $parameters = null)
00063         {
00064             $request = xmlrpc_encode_request($method, $parameters);
00065             
00066             $headers = array(
00067                 "Content-type: text/xml",
00068                 "Content-length: ".strlen($request)
00069             );
00070             
00071             $curl = curl_init();
00072             curl_setopt($curl, CURLOPT_URL, $this->url);
00073             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
00074             curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
00075             curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
00076             
00077             if ($this->timeout)
00078                 curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
00079             
00080             $rawResponse = curl_exec($curl);
00081             $curlErrno = curl_errno($curl);
00082             $curlError = curl_error($curl);
00083             
00084             curl_close($curl);
00085             
00086             if ($curlErrno)
00087                 throw new NetworkException($curlError, $curlErrno);
00088                 
00089             $result = xmlrpc_decode($rawResponse);
00090             
00091             if (xmlrpc_is_fault($result))
00092                 throw new NetworkException(
00093                     $result['faultString'],
00094                     $result['faultCode']
00095                 );
00096             
00097             return $result;
00098         }
00099     }
00100 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4