How to solve php interaction with other languages (80)

Source: Internet
Author: User
Php interaction with other languages (80) Suppose I have a web server S1 and an application server S2 (such as the search function) S1y written in php, s2 applications are written in c (or java, c ++). how can I implement the interaction process between the two servers? 1. user access S1 & nbsp; 2. the server submits the operation to S23.S2 and returns the operation result to S1 & nbsp; 4. s1 return information to the user ------ solve the interaction between php and other languages (80)
Assume that I have a web server S1
There is also an application server S2 (such as the search function)

S1y is written in php, and S2 applications are written in c (or java, c ++,
How to implement interaction between the two servers

The process is like this,
1. user access S1
2. the server submits the operation to S2.
3. S2 returns the operation result to S1
4. S1 returns information to the user


------ Solution --------------------
Soap
------ Solution --------------------
Is json not acceptable?
------ Solution --------------------
Any data communication solution between servers on the network can be used.
Soap/xmlrpc/REST/json (p )/

If you have sufficient control permissions on both servers,
Of course, you can also directly communicate through the socket (non-Port 80 can be used)

If both servers can operate on a public media, such as the same database or network file system,
Of course, you can also transmit data through this media...

....


------ Solution --------------------
JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of JavaScript (Standard ECMA-262 3rd Edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language. Easy for reading and writing, and easy for machine parsing and generation.
------ Solution --------------------
You can try to use this
Http://developer.51cto.com/art/201002/182757.htm
Of course there is a slight difference. The goal is the same.
------ Solution --------------------
Write interface
------ Solution --------------------
The simplest method is to use file_get_contents ('http: // ip: Port') of php, and pass the value through GET or POST, the return value of a function is the processing result of S2. The key is how S2 creates an http-based listener.

You can use libevent to quickly create an http service. a simple code is as follows:
C/C ++ code
#include < sys/types.h>  #include < stdio.h> #include < stdlib.h> #include < unistd.h>  #include < event.h> #include < evhttp.h>   void generic_request_handler(struct evhttp_request *req, void *arg) {     struct evbuffer *returnbuffer = evbuffer_new();      evbuffer_add_printf(returnbuffer, "Thanks for the request!");     evhttp_send_reply(req, HTTP_OK, "Client", returnbuffer);     evbuffer_free(returnbuffer);     return; }  int main(int argc, char **argv) {     short http_port = 8084;     char *http_addr = "0.0.0.0";     struct evhttp *http_server = NULL;      event_init();     http_server = evhttp_start(http_addr, http_port);     evhttp_set_gencb(http_server, generic_request_handler, NULL);      fprintf(stderr, "Server started on port %d\n", http_port);     event_dispatch();      return(0); } 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.