{"id":16460,"date":"2024-01-30T13:08:19","date_gmt":"2024-01-30T05:08:19","guid":{"rendered":"https:\/\/www.zhidianwl.net\/zhidianwl\/?p=16460"},"modified":"2024-01-30T13:08:19","modified_gmt":"2024-01-30T05:08:19","slug":"app-%e5%bc%80%e5%8f%91-php%e5%8f%af%e4%bb%a5%e5%81%9aapp%e5%98%9b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.net\/zhidianwl\/2024\/01\/30\/app-%e5%bc%80%e5%8f%91-php%e5%8f%af%e4%bb%a5%e5%81%9aapp%e5%98%9b%ef%bc%9f\/","title":{"rendered":"app \u5f00\u53d1 php\u53ef\u4ee5\u505aAPP\u561b\uff1f"},"content":{"rendered":"

App \u662f\u6307 “Application” \u7684\u7f29\u5199\uff0c\u662f\u6307\u80fd\u591f\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u5b89\u88c5\u548c\u8fd0\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\u3002PHP \u662f\u6307 “Hypertext Preprocessor” \u7684\u7f29\u5199\uff0c\u662f\u4e00\u79cd\u5f00\u6e90\u7684\u670d\u52a1\u5668\u7aef\u811a\u672c\u8bed\u8a00\u3002\u5728\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528 PHP \u5f00\u53d1 Web \u5e94\u7528\u7a0b\u5e8f\u6216\u8005 REST API\u3002\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 PHP \u5f00\u53d1 App\u3002<\/p>\n

\u4e00\u3001App \u5f00\u53d1\u9700\u8981\u7684\u6280\u672f<\/p>\n

1. PHP<\/p>\n

PHP \u662f\u4e00\u79cd\u5e7f\u6cdb\u5e94\u7528\u4e8e Web \u5f00\u53d1\u4e2d\u7684\u670d\u52a1\u5668\u7aef\u8bed\u8a00\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5f00\u53d1 API \u4ee5\u53ca\u79fb\u52a8\u5e94\u7528\u7a0b\u5e8f\u7684\u540e\u7aef\u3002PHP \u53ef\u4ee5\u5904\u7406\u8868\u5355\u6570\u636e\u3001\u751f\u6210\u52a8\u6001\u9875\u9762\u548c\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u3002<\/p>\n

2. MySQL<\/p>\n

MySQL \u662f\u4e00\u79cd\u5f00\u6e90\u5173\u7cfb\u578b\u6570\u636e\u5e93\u8f6f\u4ef6\uff0c\u662f PHP \u5f00\u53d1\u8fc7\u7a0b\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u5e93\u4e4b\u4e00\u3002MySQL \u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e\uff0c\u6bd4\u5982\u7528\u6237\u4fe1\u606f\u3001\u6587\u7ae0\u4fe1\u606f\u7b49\u3002<\/p>\n

3APP\u5f00\u53d1<\/a>. RESTful API<\/p>\n

API \u662f\u6307\u5e94\u7528\u7a0b\u5e8f\u63a5\u53e3\uff0c\u662f\u7528\u4e8e\u5728\u4e0d\u540c\u7684\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u4e92\u76f8\u901a\u8baf\u7684\u4e00\u7ec4\u89c4\u8303\u3002RESTful API \u662f\u4e00\u79cd\u4f7f\u7528 HTTP \u534f\u8bae\u8fdb\u884c\u901a\u4fe1\u7684 API\uff0c\u53ef\u7528\u4e8e\u5728\u4e0d\u540c\u7684\u5e73\u53f0\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u4ee5\u53ca\u589e\u5f3a\u5e94\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u3002<\/p>\n

4. HTML\u3001CSS \u548c JavaScript<\/p>\n

HTML\u3001CSS \u548c JavaScript \u662f Web \u5f00\u53d1\u5fc5\u5907\u7684\u524d\u7aef\u6280\u672f\uff0c\u4e5f\u88ab\u7528\u4e8e\u5f00\u53d1\u8de8\u5e73\u53f0\u5e94\u7528\u7a0b\u5e8f\u3002HTML \u8d1f\u8d23\u9875\u9762\u7ed3\u6784\uff0cCSS \u8d1f\u8d23\u9875\u9762\u6837\u5f0f\uff0cJavaScript \u8d1f\u8d23\u9875\u9762\u4ea4\u4e92\u548c\u52a8\u6001\u6548\u679c\u3002<\/p>\n

\u4e8c\u3001\u4f7f\u7528 PHP \u5f00\u53d1 App<\/p>\n

1. \u521b\u5efa API<\/p>\n

\u5728\u5f00\u53d1 App \u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u521b\u5efa\u4e00\u4e2a RESTful API\uff0c\u5b83\u8d1f\u8d23\u548c\u5ba2\u6237\u7aef\u5e94\u7528\u7a0b\u5e8f\u901a\u8baf\u5e76\u63d0\u4f9b\u6570\u636e\u3002\u4f7f\u7528 PHP \u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u521b\u5efa RESTful API\u3002\u4ee5\u4e0b\u662f\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n

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

header(“Content-Type: application\/json”);<\/p>\n

$method = $_SERVER[‘REQUEST_METHOD’];<\/p>\n

if ($method == ‘GET’) {<\/p>\n

$response = array(‘success’ => true, ‘message’ => ‘Hello World’);<\/p>\n

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

} else {<\/p>\n

$response = array(‘success’ => false, ‘message’ => ‘Invalid Request Method’);<\/p>\n

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

}<\/p>\n

?><\/p>\n

“`<\/p>\n

\u4e0a\u8ff0\u4ee3\u7801\u901a\u8fc7 HTTP GET \u8bf7\u6c42\u8fd4\u56de\u4e00\u4e2a\u7b80\u5355\u7684 JSON \u54cd\u5e94\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728 API \u7ec8\u7aef\u8bbf\u95ee\u6b64\u6587\u4ef6\u6765\u6d4b\u8bd5 API\u3002<\/p>\n

2. \u8fde\u63a5\u6570\u636e\u5e93<\/p>\n

\u5728\u5f00\u53d1 App \u65f6\uff0c\u901a\u5e38\u9700\u8981\u4e0e\u6570\u636e\u5e93\u6253\u4ea4\u9053\u3002PHP \u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u7f6e\u6570\u636e\u5e93\u63a5\u53e3\uff0c\u6700\u5e38\u7528\u7684\u662f PDO\u3002\u4f7f\u7528 PDO \u53ef\u4ee5\u8f7b\u677e\u5730\u8fde\u63a5 MySQL \u6570\u636e\u5e93\uff0c\u5e76\u8fdb\u884c\u589e\u5220\u6539\u67e5\u7b49\u4e00\u7cfb\u5217\u64cd\u4f5c\uff1a<\/p>\n

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

try {<\/p>\n

\/\/ \u6570\u636e\u5e93\u4fe1\u606f<\/p>\n

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

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

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

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

$dsn = “mysql:host=$host;dbname=$dbname”;<\/p>\n

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

$pdo = new PDO($dsn, $user, $password);<\/p>\n

\/\/ \u67e5\u8be2\u6570\u636e<\/p>\n

$statement = $pdo->APP<\/a>prepare(‘SELECT * FROM mytable’);<\/p>\n

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

$result = $statement->fetchAll(PDO::FETCH_ASSOC);<\/p>\n

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

$pdo = null;<\/p>\n

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

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

} catch (PDOException $error) {<\/p>\n

echo $error->getMessage();<\/p>\n

}<\/p>\n

?><\/p>\n

“`<\/p>\n

3. \u6784\u5efa\u5e94\u7528<\/p>\n

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

\u7a0b\u5e8f<\/p>\n

\u5728\u8fde\u63a5\u6570\u636e\u5e93\u548c\u521b\u5efa API \u5e76\u63d0\u4f9b\u6570\u636e\u540e\uff0c\u6211\u4eec\u9700\u8981\u6784\u5efa\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u6765\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u3002\u5e38\u7528\u7684 App \u5f00\u53d1\u6846\u67b6\u6709 React Native\u3001Ionic \u548c Flutter \u7b49\u3002\u6846\u67b6\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7ec4\u4ef6\u3001\u6a21\u5757\u548c\u5de5\u5177\uff0c\u4f7f\u5f00\u53d1\u8fc7\u7a0b\u66f4\u52a0\u5feb\u6377\u548c\u9ad8\u6548\u3002<\/p>\n

\u4f8b\u5982\uff0c\u4f7f\u7528 Ionic \u5f00\u53d1 App \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b CLI \u547d\u4ee4\u751f\u6210\u4e00\u4e2a\u6700\u5c0f\u53ef\u884c\u7684 Ionic \u9879\u76ee\uff1a<\/p>\n

“`bash<\/p>\n

ionic start myapp blank<\/p>\n

“`<\/p>\n

\u5728\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \/src\/pages\/ \u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u9875\u9762\u6765\u663e\u793a\u6570\u636e\u3002\u5bf9\u4e8e\u6570\u636e\u7684\u83b7\u53d6\uff0c\u6211\u4eec\u4f7f\u7528\u4e0a\u9762\u7684 API\u3002<\/p>\n

“`typescript<\/p>\n

\/\/ \u5f02\u6b65\u52a0\u8f7d\u6570\u636e<\/p>\n

loadData() {<\/p>\n

this.http.get(‘http:\/\/example.com\/api\/mydata’)<\/p>\n

.subscribe(data => {<\/p>\n

this.myData = data;<\/p>\n

}, error => {<\/p>\n

console.log(error);<\/p>\n

return;<\/p>\n

});<\/p>\n

}<\/p>\n

“`<\/p>\n

\u5728\u5e94\u7528\u7a0b\u5e8f\u6784\u5efa\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u5e76\u53d1\u5e03\u5230\u5e94\u7528\u5546\u5e97\u6216 App Store \u4e0a\uff0c\u8ba9\u7528\u6237\u5b89\u88c5\u5e76\u4f7f\u7528\u3002<\/p>\n

\u4e09\u3001\u7ed3\u8bba<\/p>\n

\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528 PHP \u5f00\u53d1 App \u7684\u57fa\u672c\u6280\u672f\u548c\u5f00\u53d1\u7ecf\u9a8c\uff0c\u8fd9\u4e9b\u7ecf\u9a8c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u521b\u5efa\u9ad8\u8d28\u91cf\u3001\u53ef\u9760\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5e94\u8be5\u5c3d\u53ef\u80fd\u5730\u5229\u7528\u73b0\u6709\u6846\u67b6\u548c\u5de5\u5177\uff0c\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u5e94\u7528\u7a0b\u5e8f\u7684\u8d28\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

App \u662f\u6307 \\”Application\\” \u7684\u7f29\u5199\uff0c\u662f\u6307\u80fd\u591f\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u5b89\u88c5\u548c\u8fd0\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\u3002PHP \u662f\u6307 \\”Hypertext Preprocessor\\” \u7684\u7f29\u5199\uff0c\u662f\u4e00\u79cd\u5f00\u6e90\u7684\u670d\u52a1\u5668\u7aef\u811a\u672c\u8bed\u8a00\u3002\u5728\u8f6f<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[116,271,281,1469,8923],"topic":[],"class_list":["post-16460","post","type-post","status-publish","format-standard","hentry","category-kaifaapp","tag-116","tag-271","tag-281","tag-app"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/16460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/comments?post=16460"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/16460\/revisions"}],"predecessor-version":[{"id":16478,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/16460\/revisions\/16478"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/media?parent=16460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/categories?post=16460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/tags?post=16460"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/topic?post=16460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}