-
Notifications
You must be signed in to change notification settings - Fork 2
PHP JSON API (Eclipse) HowTo
Prerequisites for this guide are HowTo:
- Build a PHP Application (in Eclipse)
- Build an Android or iOS Client App
In this guide, you will be covering the following topics:
- Creating a server file and copying and pasting source code
- sending the JSON Message (As a Mobile Developer)
If you want to save a little time and execute on a free server with PHP try:
-
www.000webhost.com/ Which I tried recently and they added some Javascript code/tags to my JSON ouput (be wary and Strip HTML tags)
-
http://www.serversfree.com/ which I haven't tried yet, but looks like they are careful about their presentation and could be the same with their service.
-
Lastly If you choose to create your own Ubuntu Server you get free server space through Rackspace's Developer program and you should also check out this guide, but
- See more on the php installation process at The PHP Manual (English).
Note: In Eclipse you can edit using PHP, Ruby, and more with the Aptana Studio Plugin where you can also do FTP, SFTP and more.
Highlight the source code below and copy it (CTRL/CMD + C or Right Click)
<?php
// RECEIVE INPUT JSON
$inputJSON = json_decode(file_get_contents('php://input'), true);
// PROCESS INPUT JSON
$originalMessage = $inputJSON["message"];
$alteredMessage = strtoupper($originalMessage);
$shuffledMessage = str_shuffle($originalMessage);
// PREPARE OUTPUT JSON
$outputJSON = array(
'originalMessage' => $originalMessage,
'shuffledMessage' => $shuffledMessage,
'alteredMessage' => $alteredMessage
);
// SEND OUTPUT JSON
header('Content-type: application/json');
echo json_encode($outputJSON);
?>
See more on the php at The PHP Manual, where you can choose your language of choice between English, Brazilian Portuguese, Chinese (Simplified), French, German, Japanese, Korean, Romanian, Russian, Spanish and Turkish.
Paste the source code by right clicking in the Package Explorer and pasting the copied content.
Your file is now generated for you in the package explorer, with the following file structure. The only difference from the Build a PHP Application (in Eclipse) MAC Guide, is that the file name is now Server.PHP.
Now that you have a file, you can execute your Server application. To start the PHP Server Application, select your file by right clicking on it, Then select execute-As-> PHP Application . You should get the following result:
Server started and waiting for client sendion!
Listening on IP: 10.0.0.6
Note: The second line that contains the Server IP Address will be dependent upon your own Wifi configuration.
Once you send your completed JSON Message, the result would look as follows:
Server started and waiting for client sendion!
Listening on IP: 10.0.0.6
sended to 10.0.0.3, and waiting for client input!
The final result would look as follows:
Server started and waiting for client sendion!
Listening on IP: 10.0.0.6
sended to 10.0.0.3, and waiting for client input!
In from client (10.0.0.3): my input
Out to client (10.0.0.3): MY INPUT

