webnat0
webnat0

Reputation: 2716

What is the best way to send data from Java to PHP?

I want to access a MySQL database from Java, but remote connection is disabled by the host. So I will send the data to PHP and then PHP will locally access the database. The data is pretty big (about 2~4kb) I've never done this before. What should I do?

Upvotes: 1

Views: 1683

Answers (6)

Roberto Aloi
Roberto Aloi

Reputation: 30985

You might want to have a look to Thrift:

http://incubator.apache.org/thrift/

Upvotes: 0

fish
fish

Reputation: 1050

I assume the Java program will be the client. I think the simplest would be to just create a simple PHP page, POST any possible parameters to that page from Java, and return the data as a JSON structure.

There are a number of JSON tools for Java and PHP listed on JSON page: http://json.org/

Upvotes: 0

this. __curious_geek
this. __curious_geek

Reputation: 43207

Http web-services

Upvotes: 1

Eric Petroelje
Eric Petroelje

Reputation: 60498

Probably the best way would be to write a web service in PHP that you call from Java.

Another somewhat easier way would be to use the HttpURLConnection class to simply do a POST of the data to the PHP page.

If the PHP page is publicly accessible, you'd want to make sure you have some kind of authentication mechanism in place and use HTTPS as well.

Upvotes: 2

Kris.Mitchell
Kris.Mitchell

Reputation: 998

Are you saying that you are going to run java from php, so that php has the handle to the mysql database and you will pass the data to java?

check out java php bridge

Upvotes: -1

Chris J
Chris J

Reputation: 9252

Sounds like your only option is to create a Web Service. You can use Apache's HttpClient in Java to create requests and have your PHP return a response that you can easily parse. Depending on what you are trying to do, an XML response might be overkill. I've used JSON in the past if I know exactly what I am getting back.

Upvotes: 0

Related Questions