时间:2021-07-01 10:21:17 帮助过:13人阅读
一、手机APP(客户端)程序接口
这里采用在PC上使用C++程序模拟HTTP协议数据的POST
#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 1024
int 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"
"Content-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;
}二、后台PHP测试程序
<?php
$input = file_get_contents("php://input");
var_dump($input);
if ($_POST['user'] == "hello" && $_POST['password'] == "123456") {
echo "welcome hello";
} else {
echo "welcome guest";
}
?>三、实现效果

上图中,客户端C++程序,POST数据给后台Nginx+PHP后,PHP通过以下前两种方式获取POST的数据:
方法1、最常见的方法是:$_POST['fieldname'];
说明:只能接收Content-Type: application/x-www-form-urlencoded提交的数据。
解释:也就是表单POST过来的数据。
方法2、file_get_contents("php://input");
说明:
允许读取 POST 的原始数据。
和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小,并且不需要任何特殊的 php.ini 设置。
php://input 不能用于 enctype="multipart/form-data"。
解释:
对于未指定 Content-Type 的POST数据,则可以使用file_get_contents(“php://input”);来获取原始数据。
事实上,用PHP接收POST的任何数据都可以使用本方法。而不用考虑Content-Type,包括二进制文件流也可以。
所以用方法二是最保险的方法。
方法3、$GLOBALS['HTTP_RAW_POST_DATA'];
说明:
总是产生 $HTTP_RAW_POST_DATA 变量包含有原始的 POST 数据。
此变量仅在碰到未识别 MIME 类型的数据时产生。
$HTTP_RAW_POST_DATA 对于 enctype="multipart/form-data" 表单数据不可用
如果post过来的数据不是PHP能够识别的,可以用 $GLOBALS['HTTP_RAW_POST_DATA']来接收,
比如 text/xml 或者 soap 等等
解释:
$GLOBALS['HTTP_RAW_POST_DATA']存放的是POST过来的原始数据。
$_POST或$_REQUEST存放的是 PHP以key=>value的形式格式化以后的数据。
但$GLOBALS['HTTP_RAW_POST_DATA']中是否保存POST过来的数据取决于Content-Type的设置,即POST数据时 必须显式示指明Content-Type: application/x-www-form-urlencoded,POST的数据才会存放到 $GLOBALS['HTTP_RAW_POST_DATA']中。
对于上传文件,采用POST enctype="multipart/form-data"。PHP后端代码示例:
<!DOCTYPE>
<html>
<body>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" value="submit" />
</form>
<?php
echo "<pre>";
print_r($_FILES);
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
$file = fopen($_FILES["file"]["tmp_name"], "r");
while (!feof($file)) {
echo fgetc($file);
}
fclose($file);
}
?>
</body>
</html>相关推荐:
PHP开发APP接口需要注意哪些问题
laravel编写APP接口(API)
关于PHP写APP接口的安全问题探讨一
以上就是PHP后台与手机APP接口开发实例代码的详细内容,更多请关注Gxl网其它相关文章!