{"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":"<p>\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<p>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<p>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<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.cdn.yimenapp.com\/2023-04\/06\/11\/21420312434.jpg\" \/><\/figure>\n<\/p>\n<p>\u540d\u3001\u5bc6\u7801\u7b49\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`SQL<\/p>\n<p>CREATE DATABASE userDB;<\/p>\n<p>USE userDB;<\/p>\n<p>CREATE TABLE userInfo (<\/p>\n<p>  id INT AUTO_INCREMENT PRIMARY KEY,<\/p>\n<p>  username VARCHAR(50) NOT NULL UNIQUE,<\/p>\n<p>  passwor<a href=\"https:\/\/www.anzhuoe.com\/\">\u5b89\u5353app\u5236\u4f5c<\/a>d VARCHAR(255) NOT NULL<\/p>\n<p>);<\/p>\n<p>&#8220;`<\/p>\n<p>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&#8221;user_api.php&#8221;\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<p>&#8220;`PHP<\/p>\n<\/p>\n<p>  $username = $_POST[&#8216;username&#8217;];<\/p>\n<p>  $password = $_POST[&#8216;password&#8217;];<\/p>\n<p>  \/\/ \u6570\u636e\u9a8c\u8bc1\u548c\u5904\u7406&#8230;<\/p>\n<p>  \/\/ \u8fd4\u56de\u6570\u636e\u7ed9Android\u7aef&#8230;<\/p>\n<p>?&gt;<\/p>\n<p>&#8220;`<\/p>\n<p>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<p>&#8220;`PHP<\/p>\n<\/p>\n<p>  \/\/ \u6570\u636e\u5e93\u8fde\u63a5\u914d\u7f6e<\/p>\n<p>  $host = &#8216;localhost&#8217;;<\/p>\n<p>  $user = &#8216;root&#8217;;<\/p>\n<p>  $pass = &#8216;password&#8217;;<\/p>\n<p>  $dbname = &#8216;userDB&#8217;;<\/p>\n<p>  \/\/ \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p>  $mysqli = new mysqli($host, $user, $pass, $dbname);<\/p>\n<p>  \/\/ \u68c0\u67e5\u8fde\u63a5\u662f\u5426\u6210\u529f<\/p>\n<p>  if ($mysqli-&gt;connect_error) {<\/p>\n<p>    die(&#8216;Connection failed: &#8216; . $mysqli-&gt;connect_error);<\/p>\n<p>  }<\/p>\n<p>  \/\/ \u67e5\u8be2\u7528\u6237\u4fe1\u606f<\/p>\n<p>  $sql = &#8220;SELECT * FROM userInfo WHERE username = ? AND password = ?&#8221;;<\/p>\n<p>  $stmt = $mysqli-&gt;prepare($sql);<\/p>\n<p>  $stmt-&gt;bind_param(&#8216;ss&#8217;, $username, $password);<\/p>\n<p>  $stmt-&gt;execute();<\/p>\n<p>  $result = $stmt-&gt;get_result();<\/p>\n<p>  \/\/ \u6570\u636e\u9a8c\u8bc1\u548c\u5904\u7406&#8230;<\/p>\n<p>  \/\/ \u8fd4\u56de\u6570\u636e\u7ed9Android\u7aef&#8230;<\/p>\n<p>  \/\/ \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p>  $stmt-&gt;close();<\/p>\n<p>  $mysqli-&gt;close();<\/p>\n<p>?&gt;<\/p>\n<p>&#8220;`<\/p>\n<p>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<p>&#8220;`PHP<\/p>\n<\/p>\n<p>  \/\/ \u67e5\u8be2\u7ed3\u679c<\/p>\n<p>  if ($result-&gt;num_rows &gt; 0) {<\/p>\n<p>    $response = array(&#8216;status&#8217; =&gt; &#8216;success&#8217;, &#8216;msg&#8217; =&gt; &#8216;\u767b\u5f55\u6210\u529f&#8217;);<\/p>\n<p>  } else {<\/p>\n<p>    $response = array(&#8216;status&#8217; =&gt; &#8216;failed&#8217;, &#8216;msg&#8217; =&gt; <a href=\"https:\/\/www.anzhuoe.com\/\">\u5b89\u5353app\u5f00\u53d1\u5de5\u5177<\/a>&#8216;\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef&#8217;);<\/p>\n<p>  }<\/p>\n<p>  \/\/ \u8fd4\u56deJSON\u6570\u636e\u7ed9Android\u7aef<\/p>\n<p>  echo json_encode($response);<\/p>\n<p>?&gt;<\/p>\n<p>&#8220;`<\/p>\n<p>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<p>\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":"<p>\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}]}}