{"id":16683,"date":"2023-12-12T11:46:41","date_gmt":"2023-12-12T03:46:41","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=16683"},"modified":"2023-12-12T11:46:41","modified_gmt":"2023-12-12T03:46:41","slug":"php%e5%bc%80%e5%8f%91%e5%ae%89%e5%8d%93app%e6%8e%a5%e5%8f%a3%e8%a6%81%e6%80%8e%e4%b9%88%e5%81%9a%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/16683\/","title":{"rendered":"php\u5f00\u53d1\u5b89\u5353app\u63a5\u53e3\u8981\u600e\u4e48\u505a\uff1f"},"content":{"rendered":"

\u5728\u73b0\u4ee3\u4e92\u8054\u7f51\u5e94\u7528\u7684\u5f00\u53d1\u4e2d\uff0c\u4f7f\u7528PHP\u6765\u5f00\u53d1\u540e\u7aefAPI\u63a5\u53e3\u5c06\u6570\u636e\u63d0\u4f9b\u7ed9Android\u5e94\u7528\u5df2\u7ecf\u975e\u5e38\u666e\u904d\u3002\u8fd9\u79cd\u67b6\u6784\u65b9\u5f0f\u7684\u4f18\u52bf\u5728\u4e8e\uff0c\u9879\u76ee\u5404\u4e2a\u90e8\u5206\u4e4b\u95f4\u7684\u89e3\u8026\uff0c\u5feb\u901f\u8fed\u4ee3\uff0c\u4ee5\u53ca\u51cf\u8f7b\u5ba2\u6237\u7aef\u7684\u5f00\u53d1\u538b\u529b\u3002\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PHP\u6765\u4e3aAndroid\u5e94\u7528\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684API\u63a5\u53e3\u3002<\/p>\n

1. \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e3aAPI\u63a5\u53e3\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u670d\u52a1\u5668\u73af\u5883\u3002\u76ee\u524d\u6d41\u884c\u7684\u6709LAMP\uff08Linux, Apache, MySQL, PHP\uff09\u3001LNMP\uff08Linux, Nginx, MySQL, PHP\uff09\u3001WAMP\uff08Windows, Apache, MySQL, PHP\uff09\u7b49\u73af\u5883\u3002\u6839\u636e\u60a8\u7684\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u4e00\u4e2a\u8212\u9002\u7684\u5f00\u53d1\u73af\u5883\u3002<\/p>\n

2. \u5176\u6b21\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6e\u597d\u6570\u636e\u5e93\u3002\u8fd9\u91cc\u4ee5MySQL\u4e3a\u4f8b\u3002\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\uff0c\u7528\u4e8e\u5b58\u50a8\u7528\u6237\u7684\u4fe1\u606f\u3002\u5728\u8fd9\u4e2a\u6570\u636e\u5e93\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u8868\u6765\u5b58\u50a8\u7528\u6237\u7684ID\u3001\u7528\u6237<\/p>\n

<\/figure>\n<\/p>\n

\u540d\u3001\u5bc6\u7801\u7b49\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n

“`SQL<\/p>\n

CREATE DATABASE userDB;<\/p>\n

USE userDB;<\/p>\n

CREATE TABLE userInfo (<\/p>\n

id INT AUTO_INCREMENT PRIMARY KEY,<\/p>\n

username VARCHAR(50) NOT NULL UNIQUE,<\/p>\n

passwor\u5b89\u5353app\u5236\u4f5c<\/a>d VARCHAR(255) NOT NULL<\/p>\n

);<\/p>\n

“`<\/p>\n

3. \u4f7f\u7528PHP\u6765\u7f16\u5199API\u63a5\u53e3\u3002\u5728\u8fd9\u4e00\u6b65\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aPHP\u6587\u4ef6\uff0c\u4f8b\u5982”user_api.php”\u3002\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u5904\u7406Android\u5e94\u7528\u4f20\u9012\u8fc7\u6765\u7684\u6570\u636e\u3002\u4e00\u822c\u6765\u8bf4\uff0cAndroid\u5e94\u7528\u4f1a\u4ee5POST\u6216GET\u7684\u65b9\u5f0f\u4f20\u9012\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528`$_POST`\u6216`$_GET`\u6570\u7ec4\u6765\u63a5\u6536\u3002\u4f8b\u5982\uff0c\u9488\u5bf9\u7528\u6237\u767b\u5f55\u7684API\u63a5\u53e3\uff1a<\/p>\n

“`PHP<\/p>\n<\/p>\n

$username = $_POST[‘username’];<\/p>\n

$password = $_POST[‘password’];<\/p>\n

\/\/ \u6570\u636e\u9a8c\u8bc1\u548c\u5904\u7406…<\/p>\n

\/\/ \u8fd4\u56de\u6570\u636e\u7ed9Android\u7aef…<\/p>\n

?><\/p>\n

“`<\/p>\n

4. \u8fde\u63a5\u6570\u636e\u5e93\u3002\u5728\u63a5\u6536\u5230Android\u7aef\u4f20\u9012\u6765\u7684\u6570\u636e\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u8fde\u63a5MySQL\u6570\u636e\u5e93\uff0c\u5e76\u67e5\u8be2\u4e0e\u4e4b\u76f8\u5173\u7684\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PHP\u7684PDO\u6216MySQLi\u6269\u5c55\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n

“`PHP<\/p>\n<\/p>\n

\/\/ \u6570\u636e\u5e93\u8fde\u63a5\u914d\u7f6e<\/p>\n

$host = ‘localhost’;<\/p>\n

$user = ‘root’;<\/p>\n

$pass = ‘password’;<\/p>\n

$dbname = ‘userDB’;<\/p>\n

\/\/ \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n

$mysqli = new mysqli($host, $user, $pass, $dbname);<\/p>\n

\/\/ \u68c0\u67e5\u8fde\u63a5\u662f\u5426\u6210\u529f<\/p>\n

if ($mysqli->connect_error) {<\/p>\n

die(‘Connection failed: ‘ . $mysqli->connect_error);<\/p>\n

}<\/p>\n

\/\/ \u67e5\u8be2\u7528\u6237\u4fe1\u606f<\/p>\n

$sql = “SELECT * FROM userInfo WHERE username = ? AND password = ?”;<\/p>\n

$stmt = $mysqli->prepare($sql);<\/p>\n

$stmt->bind_param(‘ss’, $username, $password);<\/p>\n

$stmt->execute();<\/p>\n

$result = $stmt->get_result();<\/p>\n

\/\/ \u6570\u636e\u9a8c\u8bc1\u548c\u5904\u7406…<\/p>\n

\/\/ \u8fd4\u56de\u6570\u636e\u7ed9Android\u7aef…<\/p>\n

\/\/ \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n

$stmt->close();<\/p>\n

$mysqli->close();<\/p>\n

?><\/p>\n

“`<\/p>\n

5. \u6570\u636e\u9a8c\u8bc1\u548c\u5904\u7406\u3002\u5728\u67e5\u8be2\u5230\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u9a8c\u8bc1\u7528\u6237\u7684\u767b\u5f55\u662f\u5426\u6210\u529f\uff0c\u5e76\u6839\u636e\u9a8c\u8bc1\u7ed3\u679c\u751f\u6210\u76f8\u5e94\u7684JSON\u6570\u636e\u8fd4\u56de\u7ed9Android\u5e94\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n

“`PHP<\/p>\n<\/p>\n

\/\/ \u67e5\u8be2\u7ed3\u679c<\/p>\n

if ($result->num_rows > 0) {<\/p>\n

$response = array(‘status’ => ‘success’, ‘msg’ => ‘\u767b\u5f55\u6210\u529f’);<\/p>\n

} else {<\/p>\n

$response = array(‘status’ => ‘failed’, ‘msg’ => \u5b89\u5353app\u5f00\u53d1\u5de5\u5177<\/a>‘\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef’);<\/p>\n

}<\/p>\n

\/\/ \u8fd4\u56deJSON\u6570\u636e\u7ed9Android\u7aef<\/p>\n

echo json_encode($response);<\/p>\n

?><\/p>\n

“`<\/p>\n

6. \u6700\u540e\uff0c\u5728Android\u5e94\u7528\u7aef\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u76f8\u5e94\u7684\u4ee3\u7801\u6765\u5904\u7406\u7528\u6237\u767b\u5f55\uff0c\u53d1\u9001\u8bf7\u6c42\u7ed9\u6211\u4eec\u7684PHP API\u63a5\u53e3\uff0c\u5e76\u89e3\u6790\u8fd4\u56de\u7684JSON\u6570\u636e\u3002\u8fd9\u91cc\u63a8\u8350\u4f7f\u7528Volley\u6216Retrofit\u7b49\u7f51\u7edc\u5e93\u6765\u7b80\u5316\u8bf7\u6c42\u548c\u6570\u636e\u5904\u7406\u3002<\/p>\n

\u81f3\u6b64\uff0c\u6211\u4eec\u5b8c\u6210\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u6237\u767b\u5f55\u529f\u80fd\u7684API\u63a5\u53e3\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u7684\u5b9e\u9645\u9700\u6c42\u7f16\u5199\u66f4\u590d\u6742\u7684\u63a5\u53e3\u3002\u540c\u65f6\uff0c\u8003\u8651\u5230\u5b89\u5168\u6027\uff0c\u60a8\u8fd8\u5e94\u5f53\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u4f7f\u7528HTTPS\u52a0\u5bc6\u4f20\u8f93\u6570\u636e\uff0c\u4e3a\u6570\u636e\u5e93\u8bbe\u7f6e\u5408\u7406\u7684\u6743\u9650\uff0c\u4ee5\u53ca\u91c7\u7528\u66f4\u5b89\u5168\u7684\u5bc6\u7801\u5b58\u50a8\u65b9\u5f0f\uff0c\u5982\u54c8\u5e0c\u52a0\u76d0\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u53ef\u4ee5\u5e2e\u52a9\u60a8\u642d\u5efa\u4e00\u4e2a\u57fa\u672c\u7684PHP\u5f00\u53d1Android\u5e94\u7528\u63a5\u53e3\u7684\u539f\u7406\u548c\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u73b0\u4ee3\u4e92\u8054\u7f51\u5e94\u7528\u7684\u5f00\u53d1\u4e2d\uff0c\u4f7f\u7528PHP\u6765\u5f00\u53d1\u540e\u7aefAPI\u63a5\u53e3\u5c06\u6570\u636e\u63d0\u4f9b\u7ed9Android\u5e94\u7528\u5df2\u7ecf\u975e\u5e38\u666e\u904d\u3002\u8fd9\u79cd\u67b6\u6784\u65b9\u5f0f\u7684\u4f18\u52bf\u5728\u4e8e\uff0c\u9879\u76ee\u5404\u4e2a\u90e8\u5206\u4e4b\u95f4\u7684\u89e3\u8026\uff0c\u5feb\u901f\u8fed\u4ee3\uff0c\u4ee5\u53ca\u51cf\u8f7b\u5ba2\u6237\u7aef\u7684\u5f00\u53d1\u538b\u529b\u3002\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c<\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[23252,14018,789,95,23253],"class_list":{"0":"post-16683","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-zuoapp","7":"tag-app","9":"tag-789","10":"tag-95","11":"tag-23253"},"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/16683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=16683"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/16683\/revisions"}],"predecessor-version":[{"id":16713,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/16683\/revisions\/16713"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=16683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=16683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=16683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}