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 
00015     final class XmlRpcClient
00016     {
00017         private $url        = null;
00018         private $timeout    = null;
00019         
00020         public function __construct($url = null)
00021         {
00022             $this->url = $url;
00023         }
00024         
00028         public static function create($url = null)
00029         {
00030             return new self($url);
00031         }
00032         
00033         public function getUrl()
00034         {
00035             return $this->url;
00036         }
00037         
00041         public function setUrl($url)
00042         {
00043             $this->url = $url;
00044             return $this;
00045         }
00046         
00047         public function getTimeout()
00048         {
00049             return $this->timeout;
00050         }
00051         
00055         public function setTimeout($timeout)
00056         {
00057             $this->timeout = $timeout;
00058             return $this;
00059         }
00060         
00061         public function query($method, $parameters = null)
00062         {
00063             $request = xmlrpc_encode_request($method, $parameters);
00064             
00065             $headers = array(
00066                 "Content-type: text/xml",
00067                 "Content-length: ".strlen($request)
00068             );
00069             
00070             $curl = curl_init();
00071             curl_setopt($curl, CURLOPT_URL, $this->url);
00072             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
00073             curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
00074             curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
00075             
00076             if ($this->timeout)
00077                 curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
00078             
00079             $rawResponse = curl_exec($curl);
00080             $curlErrno = curl_errno($curl);
00081             $curlError = curl_error($curl);
00082             
00083             curl_close($curl);
00084             
00085             if ($curlErrno)
00086                 throw new NetworkException($curlError, $curlErrno);
00087                 
00088             $result = xmlrpc_decode($rawResponse);
00089             
00090             if (xmlrpc_is_fault($result))
00091                 throw new NetworkException(
00092                     $result['faultString'],
00093                     $result['faultCode']
00094                 );
00095             
00096             return $result;
00097         }
00098     }
00099 ?>