{"id":24776,"date":"2024-11-21T10:30:27","date_gmt":"2024-11-21T02:30:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24776\/"},"modified":"2024-11-21T10:30:27","modified_gmt":"2024-11-21T02:30:27","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8hyperf%e6%a1%86%e6%9e%b6%e8%bf%9b%e8%a1%8c%e5%9c%b0%e7%90%86%e4%bd%8d%e7%bd%ae%e6%9c%8d%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24776\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5730\u7406\u4f4d\u7f6e\u670d\u52a1"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169820903491981.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\uff0c\u53ef\u8fde\u63a5\u767e\u5ea6\u5730\u56feAPI<\/p>\n<p>\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\u5728\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u90fd\u5f97\u5230\u5e7f\u6cdb\u5e94\u7528\uff0c\u800cHyperf\u6846\u67b6\u4f5c\u4e3a\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684PHP\u6846\u67b6\uff0c\u4e5f\u53ef\u4ee5\u4e0e\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\u76f8\u7ed3\u5408\uff0c\u4e3a\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u5730\u7406\u4f4d\u7f6e\u76f8\u5173\u7684\u529f\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fde\u63a5\u767e\u5ea6\u5730\u56feAPI\u8fdb\u884c\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u7533\u8bf7\u767e\u5ea6\u5730\u56feAPI\u5bc6\u94a5<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u53bb\u767e\u5ea6\u5730\u56fe\u5f00\u653e\u5e73\u53f0\u6ce8\u518c\u4e00\u4e2a\u5f00\u53d1\u8005\u8d26\u53f7\uff0c\u5e76\u7533\u8bf7\u4e00\u4e2aAPI\u5bc6\u94a5\u3002\u767b\u5f55\u767e\u5ea6\u5730\u56fe\u5f00\u653e\u5e73\u53f0\u540e\uff0c\u5728\u63a7\u5236\u53f0\u83dc\u5355\u4e2d\u627e\u5230\u201c\u6211\u7684\u5e94\u7528\u201d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5e94\u7528\uff0c\u5e76\u83b7\u53d6API\u5bc6\u94a5\u3002<\/p>\n<p>\u4e8c\u3001\u5b89\u88c5Hyperf\u6846\u67b6<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u5728\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u5b89\u88c5Hyperf\u6846\u67b6\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7composer\u547d\u4ee4\u6765\u5b89\u88c5\uff0c\u5177\u4f53\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre>$ composer create-project hyperf\/hyperf-skeleton project-name<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e09\u3001\u6dfb\u52a0\u767e\u5ea6\u5730\u56fePHP SDK<\/p>\n<p>Hyperf\u6846\u67b6\u7684composer.json\u6587\u4ef6\u5df2\u7ecf\u81ea\u52a8\u6dfb\u52a0\u4e86\u767e\u5ea6\u5730\u56fe\u7684PHP SDK\u7684\u4f9d\u8d56\u3002\u4f60\u53ea\u9700\u8981\u8fd0\u884ccomposer\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\u3002<\/p>\n<pre>$ composer install<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u56db\u3001\u521b\u5efa\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\u7c7b<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\u7684\u7c7b\uff0c\u547d\u540d\u4e3aLocationService.php\u3002\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u767e\u5ea6\u5730\u56feAPI\u63d0\u4f9b\u7684\u9006\u5730\u7406\u7f16\u7801\u63a5\u53e3\u6765\u83b7\u53d6\u4e00\u4e2a\u5730\u70b9\u7684\u8be6\u7ec6\u5730\u5740\u3002<\/p>\n<pre>&lt;?php namespace AppService;\n\nuse GuzzleHttpClient;\n\nclass LocationService\n{\n    protected $baseUrl = 'http:\/\/api.map.baidu.com\/reverse_geocoding\/v3\/';\n\n    protected $apiKey;\n\n    public function __construct($apiKey)\n    {\n        $this-&gt;apiKey = $apiKey;\n    }\n\n    public function getAddress($latitude, $longitude)\n    {\n        $client = new Client();\n\n        $response = $client-&amp;gt;get($this-&amp;gt;baseUrl, [\n            'query' =&amp;gt; [\n                'ak' =&amp;gt; $this-&amp;gt;apiKey,\n                'output' =&amp;gt; 'json',\n                'coordtype' =&amp;gt; 'wgs84ll',\n                'location' =&amp;gt; \"{$latitude},{$longitude}\"\n            ]\n        ]);\n\n        $data = json_decode($response-&amp;gt;getBody()-&amp;gt;getContents(), true);\n\n        if (isset($data['result']['formatted_address'])) {\n            return $data['result']['formatted_address'];\n        }\n\n        return null;\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aLocationService\u7c7b\u3002\u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e00\u4e2aAPI\u5bc6\u94a5\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u5b83\u8d4b\u503c\u7ed9\u6210\u5458\u53d8\u91cf$apiKey\u3002getAddress\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u7eac\u5ea6\u548c\u7ecf\u5ea6\u4f5c\u4e3a\u53c2\u6570\uff0c\u901a\u8fc7\u8c03\u7528\u767e\u5ea6\u5730\u56feAPI\u83b7\u53d6\u5730\u70b9\u7684\u8be6\u7ec6\u5730\u5740\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<p>\u4e94\u3001\u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528\u5730\u7406\u4f4d\u7f6e\u670d\u52a1<\/p>\n<p>\u5728Hyperf\u6846\u67b6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528\u6211\u4eec\u521b\u5efa\u7684\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\u7c7b\u6765\u83b7\u53d6\u5730\u70b9\u7684\u8be6\u7ec6\u5730\u5740\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u63a7\u5236\u5668\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php namespace AppController;\n\nuse HyperfDiAnnotationInject;\nuse HyperfHttpServerAnnotationController;\nuse HyperfHttpServerAnnotationGetMapping;\nuse AppServiceLocationService;\n\n\/**\n * @Controller\n *\/\nclass LocationController\n{\n    \/**\n     * @Inject\n     * @var LocationService\n     *\/\n    protected $locationService;\n\n    \/**\n     * @GetMapping(\"\/location\")\n     *\/\n    public function index($latitude, $longitude)\n    {\n        $address = $this-&gt;locationService-&amp;gt;getAddress($latitude, $longitude);\n\n        return [\n            'latitude' =&amp;gt; $latitude,\n            'longitude' =&amp;gt; $longitude,\n            'address' =&amp;gt; $address\n        ];\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5728\u63a7\u5236\u5668\u7684\u4f9d\u8d56\u6ce8\u5165\u6ce8\u89e3\u4e2d\u6ce8\u5165\u4e86LocationService\u7c7b\u7684\u5b9e\u4f8b\u3002\u5728index\u65b9\u6cd5\u4e2d\uff0c\u901a\u8fc7\u8c03\u7528LocationService\u7684getAddress\u65b9\u6cd5\u6765\u83b7\u53d6\u5730\u70b9\u7684\u8be6\u7ec6\u5730\u5740\uff0c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u3002<\/p>\n<p>\u516d\u3001\u914d\u7f6e\u8def\u7531<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728\u8def\u7531\u6587\u4ef6\u4e2d\u914d\u7f6e\u4e00\u4e2a\u8def\u7531\u6765\u8bbf\u95eeLocationController\u4e2d\u7684index\u65b9\u6cd5\u3002\u6253\u5f00config\/routes.php\u6587\u4ef6\uff0c\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php use HyperfHttpServerRouterRouter;\n\nRouter::addRoute(['GET'], '\/location', 'AppControllerLocationController@index');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95eehttp:\/\/localhost:9501\/location?latitude=\u7eac\u5ea6&amp;longitude=\u7ecf\u5ea6\u6765\u83b7\u53d6\u4e00\u4e2a\u5730\u70b9\u7684\u8be6\u7ec6\u5730\u5740\u4e86\u3002<\/p>\n<p>\u7ed3\u8bed<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fde\u63a5\u767e\u5ea6\u5730\u56feAPI\u8fdb\u884c\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\uff0c\u5e76\u63d0\u4f9b\u4e86\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u901a\u8fc7\u9605\u8bfb\u672c\u6587\uff0c\u4f60\u53ef\u4ee5\u5b66\u4f1a\u5982\u4f55\u5728Hyperf\u6846\u67b6\u4e2d\u4f7f\u7528\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\uff0c\u5e76\u6839\u636e\u9700\u6c42\u6765\u6269\u5c55\u8fd9\u4e2a\u670d\u52a1\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\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>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\uff0c\u53ef\u8fde\u63a5\u767e\u5ea6\u5730\u56feAPI \u5730\u7406\u4f4d\u7f6e\u670d\u52a1\u5728\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u90fd\u5f97\u5230\u5e7f\u6cdb\u5e94\u7528\uff0c\u800cHyperf\u6846\u67b6\u4f5c\u4e3a\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684PHP\u6846\u67b6\uff0c\u4e5f\u53ef\u4ee5\u4e0e\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\u76f8\u7ed3\u5408\uff0c\u4e3a\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u5730\u7406\u4f4d\u7f6e\u76f8\u5173\u7684\u529f\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fde\u63a5\u767e\u5ea6\u5730\u56feAPI\u8fdb\u884c\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u7533\u8bf7\u767e\u5ea6\u5730\u56feAPI\u5bc6\u94a5 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u53bb\u767e\u5ea6\u5730\u56fe\u5f00\u653e\u5e73\u53f0\u6ce8\u518c\u4e00\u4e2a\u5f00\u53d1\u8005\u8d26\u53f7\uff0c\u5e76\u7533\u8bf7\u4e00\u4e2aAPI\u5bc6\u94a5\u3002\u767b\u5f55\u767e\u5ea6\u5730\u56fe\u5f00\u653e\u5e73\u53f0\u540e\uff0c\u5728\u63a7\u5236\u53f0\u83dc\u5355\u4e2d\u627e\u5230\u201c\u6211\u7684\u5e94\u7528\u201d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5e94\u7528\uff0c\u5e76\u83b7\u53d6API\u5bc6\u94a5\u3002 \u4e8c\u3001\u5b89\u88c5Hyperf\u6846\u67b6 \u63a5\u4e0b\u6765\uff0c\u5728\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u5b89\u88c5Hyperf\u6846\u67b6\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7composer\u547d\u4ee4\u6765\u5b89\u88c5\uff0c\u5177\u4f53\u547d\u4ee4\u5982\u4e0b\uff1a $ composer create-project hyperf\/hyperf-skeleton project-name \u767b\u5f55\u540e\u590d\u5236 \u4e09\u3001\u6dfb\u52a0\u767e\u5ea6\u5730\u56fePHP SDK Hyperf\u6846\u67b6\u7684composer.json\u6587\u4ef6\u5df2\u7ecf\u81ea\u52a8\u6dfb\u52a0\u4e86\u767e\u5ea6\u5730\u56fe\u7684PHP SDK\u7684\u4f9d\u8d56\u3002\u4f60\u53ea\u9700\u8981\u8fd0\u884ccomposer\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\u3002 $ composer install \u767b\u5f55\u540e\u590d\u5236 \u56db\u3001\u521b\u5efa\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\u7c7b \u521b\u5efa\u4e00\u4e2a\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\u7684\u7c7b\uff0c\u547d\u540d\u4e3aLocationService.php\u3002\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u767e\u5ea6\u5730\u56feAPI\u63d0\u4f9b\u7684\u9006\u5730\u7406\u7f16\u7801\u63a5\u53e3\u6765\u83b7\u53d6\u4e00\u4e2a\u5730\u70b9\u7684\u8be6\u7ec6\u5730\u5740\u3002 &lt;?php namespace AppService; use GuzzleHttpClient; class LocationService { protected $baseUrl = &#8216;http:\/\/api.map.baidu.com\/reverse_geocoding\/v3\/&#8217;; protected $apiKey; public function __construct($apiKey) { $this-&gt;apiKey = $apiKey; } public function getAddress($latitude, $longitude) { $client = new Client(); $response = [&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-24776","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24776","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=24776"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24776\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}