{"id":28363,"date":"2024-11-24T13:08:38","date_gmt":"2024-11-24T05:08:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/28363\/"},"modified":"2024-11-24T13:08:38","modified_gmt":"2024-11-24T05:08:38","slug":"%e5%9c%a8-laravel-%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e4%b8%ad%e9%9b%86%e6%88%90%e7%ac%ac%e4%b8%89%e6%96%b9-api-%e7%9a%84%e5%88%86%e6%ad%a5%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/28363\/","title":{"rendered":"\u5728 Laravel \u5e94\u7528\u7a0b\u5e8f\u4e2d\u96c6\u6210\u7b2c\u4e09\u65b9 API \u7684\u5206\u6b65\u6307\u5357"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173157012441099.jpg\" class=\"aligncenter\" title=\"\u5728 Laravel \u5e94\u7528\u7a0b\u5e8f\u4e2d\u96c6\u6210\u7b2c\u4e09\u65b9 API \u7684\u5206\u6b65\u6307\u5357\u63d2\u56fe\" alt=\"\u5728 Laravel \u5e94\u7528\u7a0b\u5e8f\u4e2d\u96c6\u6210\u7b2c\u4e09\u65b9 API \u7684\u5206\u6b65\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4e3b\u9898<\/strong>\uff1a\u3001api\u3001thirdpartyintegration\u3001web \u5f00\u53d1 php laraveltips apisinlaravel<\/p>\n<p>\u5c06\u7b2c\u4e09\u65b9 api \u96c6\u6210\u5230 laravel \u4e2d\u53ef\u4ee5\u901a\u8fc7\u5229\u7528\u5916\u90e8\u670d\u52a1\uff08\u4f8b\u5982\u652f\u4ed8\u3001\u6570\u636e\u68c0\u7d22\u7b49\uff09\u6765\u589e\u5f3a\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u6709\u6548\u96c6\u6210\u7b2c\u4e09\u65b9 api \u7684\u5206\u6b65\u6307\u5357\u548c\u793a\u4f8b\u3002<\/p>\n<h3> \u5148\u51b3\u6761\u4ef6\uff1a <\/h3>\n<ul>\n<li>\u4e00\u4e2a\u53ef\u4ee5\u8fd0\u884c\u7684 laravel \u5b89\u88c5\u3002<\/li>\n<li>\u7b2c\u4e09\u65b9 api\uff08\u6211\u4eec\u5c06\u4f7f\u7528\u5929\u6c14 api \u4f5c\u4e3a\u793a\u4f8b\uff09\u3002<\/li>\n<\/ul>\n<hr>\n<h3> \u7b2c 1 \u6b65\uff1a<strong>\u8bbe\u7f6e\u60a8\u7684 api \u5bc6\u94a5\u548c\u73af\u5883\u53d8\u91cf<\/strong> <\/h3>\n<p>\u9996\u5148\uff0c\u6ce8\u518c\u7b2c\u4e09\u65b9api\u5e76\u83b7\u53d6\u60a8\u7684api\u5bc6\u94a5\u3002\u5728 laravel \u7684 .env \u6587\u4ef6\u4e2d\u5b58\u50a8 api \u5bc6\u94a5\u7b49\u654f\u611f\u4fe1\u606f\u3002<\/p>\n<ol>\n<li> <strong>\u83b7\u53d6 api \u5bc6\u94a5<\/strong>\uff1a\u6ce8\u518c\u7b2c\u4e09\u65b9 api\uff08\u4f8b\u5982 openweathermap\uff09\u5e76\u68c0\u7d22\u60a8\u7684 api \u5bc6\u94a5\u3002<\/li>\n<li> <strong>\u6dfb\u52a0\u5230.env<\/strong>\uff1a <\/li>\n<\/ol>\n<pre>   weather_api_key=your_api_key_here\n   weather_api_url=https:\/\/api.openweathermap.org\/data\/2.5\/weather\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u6b65\u9aa4 2\uff1a<strong>\u5b89\u88c5 guzzle\uff08http \u5ba2\u6237\u7aef\uff09<\/strong> <\/h3>\n<p>laravel \u4f7f\u7528 guzzle\uff08\u4e00\u4e2a php http \u5ba2\u6237\u7aef\uff09\u6765\u53d1\u51fa http \u8bf7\u6c42\u3002\u5982\u679c\u60a8\u7684 laravel \u9879\u76ee\u4e2d\u5c1a\u672a\u5b89\u88c5 guzzle\uff0c\u8bf7\u5b89\u88c5\u5b83\uff1a<\/p>\n<pre>composer require guzzlehttp\/guzzle\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u6b65\u9aa4 3\uff1a<strong>\u4e3a api \u8bf7\u6c42\u521b\u5efa\u670d\u52a1\u7c7b<\/strong> <\/h3>\n<p>\u4e3a\u4e86\u4fdd\u6301\u4ee3\u7801\u4e95\u4e95\u6709\u6761\uff0c\u8bf7\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u7c7b\u6765\u5904\u7406 api \u96c6\u6210\u903b\u8f91\u3002<\/p>\n<ol>\n<li> <strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u670d\u52a1\u7c7b<\/strong>:<\/li>\n<\/ol>\n<p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efa\u670d\u52a1\u7c7b\uff1a<\/p>\n<pre>   php artisan make:service weatherservice\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li> <strong>\u5b9e\u73b0\u670d\u52a1\u7c7b<\/strong>:<\/li>\n<\/ol>\n<p>\u5728 app\/services\/weatherservice.php \u4e2d\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u4ece\u5929\u6c14 api \u4e2d\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<pre>   &lt;?php\n\n   namespace appservices;\n\n   use guzzlehttpclient;\n\n   class weatherservice\n   {\n       protected $client;\n\n       public function __construct(client $client)\n       {\n           $this-&gt;client = $client;\n       }\n\n       public function getweather($city)\n       {\n           $url = env('weather_api_url');\n           $apikey = env('weather_api_key');\n\n           $response = $this-&gt;client-&gt;get($url, [\n               'query' =&gt; [\n                   'q' =&gt; $city,\n                   'appid' =&gt; $apikey,\n                   'units' =&gt; 'metric'  \/\/ or 'imperial' for fahrenheit\n               ]\n           ]);\n\n           return json_decode($response-&gt;getbody(), true);\n       }\n   }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u6b65\u9aa44\uff1a<strong>\u5c06\u670d\u52a1\u7ed1\u5b9a\u5230\u670d\u52a1\u63d0\u4f9b\u8005<\/strong> <\/h3>\n<p>\u8981\u4f7f weatherservice \u5728\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ef\u8bbf\u95ee\uff0c\u8bf7\u5c06\u5176\u7ed1\u5b9a\u5230\u670d\u52a1\u63d0\u4f9b\u8005\u4e2d\u3002<\/p>\n<ol>\n<li> <strong>\u521b\u5efa\u65b0\u7684\u670d\u52a1\u63d0\u4f9b\u5546<\/strong>\uff1a <\/li>\n<\/ol>\n<pre>   php artisan make:provider apiserviceprovider\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li> <strong>\u5728 apiserviceprovider.php \u4e2d\u6ce8\u518c\u670d\u52a1<\/strong>:<\/li>\n<\/ol>\n<p>\u5728 app\/providers\/apiserviceprovider.php \u4e2d\uff0c\u6dfb\u52a0\uff1a<\/p>\n<pre>   &lt;?php\n\n   namespace appproviders;\n\n   use illuminatesupportserviceprovider;\n   use guzzlehttpclient;\n   use appservicesweatherservice;\n\n   class apiserviceprovider extends serviceprovider\n   {\n       public function register()\n       {\n           $this-&gt;app-&gt;singleton(weatherservice::class, function () {\n               return new weatherservice(new client());\n           });\n       }\n\n       public function boot()\n       {\n           \/\/\n       }\n   }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li> <strong>\u6ce8\u518c\u670d\u52a1\u63d0\u4f9b\u5546<\/strong>\uff1a<\/li>\n<\/ol>\n<p>\u5728config\/app.php\u4e2d\uff0c\u5c06appprovidersapiserviceprovider::class\u6dfb\u52a0\u5230providers\u6570\u7ec4\u4e2d\u3002<\/p>\n<h3> \u7b2c5\u6b65\uff1a<strong>\u521b\u5efaapi\u4ea4\u4e92\u7684\u63a7\u5236\u5668<\/strong> <\/h3>\n<p>\u8981\u5904\u7406 api \u8bf7\u6c42\u548c\u54cd\u5e94\uff0c\u8bf7\u521b\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\u6765\u4e0e weatherservice \u4ea4\u4e92\u3002<\/p>\n<ol>\n<li> <strong>\u751f\u6210\u63a7\u5236\u5668<\/strong>\uff1a <\/li>\n<\/ol>\n<pre>   php artisan make:controller weathercontroller\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li> <strong>\u4f7f\u7528\u63a7\u5236\u5668\u4e2d\u7684\u670d\u52a1<\/strong>:<\/li>\n<\/ol>\n<p>\u5728 app\/http\/controllers\/weathercontroller.php \u4e2d\uff0c\u6dfb\u52a0\uff1a<\/p>\n<pre>   &lt;?php\n\n   namespace apphttpcontrollers;\n\n   use appservicesweatherservice;\n   use illuminatehttp\request;\n\n   class weathercontroller extends controller\n   {\n       protected $weatherservice;\n\n       public function __construct(weatherservice $weatherservice)\n       {\n           $this-&gt;weatherservice = $weatherservice;\n       }\n\n       public function show($city)\n       {\n           $weatherdata = $this-&gt;weatherservice-&gt;getweather($city);\n\n           return view('weather.show', ['weather' =&gt; $weatherdata]);\n       }\n   }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u7b2c 6 \u6b65\uff1a<strong>\u5b9a\u4e49\u8def\u7ebf<\/strong> <\/h3>\n<p>\u6dfb\u52a0\u8def\u7531\u4ee5\u6839\u636e\u57ce\u5e02\u540d\u79f0\u53d1\u51fa api \u8bf7\u6c42\u3002<\/p>\n<ol>\n<li> <strong>\u66f4\u65b0routes\/web.php<\/strong>\uff1a <\/li>\n<\/ol>\n<pre>   use apphttpcontrollersweathercontroller;\n\n   route::get('\/weather\/{city}', [weathercontroller::class, 'show']);\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u7b2c7\u6b65\uff1a<strong>\u521b\u5efa\u4e00\u4e2a\u89c6\u56fe\u6765\u663e\u793a\u5929\u6c14\u6570\u636e<\/strong> <\/h3>\n<p>\u521b\u5efa\u4e00\u4e2a\u89c6\u56fe\u6765\u663e\u793a\u4ece api \u83b7\u53d6\u7684\u5929\u6c14\u4fe1\u606f\u3002<\/p>\n<ol>\n<li> <strong>\u521b\u5efa\u89c6\u56fe<\/strong>:<\/li>\n<\/ol>\n<p>\u5728 resources\/views\/weather\/show.blade.php \u4e2d\uff0c\u6dfb\u52a0\uff1a<\/p>\n<pre>   &lt;!doctype html&gt;\n   &lt;html lang=\"en\"&gt;\n   &lt;head&gt;\n       &lt;meta charset=\"utf-8\"&gt;\n       &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n       &lt;title&gt;weather information&lt;\/title&gt;\n   &lt;\/head&gt;\n   &lt;body&gt;\n       &lt;h1&gt;weather in {{ $weather['name'] }}&lt;\/h1&gt;\n       &lt;p&gt;temperature: {{ $weather['main']['temp'] }}\u00b0c&lt;\/p&gt;\n       &lt;p&gt;condition: {{ $weather['weather'][0]['description'] }}&lt;\/p&gt;\n       &lt;p&gt;humidity: {{ $weather['main']['humidity'] }}%&lt;\/p&gt;\n   &lt;\/body&gt;\n   &lt;\/html&gt;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u7b2c 8 \u6b65\uff1a<strong>\u6d4b\u8bd5\u96c6\u6210<\/strong> <\/h3>\n<p>\u542f\u52a8 laravel \u5f00\u53d1\u670d\u52a1\u5668\uff1a<\/p>\n<pre>php artisan serve\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8bbf\u95ee http:\/\/localhost:8000\/weather\/{city}\uff0c\u5c06 {city} \u66ff\u6362\u4e3a\u60a8\u8981\u68c0\u67e5\u7684\u57ce\u5e02\u540d\u79f0\uff08\u4f8b\u5982\u4f26\u6566\uff09\u3002<\/p>\n<hr>\n<h3> \u6982\u62ec <\/h3>\n<p>\u60a8\u73b0\u5728\u5df2\u7ecf\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u5c06\u7b2c\u4e09\u65b9 api \u96c6\u6210\u5230 laravel \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff1a<\/p>\n<ol>\n<li>\u5728\u73af\u5883\u6587\u4ef6\u4e2d\u8bbe\u7f6e api \u5bc6\u94a5\u3002<\/li>\n<li>\u5b89\u88c5\u5e76\u914d\u7f6e http \u5ba2\u6237\u7aef\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u6765\u5904\u7406 api \u8bf7\u6c42\u3002<\/li>\n<li>\u5c06\u670d\u52a1\u7ed1\u5b9a\u5230\u670d\u52a1\u63d0\u4f9b\u8005\u4e2d\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\u6765\u4f7f\u7528\u8be5\u670d\u52a1\u3002<\/li>\n<li>\u5b9a\u4e49\u8def\u7ebf\u3002<\/li>\n<li>\u6784\u5efa\u4e00\u4e2a\u89c6\u56fe\u6765\u663e\u793a\u6570\u636e\u3002<\/li>\n<li>\u6d4b\u8bd5\u60a8\u7684\u96c6\u6210\u3002<\/li>\n<\/ol>\n<p>\u6b64\u8bbe\u7f6e\u9075\u5faa laravel \u6700\u4f73\u5b9e\u8df5\uff0c\u4f7f\u60a8\u7684\u4ee3\u7801\u4fdd\u6301\u6a21\u5757\u5316\u548c\u5b89\u5168\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6269\u5c55\u5230\u60a8\u60f3\u8981\u96c6\u6210\u7684\u4efb\u4f55\u7b2c\u4e09\u65b9 api\uff01<\/p>\n<p>\u4e0e\u6211\u8054\u7cfb\uff1a@ linkedin \u5e76\u67e5\u770b\u6211\u7684\u4f5c\u54c1\u96c6\u3002<\/p>\n<p>\u8bf7\u7ed9\u6211\u7684 hub \u9879\u76ee\u4e00\u9897\u661f \u2b50\ufe0f<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728 Laravel \u5e94\u7528\u7a0b\u5e8f\u4e2d\u96c6\u6210\u7b2c\u4e09\u65b9 API \u7684\u5206\u6b65\u6307\u5357\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u9898\uff1a\u3001api\u3001thirdpartyintegration\u3001web \u5f00\u53d1 php laraveltips apisinlaravel \u5c06\u7b2c\u4e09\u65b9 api \u96c6\u6210\u5230 laravel \u4e2d\u53ef\u4ee5\u901a\u8fc7\u5229\u7528\u5916\u90e8\u670d\u52a1\uff08\u4f8b\u5982\u652f\u4ed8\u3001\u6570\u636e\u68c0\u7d22\u7b49\uff09\u6765\u589e\u5f3a\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u6709\u6548\u96c6\u6210\u7b2c\u4e09\u65b9 api \u7684\u5206\u6b65\u6307\u5357\u548c\u793a\u4f8b\u3002 \u5148\u51b3\u6761\u4ef6\uff1a \u4e00\u4e2a\u53ef\u4ee5\u8fd0\u884c\u7684 laravel \u5b89\u88c5\u3002 \u7b2c\u4e09\u65b9 api\uff08\u6211\u4eec\u5c06\u4f7f\u7528\u5929\u6c14 api \u4f5c\u4e3a\u793a\u4f8b\uff09\u3002 \u7b2c 1 \u6b65\uff1a\u8bbe\u7f6e\u60a8\u7684 api \u5bc6\u94a5\u548c\u73af\u5883\u53d8\u91cf \u9996\u5148\uff0c\u6ce8\u518c\u7b2c\u4e09\u65b9api\u5e76\u83b7\u53d6\u60a8\u7684api\u5bc6\u94a5\u3002\u5728 laravel \u7684 .env \u6587\u4ef6\u4e2d\u5b58\u50a8 api \u5bc6\u94a5\u7b49\u654f\u611f\u4fe1\u606f\u3002 \u83b7\u53d6 api \u5bc6\u94a5\uff1a\u6ce8\u518c\u7b2c\u4e09\u65b9 api\uff08\u4f8b\u5982 openweathermap\uff09\u5e76\u68c0\u7d22\u60a8\u7684 api \u5bc6\u94a5\u3002 \u6dfb\u52a0\u5230.env\uff1a weather_api_key=your_api_key_here weather_api_url=https:\/\/api.openweathermap.org\/data\/2.5\/weather \u767b\u5f55\u540e\u590d\u5236 \u6b65\u9aa4 2\uff1a\u5b89\u88c5 guzzle\uff08http \u5ba2\u6237\u7aef\uff09 laravel \u4f7f\u7528 guzzle\uff08\u4e00\u4e2a php http \u5ba2\u6237\u7aef\uff09\u6765\u53d1\u51fa http \u8bf7\u6c42\u3002\u5982\u679c\u60a8\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-28363","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=28363"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28363\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=28363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=28363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=28363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}