PHP background and mobile app interface development example code

Source: Internet
Author: User
Tags form post
This article is mainly to share with you the PHP background and mobile phone app interface Development example code, hope to help everyone

First, the mobile app (client) program interface

This is a post that simulates HTTP protocol data on a PC using C + + programs

#include <iostream> #include <fstream> #include <cstdlib> #include <cstring> #include <sys/ socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/types.h> #include < unistd.h>using namespace std; #define DEST_IP "10.209.177.22" #define Dest_port 80#define max_data_size 1024int Main ()        {int ret;        int sockfd;        struct sockaddr_in dest_addr;        memset (&dest_addr, 0x00, sizeof (sockaddr_in));        dest_addr.sin_family = af_inet;        DEST_ADDR.SIN_ADDR.S_ADDR = inet_addr (DEST_IP);        Dest_addr.sin_port = htons (Dest_port);        cout << "dest addr IP:" << inet_ntoa (dest_addr.sin_addr) << Endl;        SOCKFD = socket (af_inet, sock_stream, ipproto_tcp);                if (SOCKFD < 0) {cout << "create socket fail!" << Endl;        Exit (1);        ret = Connect (SOCKFD, (struct sockaddr *) &dest_addr, sizeof (struct sockaddr)); if (ret! =0) {cout << "Connect server fail!" << Endl;                Close (SOCKFD);        Exit (1);        } else {cout << connect server success! << Endl;        } cout << Endl;        int Sendlen, Recvlen;        Char Sendbuf[max_data_size] = {0};        Char Recvbuf[max_data_size] = {0};        String Body ("user=hello&password=123456");        int content_length = Body.length (); snprintf (sendbuf, sizeof (SENDBUF)-1, "post/api.php http/1.1\r\n" "host:10.209.177.22\r\n" "Conten        T-type:application/x-www-form-urlencoded\r\n "" Content-length:%d\r\n ", content_length);        strcat (SendBuf, "\ r \ n");        strcat (SendBuf, Body.c_str ());        Sendlen = Send (SOCKFD, sendbuf, sizeof (SENDBUF), 0);                if (Sendlen < 0) {cout << "send fail" << Endl;                Close (SOCKFD);        Exit (1); } if ((Recvlen = RECV (SOCKFD, Recvbuf, sizeof (RECVBUF), 0)) = =-1) {cout << "recv fail" << Endl;                Close (SOCKFD);        Exit (1);        } else {cout << recvbuf << Endl;        } close (SOCKFD); return 0;}

Second, the background PHP test procedures

<?php$input = file_get_contents ("Php://input"); Var_dump ($input); if ($_post[' user '] = = "Hello" && $_post[' Password '] = = "123456") {    echo "welcome Hello";} else {    echo "Welcome Guest";}? >

Third, the realization effect


, after the client C + + program, post data to the background nginx+php, PHP obtains post data in the first two ways:

Method 1, the most common method is: $_post[' fieldname '];
Note: Only data submitted by content-type:application/x-www-form-urlencoded can be received.
Explanation: That is, the data that the form post comes from.

Method 2, File_get_contents ("Php://input");
Description
Allows the raw data to be read from the POST.
Compared to $HTTP _raw_post_data, it brings less pressure to memory and does not require any special php.ini settings.
Php://input cannot be used for enctype= "Multipart/form-data".
Explain:
For post data that does not specify Content-type, you can use File_get_contents ("Php://input") to get the raw data.
In fact, this method can be used for any data that is received by the post in PHP. Regardless of the Content-type, including binary file streams is also possible.

So using method Two is the most insurance method.

Method 3, $GLOBALS [' Http_raw_post_data '];

Description
Always produces $HTTP _raw_post_data variable contains the original POST data.
This variable is only generated when it encounters data that does not recognize the MIME type.
$HTTP _raw_post_data is not available for enctype= ' multipart/form-data ' form data
If the POST data is not recognized by PHP, it can be received $GLOBALS [' Http_raw_post_data '].
Like text/xml or SOAP, etc.
Explain:
The $GLOBALS [' Http_raw_post_data '] stores the raw data that came back from the POST.
$_post or $_request stores the data in PHP in the form of a key=>value format.

But whether or not the data in $globals[' Http_raw_post_data ' is saved depends on the settings of the Content-type, that is, the post data must be explicitly indicated content-type:application/ X-www-form-urlencoded,post data is stored in the $GLOBALS [' Http_raw_post_data '].

For uploading files, use post enctype= "Multipart/form-data". Sample PHP Backend Code:

<! Doctype>

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.