mnet: core libraries and admin pages
[moodle.git] / mnet / testclient.php
CommitLineData
71558f85 1<?php
2/**
3 * A template to test Moodle's XML-RPC feature
4 *
5 * This script 'remotely' executes the mnet_concatenate_strings function in
6 * mnet/testlib.php
7 * It steps through each stage of the process, printing some data as it goes
8 * along. It should help you to get your remote method working.
9 *
10 * @author Donal McMullan donal@catalyst.net.nz
11 * @version 0.0.1
12 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
13 * @package mnet
14 */
15require_once(dirname(dirname(__FILE__)) . '/config.php');
16require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';
17
18error_reporting(E_ALL);
19
20if (isset($_GET['func']) && is_numeric($_GET['func'])) {
21 $func = $_GET['func'];
22
23
24// Some HTML sugar
25echo '<?xml version="1.0" encoding="utf-8"?>';
26?>
27<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
28<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
29<head><title>Moodle MNET Test Client</title></head><body>
30<?php
31
32// For the demo, our 'remote' host is actually our local host.
33$wwwroot = $CFG->wwwroot;
34
35// Enter the complete path to the file that contains the function you want to
36// call on the remote server. In our example the function is in
37// mnet/testlib/
38// The function itself is added to that path to complete the $path_to_function
39// variable
40$path_to_function[0] = 'mnet/rpclib/mnet_concatenate_strings';
41$path_to_function[1] = 'mod/scorm/rpclib/scorm_add_floats';
42$path_to_function[2] = 'system/listMethods';
43$path_to_function[3] = 'system/methodSignature';
44$path_to_function[4] = 'system/methodHelp';
45$path_to_function[5] = 'system/listServices';
46$path_to_function[6] = 'system/listMethods';
47$path_to_function[7] = 'system/listMethods';
48
49$paramArray[0] = array(array('some string, ', 'string'),
50array('some other string, ', 'string'),
51array('and a final string', 'string'));
52
53$paramArray[1] = array(array(5.3, 'string'),
54array(7.1, 'string'),
55array(8.25323, 'string'));
56
57$paramArray[2] = array();
58
59$paramArray[3] = array(array('auth/mnet/auth/user_authorise', 'string'));
60
61$paramArray[4] = array(array('auth/mnet/auth/user_authorise', 'string'));
62
63$paramArray[5] = array();
64
65$paramArray[6] = array(array('sso', 'string'));
66
67$paramArray[7] = array(array('concatenate', 'string'));
68
69echo 'Your local wwwroot appears to be <strong>'. $wwwroot ."</strong>.<br />\n";
70echo "We will use this as the local <em>and</em> remote hosts.<br /><br />\n";
71flush();
72
73// mnet_peer pulls information about a remote host from the database.
74$mnet_peer = new mnet_peer();
75$mnet_peer->set_wwwroot($wwwroot);
76
77echo "Your \$mnet_peer from the database looks like:<br />\n<pre>";
78$h2 = get_object_vars($mnet_peer);
79while(list($key, $val) = each($h2)) {
80 if (!is_numeric($key)) echo '<strong>'.$key.':</strong> '. $val."\n";
81}
82echo "</pre><br/>It's ok if that info is not complete - the required field is:<br />\nwwwroot: <b>{$mnet_peer->wwwroot}</b>.<br /><br/>\n";
83flush();
84
85// The transport id is one of:
86// RPC_HTTPS_VERIFIED 1
87// RPC_HTTPS_SELF_SIGNED 2
88// RPC_HTTP_VERIFIED 3
89// RPC_HTTP_SELF_SIGNED 4
90
91if (!$mnet_peer->transport) exit('No transport method is approved for this host in your DB table. Please enable a transport method and try again.');
92$t[1] = 'http2 (port 443 encrypted) with a verified certificate.';
93$t[2] = 'https (port 443 encrypted) with a self-signed certificate.';
94$t[4] = 'http (port 80 unencrypted) with a verified certificate.';
95$t[8] = 'http (port 80 unencrypted) with a self-signed certificate.';
96$t[16] = 'http (port 80 unencrypted) unencrypted with no certificate.';
97
98echo 'Your transportid is <strong>'.$mnet_peer->transport.'</strong> which represents <em>'.$t[$mnet_peer->transport]."</em><br /><br />\n";
99flush();
100
101// Create a new request object
102$mnet_request = new mnet_xmlrpc_client();
103
104// Tell it the path to the method that we want to execute
105$mnet_request->set_method($path_to_function[$func]);
106// Add parameters for your function. The mnet_concatenate_strings takes three
107// parameters, like mnet_concatenate_strings($string1, $string2, $string3)
108// PHP is weakly typed, so you can get away with calling most things strings,
109// unless it's non-scalar (i.e. an array or object or something).
110foreach($paramArray[$func] as $param) {
111 $mnet_request->add_param($param[0], $param[1]);
112}
113
114if (count($mnet_request->params)) {
115 echo 'Your parameters are:<br />';
116 while(list($key, $val) = each($mnet_request->params)) {
117 echo '&nbsp;&nbsp; <strong>'.$key.':</strong> '. $val."<br/>\n";
118 }
119}
120flush();
121
122// We send the request:
123$mnet_request->send($mnet_peer);
124
125?>
126
127A var_dump of the decoded response: <strong><pre><?php var_dump($mnet_request->response); ?></pre></strong><br />
128
129<?php
130 if (count($mnet_request->params)) {
131?>
132 A var_dump of the parameters you sent: <strong><pre><?php var_dump($mnet_request->params); ?></pre></strong><br />
133<?php
134 }
135}
136 ?>
137 <p>
138 Choose a function to call:<br />
139 <a href="testclient.php?func=2">system/listMethods</a><br />
140 <a href="testclient.php?func=3">system/methodSignature</a><br />
141 <a href="testclient.php?func=4">system/methodHelp</a><br />
142 <a href="testclient.php?func=5">listServices</a><br />
143 <a href="testclient.php?func=6">system/listMethods(SSO)</a><br />
144 <a href="testclient.php?func=7">system/listMethods(concatenate)</a><br />
145
146</body></html>