I've just realised, when replying to someone's question about my postings, that I didn't explain where the value of the $token variable came from. The values in my demo are made up, and will not work. Anyone using my code will need to make their own values. I don't have access to Moodle at the moment, but I seem to remember that the way to do this is as described in section "Create a token" of "Using web services". The steps it describes will create and display a new token, whose value you can then use in the code.