Skip to content

PHP JSON API (Eclipse) HowTo

Christopher Miller edited this page Jun 17, 2015 · 1 revision

This MAC Guide describes how to create a PHP JSON API using the Eclipse IDE.

Prerequisites for this guide are HowTo:

In this guide, you will be covering the following topics:

1 Creating Server file by copying and pasting source code

1.0 Create file on server

If you want to save a little time and execute on a free server with PHP try:

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.

1.1 Copy source code

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.

1.2 Paste the Server source code

Paste the source code by right clicking in the Package Explorer and pasting the copied content.

Paste Source

1.3 Server file created

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.

First file

2 Starting the Server implementation

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.

3 Sending the JSON Message (As a Mobile Developer)

3.1 Send the Client file

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!

3.2 Send a Message from Client App

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

Clone this wiki locally