Make GWT interact with REST Architecture in PHP - php

On my localhost,I have an already made REST Architecture. I need to make a GWT application to interact with the REST Interface.The Architecture returns JSON format data which I need to parse with GWT. Does someone have an idea how to make this?
Edit:
I have a service running at http://localhost/recess/restApp/comment, which returns a JSON like this `{"Action":"GetComments","ActionStatus":1,"CommentIds":["4","5","7"]}, I need to get this,
Normally, GWT runs a java servlet, so how do i change this??

There are two approaches:
AutoBeans
JS Overlay Types
Use JS Overlay Types to make your JS objects available to Java/GWT code, and JsonUtils to parse JSON to your overlay types.
In the event you couldn't use overlay types, have a look at JSONParser (cumbersome API unfortunately). JSONObject is also the only way to serialize an overlay type to JSON (wrap it in a JSONObject then call toString()).
The alternative is AutoBean. The main (if not only) advantage is to make your code reusable in other Java apps (such as an Android client, or in a Java servlet calling your PHP service). Otherwise, it mostly saves you from writing JSNI (on declare methods in an interface and possibly annotate them with #PropertyName)
The answer wouldn't be complete if it didn't mention JsonpRequestBuilder (there's also RequestFactory with JsonRpcService but it's about JSON-RPC –not quite RESTful– and is still experimentatl)

Have you looked at GWTs support for JSON?

Related

Web RPG - Storing dialog

I'd like to create an RPG in JavaScript. The RPG will have a lot of dialog in it. I have heard XML is the way to go..., but I have no experience using XML, and a good amount of experience with PHP and MySQL.
My questions are:
Would it be better to store dialog in a MySQL database and access it with PHP using IDs? Or should I just keep it all on the front end by accessing XML files with Javascript?
If I use XML, do I need to run the javascript on a webserver (Like WAMP)? Or can I parse it locally with:
function parseXML() {
xmlhttp=new window.XMLHttpRequest();
xmlhttp.open("GET","dialog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
}
(Which is currently giving the error XMLHttpRequest cannot load file:///C:/Users... Cross origin requests are only supported for HTTP.
Even after reading up on XML, I'm still not sure how I can integrate it with JavaScript. Can someone help give me an example of how I could take a sample rpg XML dialog and meaningfully parse it in JavaScript?
Thank you
Why don't you use JSON instead? It's much easier to use from JavaScript code. While you're at it, avoid usind XMLHttpRequest by hand and use some library that handles the bad stuff for you (e.g., jQuery).
For the "dialog" part, I'm not sure what you're gonna use MySQL for. Anyway, you can use any form of storage on the server side, provided that your PHP scripts give well-formed replies to the client side code.
Would it be better to store dialog in a MySQL database and access it
with PHP using IDs? Or should I just keep it all on the front end by
accessing XML files with Javascript?
The main advantage of having a database here is that you gain a lot of flexibility. You can create/edit/delete a piece of dialogue and its relations quite easily and without having to maintain/regenerate a huge data file every time (As you would do with a XML or JSON or w/e formalism you choose).
On the other hand, this solution might be overkill if you just have simple dialogues with no relations and limited content.
Since you are about to do a game in Javascript, and if your content is not changing that much, you can consider a JSON encoding for your dialogues. You could parse the data instantly via Javascript and still have the possibility to make all kind of complex relations between dialogues as you would in XML.
If I use XML, do I need to run the javascript on a webserver (Like WAMP)? Or can I parse it locally with: ...
You have this error because you indeed need to run a server here. If you just want to do some tests, no need to use WAMP or whatever big webserver, just run a python server for now and that will do the trick.
cd /yourProject
python -m SimpleHTTPServer
Let the server run and open your browser at:
http://127.0.0.1:8000
Give the input file a base url like http://localhost/rpgame/dialog.xml.

Is there a XML decoder for PHP objects?

I am build a application in Delphi that stores some configuration that is not going to be passive to query filters on the database. So I decided to create some blob text fields to store those configurations that will only be used as keys to configure some modules of the application.
In this Delphi side of the application, I am using NativeXML run-time components to decode the configuration class or record type of each module into/from XML and populate that field on the database.
My problem came when I realized that this application will have a web site module where people will register for clinical attending and this part will need to use some of the configuration stored on that XML on the database. So...
I am newbie on PHP and I wish to know from you if PHP has the ability to do that XML<->Object\Record DeCoding or do I have to look for a library that makes it possible?
Note: If there is only a record type capacity, I will use it, but if not, I prefer to use classes
Edit:
In response to some comments on answers, I would say that I use XML instead of JSON because of this Delphi XML library that suited me well! If someone could point me to a goo JSON DeCode library to convert JSON<->Delphi Objects will really use it instead of XML because I like to work with JSON. Would that solve the problem on the PHP parsing?
That is not that easy in PHP. However there are lots of smart folks out there, who where facing the same problem.
Paul Ferrertt has a XML-Encode Class here:
http://www.paulferrett.com/2009/encoding-an-object-in-xml-with-php/
In PHP there are multiple functions to decode a XML sheet. Start here:
http://www.php.net/manual/en/refs.xml.php
http://php.net/manual/en/function.xml-parse.php
However you won' t be able to get an object back as easy as with json_decode() and that for a reason XML is not meant to transfer objects (and the like) around. You have to write your own conversion methods.
I suggest you to read this : http://www.php.net/manual/en/refs.xml.php. Some of these libraries are easier to use that are others, some others are more powerful, etc.

Yii SOAP Web Service returning XML

I am trying to write a web service API using PHP and the Yii framework. This means that I will be using SOAP web services, since Yii does not directly support RESTful web services.
I would like to return results in XML format, as strings (this might be stupid, but I'm working with rather cumbersome data types that would make life more difficult for the web service clients trying to use them). The problem I'm facing is that the XML I'm returning contains tags, which leads to the strings not being read properly by the web service client.
For instance, I can return this string in one of my web services:
"hello"
But not this string:
"<hello>"
(An empty string is read by the client)
So, is there a way for a SOAP web service to return an XML document as a string (in general, or using Yii in particular)?
This should really be done by the framework (don't know about Yii though), but since it doesn't seem to work automatically for you, try escaping the data first:
$data_to_send = htmlspecialchars($data_to_send);
If Yii doesn't encode the data, your client will see the correct output (the raw XML string) when fetching the data from their SOAP library. If Yii, however, does encode the data, it will get double-encoded which is probably not what you want, but if it does encode it you shouldn't have this problem in the first place...
If your data contains characters that are also XML tags, you can use the CDATA option.
<value><![CDATA[<hello>]]></value>
Please don't use soap. It is one if the worst technologies ever made (see the numerous posts on SO) . Yii does support REST now (fully as of 1.17). Just use JSON format. Do you need to delete or insert from the client?

Real time RSS display on web page (best practices and source codes)

i have a php script who parser a rss and give me the data in a know pattern. Im very new with ASP, JavaScript and Jquery so i dont have any idea of how to autoupdate the script and display the new data with a smooth animation (see this example, that exactly what i want). Thanks for the support and if you know a good script to made this i will appreciate it.
Seems like you're looking for this:
http://leftlogic.com/lounge/articles/jquery_spy2/
It's PHP (not ASP), so that might be an issue, though the code is SUPER easy to implement (I've written by own implementation on three separate occasions).
The site itself has some decent documentation on getting things up and running, but if you need some extra help, comment and I'll point you in the right direction :)
Good luck!
The resources people have linked here are helpful and merely mentioning jQuery means you're probably headed in the right direction. But if you're new to this it might still be worth mentioning some of the concepts you'll be looking to play with here.
First of all, you'll probably want to stick with one language on the client side and one on the server side. This means choosing either PHP or ASP -- this isn't clear from your question but I'll assume you're dealing with PHP since that's the language I use for this kind of thing. JavaScript + jQuery is the right choice for the browser (client) side of things.
Like Luca points out, you'll have to set up some JavaScript code that goes live on page load and "polls" the server at a set interval. In JavaScript you do this using something called XMLHttpRequest (or "XHR") and it's pretty complicated. You could use combination of jQuery and a library like the one Matt points to in his answer, or just jQuery -- sample code abounds but it's basically a loop with a function call and sleep timer.
That function call is going to be one of the more difficult parts if you're trying to emulate the Twitter World Cup site. But here's the basic idea: You need to populate a list using jQuery and a data standard like JSON. Since the RSS feed you'll be parsing is written in XML, you'll have to write a server side (PHP/ASP) script that fetches, parses and converts the feed to JSON. In PHP, this is best done through cURL (file_get_contents() if you're lazy), SimpleXML and json_encode(), respectively.
Your JavaScript should load the list based on JSON. To do this, and display any new items, what you'll do is load the JSON from the client (browser) side using a jQuery method like getJSON(). Then you spin through the array object and add any new items to the list by adding new <li> elements to the "DOM." The same jQuery code that does this can easily also do the cross dissolve with something like fadeIn().
It looks like the script on that example page has an Ajax request running every TOT seconds.
You could simply have your PHP script return the RSS data (in JSON format say) and let JavaScript parse it and generate some HTML with it.
If all of this doesn't make sense to you I advice reading a little about JavaScript and PHP... there's plenty of good books.

How to exchange data between Objective-C and PHP

I am playing around with Objective-C and have been looking for a good how-to example to exchange data it and PHP. JSON? SOAP? If anyone knows of a well written example I could hack at, I would appreciate it.
When you say exchange data, do you mean over a network connection, between programs or through files?
If you are look to transfer the data over an internet connection (eg. your Objective-C program has/is a web server) then I would personally use JSON as this would allow your Objective-C program to communicate with a javascript web app in future if required as well as the simple php json_encode / json_decode functions.
I would personally say the same applies to other systems, but depending on how you expect this to work something like xml or SOAP may work better.
Edit:
JSON Framework for Objective-C on Google Code
PHP Manual for JSON (including links on to json_encode and json_decode) functions.

Categories