How to exchange data between Objective-C and PHP - 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.

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.

Make GWT interact with REST Architecture in 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?

Can I execute js files via php?

The situation is next:
I have php file, which parses a web-page. on that web-page is a phone number and it's digits are mixed with each other. The only way to put each digit on the correct place is to use some JS functions (on the client side). So, when I execute that php file in linux console, it gives me all that I need, except js function's result (no wonder - JavaScript is not a server-side language). So all I see from JS - only a code, that I have written.
The question: can I execute js files via php and how?
Results of a quick google search (terms = javascript engine php)
J4P5 -- not developed since 2005 [BAD](according to its News)
PECL package spidermonkey
a 2008 post by jeresig points to PHPJS but can't see when it was last updated.
I'm sure you'll find many more links on that google search.
Alternatively:
you say that the digits are scrambled and you need to "unscramble" them using js. Can you code that unscrambling logic into a PHP function and just use it? Will sure save you a lot of trouble, but if learning to use js in php is what you're after, then its a whole different story...
Zend tutorial: "Using javascript in PHP with PECL and spidermonkey"?
http://en.wikipedia.org/wiki/Comparison_of_Server-side_JavaScript_solutions
Alternatively, PHP has simple functions for executing other programs and retrieving their output (I used this along with GPG once to create a PHP file manager which could live-encrypt files as you were uploading them and live-decrypt as you were downloading them)
Using those functions along with http://code.google.com/p/v8/ you should be able to interpret any javascript.
Not unless you know someone who's implemented a Javascript engine in PHP.
In other words, probably not.
Without some sort of browser emulation or passing the unparsed js off to a server side implementation of javascript (maybe node.js?), you won't be able to execute it.
However, does the page use the same js function to unscramble the phone number every time? You should be able to read the incorrect digits and shuffle them with PHP.
If you're prepared to do a bit of work building your own JS runtime to work with it, Tim Whitlock has written a javascript tokenizer and parser in pure PHP
node.js is server-side... but full JS :) no PHP in it so I don't it answer your needs...
Anyway, here is an example : a chat in JS both client & server-side : http://chat.nodejs.org/
Plus, not every host allows you to use the v8 engine...
If you have Javascript data objects, and you need to convert them to/from PHP arrays, that's quite easy using PHP's json_encode() and json_decode() functions.
But actually running Javascript code? No. You can't. You might be able to find a JS interpreter written in PHP (a few other answers have pointed a links that may or may not help you here), or more likely execute the JS using a stand-alone JS interpreter on your server which you call out to from PHP. However if the JS code includes references to the browser's DOM (which is highly likely), that's a whole other set of issues which will almost certainly make it impossible.
Given the way you describe the question, I'd say the easiest solution for you would just be to re-implement the JS code as PHP code; it's unlikely that all the work arounds being suggested would be appropriate for what sounds like a fairly simple bit of utility code.

Serving a json file for IPHONE app

I am trying to get an introduction to serving files to the iphone. I have watched tutorials on getting files from sites like Flickr and twitter. I need a tutorial to show me how to set up the site that is feeding that information. Most of those sites send you a json file. Can I just keep a dynamic file on a server using php?
PHP has some great json functions. See json_encode, which takes a PHP structure and converts it to a json string, and json_decode, which does the opposite. Make sure you set a Content-type: application/json header, echo the result of json_encode, and your iPhone app should be able to read it. Similarly, you can use json_decode to read JSON data that has been sent to your PHP script using GET or POST (preferably the latter).
All those sites use a web service to achieve the json response, depending on what you are using the ability to send back json responses might be built in to the services framework etc (.net for example), here is a question that talks about using .net to make a webservice iPhone interaction with ASP.NET WebService, im sure you can find many more out there
Found this tutorial once i got a better understanding of what i needed to do. http://davidwalsh.name/web-service-php-mysql-xml-json

Categories