When Brains Fly

An Awesome Blog About Neuroscience, Computer Science, and Technology

REST, AJAX, PHP, la di da

[ January 9th, 2010 ]

Yesterday, I spent much of the day playing around with Javascript and PHP. I had the idea to create a simple web front-end to access the Weewar API and control bots, an alternative to the current main method of controlling bots, which is to use existing non-web-based Java programs to call the API or use cURL on the command line. Neither is particularly easy for novice bot developers or those new to programming. Then again, controlling a bot move-by-move manually through a GUI and without programming isn’t all that practical… Oh well, it would still be useful for me when I have to do a few simple moves with a bot or two but don’t want to sign in as the bot, access the game, and do the moves there. Regardless, it was a new mini-project which I could use to sharpen my knowledge of Javascript and PHP, and it was fun. :) (<-- whoa, graphical smiley face!)

First, I needed a way to access and display games that a user is currently playing. This can be done with the API: http://weewar.com/api1/user/[username] and is already partially done with the Weewar Profiler Greasemonkey script, which I was already somewhat familiar with, having had to edit it every time there was a Weewar UI change that broke it. I adapted that to take in a username from an HTML form and use the Javascript XMLHttpRequst object, but then it didn’t work. It turns out that, unlike Greasemonkey, using normal Javascript, you can’t make calls to pages outside of your domain, for security reasons. That makes sense, but was a bummer.

So I had to make the API calls from my webhosting server and decided to learn how to do it using PHP, which I barely knew. (What other easy ways are there?) I used this as a reference, and just now realized upon exploring the Yahoo Developer Center that my approach was already documented here: Fixing AJAX: XMLHttpRequest Considered Harmful. Ha! I guess that doesn’t leave much to say except that I pretty much did method #1 there.

Also, since I had to make many XMLHttpRequest requests, I had to make an array of those objects to keep them from confusing one another. It took me a while to figure out why that was happening, why I was only getting one or a couple requests returned correctly. So I guess that’s it. Now I know how to make REST calls using sockets in Java, Apache HttpClient in Java, the Spring 3 REST template in Java, XMLHttpRequest objects in Javascript, GM_xmlhttpRequest in Greasemonkey Javascript, and the cURL extension in PHP. Whoo!

Aside: I just realized that the Poster Firefox add-on, which I had downloaded a couple weeks back, makes REST calls as well, and in a much more general-purpose way. Oh well. I wouldn’t have learned much had I not done all that I had done yesterday, so it was not all for nothing. =) Here is what resulted from it, by the way: Weewar Bot Control

Hm. Am I an aspiring web programmer? It certainly is fun and the Internet is the Wave of the Future/Present. I’ll have to think on that some more. I’m still quite attached to neuroscience research. At the very least, web programming is a nice, useful hobby. =)

Leave a Comment!

Proudly powered by WordPress. Theme developed with WordPress Theme Generator.
Copyright © When Brains Fly. All rights reserved.