title: PHP create User in moodle via Webservice
body: <p>I want so connect an external website with a moodle-system. I've already set up the webService and created a token to get access.</p>
  
  <p>I've followed <a href="http://www.rumours.co.nz/manuals/using_moodle_web_services.htm" rel="nofollow">http://www.rumours.co.nz/manuals/using_moodle_web_services.htm</a> set up but in contrast i wanted to realise the connection via REST as in <a href="https://github.com/moodlehq/sample-ws-clients/find/master" rel="nofollow">https://github.com/moodlehq/sample-ws-clients/find/master</a></p>
  
  <p>My approach is to have a moodle class which will handle the data exchange. In first place i just wanted to try to create some new hard coded Users via the webService but it fails with the Moodle-Response:</p>
  
  <p>"invalidrecord Can not find data record in database table external_functions. "</p>
  
  <p>Which seems to me as if i the call was successfully but moodle has a problem to find the "core_user_create_users" function. I've checked the local moodle Database and in the table external_functions is an entry for "core_user_create_users" so i'm kind of confused where moodle doesn't know what to do.</p>
  
  <p>Thats my class:</p>
  
  <pre><code>require_once (DOCUMENT_ROOT.'/tcm/api/moodle/curl.php');
  
  class Moodle {
  
      private $token;          
      private $domainName;     // 'local.moodle.dev';
      private $serverUrl;
  
      public function __construct($token, $domainName) {
          $this-&gt;token = $token;
          $this-&gt;domainName = $domainName;
          $this-&gt;serverUrl = $this-&gt;domainName . '/webservice/rest/server.php' . '?wstoken=' . $this-&gt;token;
  
          echo "initialize Service: $this-&gt;serverUrl &lt;/br&gt;";
      }
  
  
      public function createUser() {
          $functionName = 'core_user_create_users';
  
          /// PARAMETERS - NEED TO BE CHANGED IF YOU CALL A DIFFERENT FUNCTION
          $user1 = new stdClass();
          $user1-&gt;username = 'testusername1';
          $user1-&gt;password = 'testpassword1';
          $user1-&gt;firstname = 'testfirstname1';
          $user1-&gt;lastname = 'testlastname1';
          $user1-&gt;email = 'testemail1@moodle.com';
          $user1-&gt;auth = 'manual';
          $user1-&gt;idnumber = 'testidnumber1';
          $user1-&gt;lang = 'en';
          $user1-&gt;theme = 'standard';
          $user1-&gt;timezone = '-12.5';
          $user1-&gt;mailformat = 0;
          $user1-&gt;description = 'Hello World!';
          $user1-&gt;city = 'testcity1';
          $user1-&gt;country = 'au';
          $preferencename1 = 'preference1';
          $preferencename2 = 'preference2';
          $user1-&gt;preferences = array(
          array('type' =&gt; $preferencename1, 'value' =&gt; 'preferencevalue1'),
          array('type' =&gt; $preferencename2, 'value' =&gt; 'preferencevalue2'));
          $user2 = new stdClass();
          $user2-&gt;username = 'testusername2';
          $user2-&gt;password = 'testpassword2';
          $user2-&gt;firstname = 'testfirstname2';
          $user2-&gt;lastname = 'testlastname2';
          $user2-&gt;email = 'testemail2@moodle.com';
          $user2-&gt;timezone = 'Pacific/Port_Moresby';
          $users = array($user1, $user2);
          $params = array('users' =&gt; $users);
  
          /// REST CALL
          $serverurl = $this-&gt;serverUrl .  '&amp;wsfunction=' . $functionName;
          require_once (DOCUMENT_ROOT.'/tcm/api/moodle/curl.php');
          $curl = new curl;
          //if rest format == 'xml', then we do not add the param for   backward compatibility with Moodle &lt; 2.2
          $restformat = "json";
          $resp = $curl-&gt;post($serverurl . $restformat, $params);
          //print_r($resp);
  
          echo '&lt;/br&gt;*************Server Response*************&lt;/br&gt;';
          var_dump($resp);
     }
  
  }
  </code></pre>
  
  <p>I'm using the curl class from the same github-project which i posted above - moodle is linkng to it in their Documentation.. 
  docs.moodle.org/dev/Creating_a_web_service_client</p>
  
  <p>The entry point of my call is hardcoded right now:</p>
  
  <pre><code>&lt;?php
  
  include_once (DOCUMENT_ROOT.'/tcm/api/moodle/moodle.php');
  //entry point of code
  
  if (isset($_POST)){
      //token and domain would be in $_POST
      $bla = new Moodle('0b5a1e98061c5f7fb70fc3b42af6bfc4', 'local.moodle.dev');
      $bla-&gt;createUser();
  }
  </code></pre>
  
  <p>Does anyone know how to solve the "invalidrecord Can not find data record in database table external_functions" error or has a different approach/suggestion how i can create my users remotely??</p>
  
  <p>Thanks in advance</p>