Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/soaplib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00032 defined('MOODLE_INTERNAL') || die();
00033 
00043 function soap_connect($wsdl, $trace=false) {
00044     try {
00045         $connection = new SoapClient($wsdl, array('soap_version'=>SOAP_1_1, 'exceptions'=>true, 'trace'=>$trace));
00046     }
00047     catch (SoapFault $f) {
00048         $connection = $f;
00049     }
00050     catch (Exception $e) {
00051         $connection = new SoapFault('client', 'Could not connect to the service');
00052     }
00053     return $connection;
00054 }
00055 
00066 function soap_call($connection, $call, $params) {
00067     try {
00068         $return = $connection->__soapCall($call, $params);
00069     }
00070     catch (SoapFault $f) {
00071         $return = $f;
00072     }
00073     catch (Exception $e) {
00074         $return = new SoapFault('client', 'Could call the method');
00075     }
00076     // return multiple parameters using an object rather than an array
00077     if (is_array($return)) {
00078         $keys = array_keys($return);
00079         $assoc = true;
00080         foreach ($keys as $key) {
00081             if (!is_string($key)) {
00082                 $assoc = false;
00083                 break;
00084             }
00085         }
00086         if ($assoc)
00087             $return = (object) $return;
00088     }
00089     return $return;
00090 }
00091 
00092 function soap_serve($wsdl, $functions) {
00093     // create server object
00094     $s = new SoapServer($wsdl);
00095     // export functions
00096     foreach ($functions as $func)
00097         $s->addFunction($func);
00098     // handle the request
00099     $s->handle();
00100 }
00101 
00102 function make_soap_fault($faultcode, $faultstring, $faultactor='', $detail='', $faultname='', $headerfault='') {
00103     return new SoapFault($faultcode, $faultstring, $faultactor, $detail, $faultname, $headerfault);
00104 }
00105 
00106 function get_last_soap_messages($connection) {
00107     return array('request'=>$connection->__getLastRequest(), 'response'=>$connection->__getLastResponse());
00108 }
00109 
00110 // Fix simple type encoding - work around a bug in early versions of PHP5 < 5.0.3, see http://bugs.php.net/bug.php?id=31832
00111 function soap_encode($value, $name, $type, $namespace, $encode=XSD_STRING) {
00112     $value = new SoapVar($value, $encode, $type, $namespace);
00113     if ('' === $name)
00114         return $value;
00115     return new SoapParam($value, $name);
00116 }
00117 
00118 // Fix complex type encoding - work around a bug in early versions of PHP5 < 5.0.3, see http://bugs.php.net/bug.php?id=31832
00119 function soap_encode_object($value, $name, $type, $namespace) {
00120     if (!is_object($value))
00121         return $value;
00122     $value = new SoapVar($value, SOAP_ENC_OBJECT, $type, $namespace);
00123     if ('' === $name)
00124         return $value;
00125     return new SoapParam($value, $name);
00126 }
00127 
00128 // Fix array encoding - work around a bug in early versions of PHP5 < 5.0.3, see http://bugs.php.net/bug.php?id=31832
00129 function soap_encode_array($value, $name, $type, $namespace) {
00130     if (!is_array($value))
00131         return $value;
00132     $value = new SoapVar($value, SOAP_ENC_ARRAY, 'ArrayOf' . $type, $namespace);
00133     if ('' === $name)
00134         return $value;
00135     return new SoapParam($value, $name);
00136 }
00137 
00138 // In both cases...
00139 function handle_soap_wsdl_request($wsdlfile, $address=false) {
00140     header('Content-type: application/wsdl+xml');
00141     $wsdl = file_get_contents($wsdlfile);
00142     if (false !== $address) {
00143         if (true === $address) {
00144             $address = (($_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
00145         }
00146         $wsdl = str_replace('###SERVER_ADDRESS###', $address, $wsdl);
00147     }
00148     echo $wsdl;
00149     exit;
00150 }
 All Data Structures Namespaces Files Functions Variables Enumerations