Andy
Andy

Reputation: 3682

Making a web service in PHP to give information to Java

Following my question regarding connecting to a MySQL database in Java, I am looking to create a web service in PHP. My Java program needs to ask the web service to gather some data from MySQL database and send the result back. However, I have a few dilema's:

Firstly, my web hosts do not support Java, and therefore the server side needs to be written in PHP but the client needs to be written in Java.

Secondly, all the tutorials I have found seem to involve creating a whole web service project in order for my Java program to communicate with the web service, where as realistically only a couple of classes need to contact the PHP web service.

And, you may have already guessed but I don't know anything about web service's. It was just suggested that I used one in order to get around the GPL licence of the JDBC driver...

I realise that similar questions may have been asked here before but as I am a complete novice, the posts that are saw here did not contain enough information for me and I require as much help as I can get - almost a step by step guide!

Alternatively, I did think about just using standard PHP Sockets, as I am pretty sure I know how to use them. However, I don't know how secure they are and I didn't want to take any risks because I will be needing to retrieve information such as licence keys!

Thanks in Advance

Upvotes: 1

Views: 1933

Answers (2)

Justin Turner
Justin Turner

Reputation: 425

What I would do is use an agnostic form of communication between your PHP service and the Java client. My weapon of choice is XML.

The steps would be:

  1. Create the PHP classes which will interact with your database and get the data you want to work with. GitHub has plenty of examples and source code. Sample PHP-MySQL Database Abstraction Layer
  2. Create a RESTful php service which takes the data from step 1 and makes it into an XML REST service. Checkout the Recess Framework, an easy to use REST framework
  3. Create your JAVA client, it should just need to be able to work with HTTP, and consume XML. No need for a huge soap or other framework.

Upvotes: 1

JJ.
JJ.

Reputation: 5475

You don't need to use PHP Sockets, all you need is a simple PHP script on your web host that fetches the data you need from the MySQL DB and outputs the data to be read by your Java client.

Your PHP script will need:

  1. To retrieve any query parameters from the Java client (probably via $_POST or $_GET).
  2. Information to connect to MySQL (hostname/ip address, db name, username, password).
  3. To run SQL query/queries to grab the data from the database.
  4. To output the data for the java client to read, in some mutually-acceptable format, such as XML, JSON, HTML, etc.

You would structure the script something like this:

<?php
    // 1. Read and validate input parameters
    $myquery_val = $_POST['queryval'];

   // 2. Connect to MySQL

   // 3. Fetch MySQL data

   // 4. Output data
?>

To learn how to connect to MySQL and retrieve data, read up on MySQL PDO: http://php.net/manual/en/ref.pdo-mysql.php

Upvotes: 1

Related Questions