{"id":167057,"date":"2024-11-05T22:00:20","date_gmt":"2024-11-05T14:00:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/167057\/"},"modified":"2024-11-05T22:00:21","modified_gmt":"2024-11-05T14:00:21","slug":"%e5%88%a9%e7%94%a8php%e5%b0%87%e6%96%b0%e6%b5%aa%e5%be%ae%e5%8d%9a%e5%85%a7%e5%ae%b9%e5%84%b2%e5%ad%98%e8%87%b3%e6%95%b8%e6%93%9a%e5%ba%ab-php%e7%8d%b2%e5%8f%96%e6%96%b0%e6%b5%aa%e5%be%ae%e5%8d%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/167057\/","title":{"rendered":"\u5229\u7528PHP\u5c07\u65b0\u6d6a\u5fae\u535a\u5167\u5bb9\u5132\u5b58\u81f3\u6578\u64da\u5eab (php\u7372\u53d6\u65b0\u6d6a\u5fae\u535a\u5167\u5bb9\u5b58\u5165\u6578\u64da\u5eab\u4e2d)"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8php%e5%b0%87%e6%96%b0%e6%b5%aa%e5%be%ae%e5%8d%9a%e5%85%a7%e5%ae%b9%e5%84%b2%e5%ad%98%e8%87%b3%e6%95%b8%e6%93%9a%e5%ba%ab-dSRmsJbQSV\">\u5229\u7528PHP\u5c07\u65b0\u6d6a\u5fae\u535a\u5167\u5bb9\u5132\u5b58\u81f3\u6578\u64da\u5eab<\/h1>\n<p>\u96a8\u8457\u793e\u4ea4\u5a92\u9ad4\u7684\u666e\u53ca\uff0c\u8a31\u591a\u958b\u767c\u8005\u5e0c\u671b\u80fd\u5920\u5f9e\u5404\u5927\u5e73\u53f0\u7372\u53d6\u6578\u64da\u4ee5\u9032\u884c\u5206\u6790\u6216\u61c9\u7528\u958b\u767c\u3002\u65b0\u6d6a\u5fae\u535a\u4f5c\u70ba\u4e2d\u570b\u6700\u5927\u7684\u793e\u4ea4\u5a92\u9ad4\u5e73\u53f0\u4e4b\u4e00\uff0c\u64c1\u6709\u6d77\u91cf\u7684\u7528\u6236\u751f\u6210\u5167\u5bb9\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5229\u7528PHP\u5f9e\u65b0\u6d6a\u5fae\u535a\u7372\u53d6\u5167\u5bb9\u4e26\u5c07\u5176\u5132\u5b58\u81f3\u6578\u64da\u5eab\u4e2d\u3002<\/p>\n<h2 id=\"%e6%ba%96%e5%82%99%e5%b7%a5%e4%bd%9c-dSRmsJbQSV\">\u6e96\u5099\u5de5\u4f5c<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u64c1\u6709\u4e00\u500b\u53ef\u7528\u7684PHP\u74b0\u5883\uff08\u5982XAMPP\u6216LAMP\uff09<\/li>\n<li>\u5b89\u88ddMySQL\u6578\u64da\u5eab<\/li>\n<li>\u8a3b\u518a\u65b0\u6d6a\u5fae\u535a\u958b\u767c\u8005\u8cec\u865f\u4e26\u5275\u5efa\u61c9\u7528\u4ee5\u7372\u53d6API\u5bc6\u9470<\/li>\n<\/ul>\n<h2 id=\"%e7%8d%b2%e5%8f%96%e6%96%b0%e6%b5%aa%e5%be%ae%e5%8d%9aapi%e5%af%86%e9%91%b0-dSRmsJbQSV\">\u7372\u53d6\u65b0\u6d6a\u5fae\u535aAPI\u5bc6\u9470<\/h2>\n<p>\u8981\u5f9e\u65b0\u6d6a\u5fae\u535a\u7372\u53d6\u6578\u64da\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u5176API\u3002\u9996\u5148\uff0c\u8a2a\u554f<a href=\"https:\/\/open.weibo.com\/\" rel=\"nofollow noopener\" target=\"_blank\">\u65b0\u6d6a\u5fae\u535a\u958b\u653e\u5e73\u53f0<\/a>\uff0c\u8a3b\u518a\u4e26\u5275\u5efa\u4e00\u500b\u61c9\u7528\u3002\u5b8c\u6210\u5f8c\uff0c\u60a8\u5c07\u7372\u5f97\u4ee5\u4e0b\u4fe1\u606f\uff1a<\/p>\n<ul>\n<li>App Key<\/li>\n<li>App Secret<\/li>\n<li>Redirect URI<\/li>\n<\/ul>\n<h2 id=\"%e4%bd%bf%e7%94%a8php%e7%8d%b2%e5%8f%96%e5%be%ae%e5%8d%9a%e5%85%a7%e5%ae%b9-dSRmsJbQSV\">\u4f7f\u7528PHP\u7372\u53d6\u5fae\u535a\u5167\u5bb9<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u4f7f\u7528PHP\u4f86\u8abf\u7528\u65b0\u6d6a\u5fae\u535a\u7684API\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u7372\u53d6\u7528\u6236\u7684\u5fae\u535a\u5167\u5bb9\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u8a2d\u7f6eAPI\u5bc6\u9470\n$app_key = 'YOUR_APP_KEY';\n$app_secret = 'YOUR_APP_SECRET';\n$access_token = 'YOUR_ACCESS_TOKEN'; \/\/ \u7372\u53d6\u7528\u6236\u6388\u6b0a\u5f8c\u7372\u5f97\n\n\/\/ \u8a2d\u7f6eAPI\u8acb\u6c42URL\n$url = 'https:\/\/api.weibo.com\/2\/statuses\/user_timeline.json';\n$params = array(\n    'access_token' =&gt; $access_token,\n    'count' =&gt; 10 \/\/ \u7372\u53d6\u6700\u65b010\u689d\u5fae\u535a\n);\n\n\/\/ \u767c\u9001GET\u8acb\u6c42\n$response = file_get_contents($url . '?' . http_build_query($params));\n$data = json_decode($response, true);\n\n\/\/ \u6aa2\u67e5\u662f\u5426\u6210\u529f\u7372\u53d6\u6578\u64da\nif (isset($data['statuses'])) {\n    foreach ($data['statuses'] as $status) {\n        echo $status['text'] . '&lt;br&gt;';\n    }\n} else {\n    echo '\u7121\u6cd5\u7372\u53d6\u5fae\u535a\u5167\u5bb9';\n}\n?&gt;<\/code><\/pre>\n<h2 id=\"%e5%b0%87%e5%be%ae%e5%8d%9a%e5%85%a7%e5%ae%b9%e5%84%b2%e5%ad%98%e8%87%b3%e6%95%b8%e6%93%9a%e5%ba%ab-dSRmsJbQSV\">\u5c07\u5fae\u535a\u5167\u5bb9\u5132\u5b58\u81f3\u6578\u64da\u5eab<\/h2>\n<p>\u5728\u7372\u53d6\u5230\u5fae\u535a\u5167\u5bb9\u5f8c\uff0c\u60a8\u53ef\u4ee5\u5c07\u5176\u5132\u5b58\u81f3MySQL\u6578\u64da\u5eab\u4e2d\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b\u6578\u64da\u8868\u4f86\u5b58\u5132\u5fae\u535a\u5167\u5bb9\uff1a<\/p>\n<pre><code>CREATE TABLE weibo_posts (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    post_id VARCHAR(255) NOT NULL,\n    content TEXT NOT NULL,\n    created_at DATETIME DEFAULT CURRENT_TIMESTAMP\n);<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u5c07\u7372\u53d6\u5230\u7684\u5fae\u535a\u5167\u5bb9\u63d2\u5165\u5230\u6578\u64da\u5eab\u4e2d\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u6578\u64da\u5eab\u9023\u63a5\n$servername = \"localhost\";\n$username = \"YOUR_DB_USERNAME\";\n$password = \"YOUR_DB_PASSWORD\";\n$dbname = \"YOUR_DB_NAME\";\n\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u63d2\u5165\u5fae\u535a\u5167\u5bb9\nforeach ($data['statuses'] as $status) {\n    $post_id = $status['id'];\n    $content = $conn-&gt;real_escape_string($status['text']);\n    \n    $sql = \"INSERT INTO weibo_posts (post_id, content) VALUES ('$post_id', '$content')\";\n    \n    if ($conn-&gt;query($sql) === TRUE) {\n        echo \"\u65b0\u8a18\u9304\u63d2\u5165\u6210\u529f\";\n    } else {\n        echo \"\u932f\u8aa4: \" . $sql . \"&lt;br&gt;\" . $conn-&gt;error;\n    }\n}\n\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-dSRmsJbQSV\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u6210\u529f\u5730\u5229\u7528PHP\u5f9e\u65b0\u6d6a\u5fae\u535a\u7372\u53d6\u5167\u5bb9\u4e26\u5c07\u5176\u5132\u5b58\u81f3\u6578\u64da\u5eab\u4e2d\u3002\u9019\u4e0d\u50c5\u80fd\u5e6b\u52a9\u60a8\u9032\u884c\u6578\u64da\u5206\u6790\uff0c\u9084\u80fd\u70ba\u60a8\u7684\u61c9\u7528\u63d0\u4f9b\u8c50\u5bcc\u7684\u5167\u5bb9\u652f\u6301\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884c\u60a8\u7684\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u4ee5\u78ba\u4fdd\u60a8\u7684\u6578\u64da\u5b89\u5168\u548c\u9ad8\u6548\u5b58\u53d6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528PHP\u7372\u53d6\u65b0\u6d6a\u5fae\u535a\u5167\u5bb9\u4e26\u5c07\u5176\u5132\u5b58\u81f3\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u6578\u64da\u7ba1\u7406\u8207\u5206\u6790\u7684\u9ad8\u6548\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-167057","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=167057"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167057\/revisions"}],"predecessor-version":[{"id":167058,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167057\/revisions\/167058"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=167057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=167057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=167057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}