{"id":81,"date":"2019-06-18T15:39:56","date_gmt":"2019-06-18T07:39:56","guid":{"rendered":"http:\/\/blog.mrfai.cn\/?p=81"},"modified":"2020-06-10T16:58:26","modified_gmt":"2020-06-10T08:58:26","slug":"php%e8%b0%83%e7%94%a8%e7%99%be%e5%ba%a6%e5%9c%b0%e5%9b%be%e5%85%ac%e4%ba%a4%e8%b7%af%e7%ba%bf%e8%a7%84%e5%88%92api%e7%9a%84%e6%b5%81%e7%a8%8b%e5%92%8c%e8%b8%a9%e5%9d%91","status":"publish","type":"post","link":"https:\/\/blog.mrfai.cn\/index.php\/2019\/06\/18\/81\/","title":{"rendered":"PHP\u8c03\u7528\u767e\u5ea6\u5730\u56fe\u516c\u4ea4\u8def\u7ebf\u89c4\u5212API\u7684\u6d41\u7a0b\u548c\u8e29\u5751"},"content":{"rendered":"\n<p>\u6700\u8fd1\u505a\u4e00\u4e2a\u9879\u76ee\u9700\u8981\u7528\u5230\u767e\u5ea6\u5730\u56fe\u516c\u4ea4\u7ebf\u8def\u89c4\u5212\uff0c\u4e8e\u662f\u5206\u4eab\u4e00\u4e0b\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"\u4e00-\u7533\u8bf7api\u7684\u6743\u9650\">\u4e00\u3001\u7533\u8bf7API\u7684\u6743\u9650<\/h1>\n\n\n\n<p>\u7533\u8bf7\u6210\u529f\u540e\u5728\u63a7\u5236\u53f0\u4f1a\u5f97\u5230\u767e\u5ea6\u5730\u56feAPI\u7684ak\u7801\u3002\u7531\u4e8e\u6211\u9009\u62e9\u7684\u8bf7\u6c42\u6821\u9a8c\u65b9\u5f0f\u662fsn\u6821\u9a8c\u65b9\u5f0f\uff0c\u6240\u4ee5\u8fd8\u6709\u4e00\u4e2ask\u7801\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"\u4e8c-\u4f7f\u7528php\u53d1\u8d77\u8bf7\u6c42\">\u4e8c\u3001\u4f7f\u7528PHP\u53d1\u8d77\u8bf7\u6c42<\/h1>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u672c\u6b21\u793a\u8303\u7684\u662f\u8bf7\u6c42\u516c\u4ea4\u8def\u7ebf\u89c4\u5212api\uff08GET\u8bf7\u6c42\uff09&nbsp;<\/p><p><a rel=\"noreferrer noopener\" href=\"http:\/\/api.map.baidu.com\/direction\/v2\/transit?origin=40.056878,116.30815&amp;destination=31.222965,121.505821&amp;ak=%E6%82%A8%E7%9A%84AK\" target=\"_blank\">http:\/\/api.map.baidu.com\/direction\/v2\/transit?origin=40.056878,116.30815&amp;destination=31.222965,121.505821&amp;ak=\u60a8\u7684AK<\/a><\/p><p>\u53c2\u8003\u6587\u6863\uff1a<\/p><p><a rel=\"noreferrer noopener\" href=\"http:\/\/lbsyun.baidu.com\/index.php?title=webapi\/direction-api-v2\" target=\"_blank\">http:\/\/lbsyun.baidu.com\/index.php?title=webapi\/direction-api-v2<\/a><\/p><\/blockquote>\n\n\n\n<p>PHP\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/ \u83b7\u53d6\u516c\u4ea4\u8def\u7ebf\n    public function gettransit() {\n        \/\/API\u63a7\u5236\u53f0\u7533\u8bf7\u5f97\u5230\u7684ak\uff08\u6b64\u5904ak\u503c\u4ec5\u4f9b\u9a8c\u8bc1\u53c2\u8003\u4f7f\u7528\uff09\n        $ak = 'yourak';\n\n        \/\/\u5e94\u7528\u7c7b\u578b\u4e3afor server, \u8bf7\u6c42\u6821\u9a8c\u65b9\u5f0f\u4e3asn\u6821\u9a8c\u65b9\u5f0f\u65f6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u751f\u6210sk\uff0c\u53ef\u4ee5\u5728\u5e94\u7528\u914d\u7f6e-\u8bbe\u7f6e\u4e2d\u9009\u62e9Security Key\u663e\u793a\u8fdb\u884c\u67e5\u770b\uff08\u6b64\u5904sk\u503c\u4ec5\u4f9b\u9a8c\u8bc1\u53c2\u8003\u4f7f\u7528\uff09\n        $sk = 'yoursk';\n\n        \/\/get\u8bf7\u6c42uri\u524d\u7f00\n        $uri = '\/direction\/v2\/transit';\n\n\n\n        \/\/\u8d77\u70b9\u5750\u6807\uff08\u5fc5\u586b\uff09\n        $origin = '22.693621,113.817503';\n\n        \/\/\u7ec8\u70b9\u5750\u6807\uff08\u5fc5\u586b\uff09\n        $destination = '22.579575,114.143696';\n\n        \/\/\u5df2\u77e5\u8d77\u70b9\u7684\u540d\u5b57\uff08\u9009\u586b\uff09\n        $origin_uid = '\u6865\u5934-\u5730\u94c1\u7ad9';\n        \n        \/\/\u5df2\u77e5\u7ec8\u70b9\u7684\u540d\u5b57\uff08\u9009\u586b\uff09\n        $destination_uid = '\u592a\u5b89-\u5730\u94c1\u7ad9';\n        \n        \/\/\u51fa\u53d1\u65f6\u95f4\u533a\u95f4\uff08\u9009\u586b\uff09\n        $departure_time = '10:00-12:00';\n        \n        \/\/\u5e02\u5185\u516c\u4ea4\u6362\u4e58\u7b56\u7565\uff08\u5730\u94c1\u4f18\u5148\uff0c\u9009\u586b\uff09\n        $tactics_incity = 5;\n\n        \/\/\u65f6\u95f4\u6233\uff08\u63a5\u53e3\u9009\u62e9sn\u68c0\u9a8c\u65b9\u5f0f\u65f6\u5fc5\u987b\uff09\n        $timestamp = time();\n\n        \/\/\u6784\u9020\u8bf7\u6c42\u4e32\u6570\u7ec4\uff08\u9664\u4e86$sk\u3001$uri\u53c2\u6570\u5916\u5168\u90e8\u8981\u586b\u5165\uff09\n        $querystring_arrays = array (\n            'origin' => $origin,\n            'destination' => $destination,\n            'origin_uid' => $origin_uid,\n            'destination_uid' => $destination_uid,\n            'departure_time' => $departure_time,\n            'tactics_incity' => $tactics_incity,\n            'timestamp' => $timestamp,\n            'ak' => $ak\n        );\n\n        \/\/\u8c03\u7528sn\u8ba1\u7b97\u51fd\u6570\uff0c\u9ed8\u8ba4get\u8bf7\u6c42\n        $sn = $this->caculateAKSN($ak, $sk, $uri, $querystring_arrays);\n\n        \/\/\u4ee5transit\u670d\u52a1\u4e3a\u4f8b\uff0c\u5730\u7406\u7f16\u7801\u7684\u8bf7\u6c42url\uff0c\u53c2\u6570\u5f85\u586b\n        $url = \"http:\/\/api.map.baidu.com\/direction\/v2\/transit?origin=%s&amp;destination=%s&amp;origin_uid=%s&amp;destination_uid=%s&amp;departure_time=%s&amp;tactics_incity=%s&amp;ak=%s&amp;sn=%s&amp;timestamp=%s\";\n\n        \/\/\u8bf7\u6c42\u53c2\u6570\u4e2d\u6709\u4e2d\u6587\u3001\u7279\u6b8a\u5b57\u7b26\u7b49\u9700\u8981\u8fdb\u884curlencode\uff0c\u786e\u4fdd\u8bf7\u6c42\u4e32\u4e0esn\u5bf9\u5e94\n        $target = sprintf($url,urlencode($origin),urlencode($destination),urlencode($origin_uid),urlencode($destination_uid),urlencode($departure_time), $tactics_incity,$ak, $sn, $timestamp);\n\n        \/\/\u83b7\u53d6\u8bf7\u6c42\u7ed3\u679c\n        $result = $this->httpto(\"$target\",'','GET');\n        $result = json_decode($result);\n        \/\/\u8fd4\u56de\u8bf7\u6c42\u7ed3\u679cjson\n        return response()->json($result);\n    }\n\n\n\n\n    \/\/ \u8ba1\u7b97sn\n    private function caculateAKSN($ak, $sk, $url, $querystring_arrays, $method = 'GET')\n    {\n        if ($method === 'POST'){\n            ksort($querystring_arrays);\n        }\n        $querystring = http_build_query($querystring_arrays);\n        return md5(urlencode($url.'?'.$querystring.$sk));\n    }\n\n\n\n\n    \/\/ \u6784\u9020http\u8bf7\u6c42\n    function httpto($url, $params, $method = 'GET', $header = array(), $multi = false) {\n        $opts = array(\n            CURLOPT_TIMEOUT        => 30,\n            CURLOPT_RETURNTRANSFER => 1,\n            CURLOPT_SSL_VERIFYPEER => false,\n            CURLOPT_SSL_VERIFYHOST => false,\n            CURLOPT_HTTPHEADER     => $header\n        );\n        \/* \u6839\u636e\u8bf7\u6c42\u7c7b\u578b\u8bbe\u7f6e\u7279\u5b9a\u53c2\u6570 *\/\n        switch(strtoupper($method)){\n            case 'GET':\n                $opts&#91;CURLOPT_URL] = $url;\n                break;\n            case 'POST':\n                \/\/\u5224\u65ad\u662f\u5426\u4f20\u8f93\u6587\u4ef6\n                $params = $multi ? $params : http_build_query($params);\n                $opts&#91;CURLOPT_URL] = $url;\n                $opts&#91;CURLOPT_POST] = 1;\n                $opts&#91;CURLOPT_POSTFIELDS] = $params;\n                break;\n            default:\n                throw new Exception('\u4e0d\u652f\u6301\u7684\u8bf7\u6c42\u65b9\u5f0f\uff01');\n        }\n        \/* \u521d\u59cb\u5316\u5e76\u6267\u884ccurl\u8bf7\u6c42 *\/\n        $ch = curl_init();\n        curl_setopt_array($ch, $opts);\n        $data  = curl_exec($ch);\n        $error = curl_error($ch);\n        curl_close($ch);\n        if($error) throw new Exception('\u8bf7\u6c42\u53d1\u751f\u9519\u8bef\uff1a' . $error);\n        return $data;\n    }<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"\u4e09-\u8e29\u5751\u63d0\u793a\">\u4e09\u3001\u8e29\u5751\u63d0\u793a<\/h1>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><span class=\"has-inline-color has-vivid-red-color\">\u95ee\u9898\uff1a\u66fe\u5728\u5b8c\u6210\u63a5\u53e3\u540e\uff0c\u6d4b\u8bd5\u63a5\u53e3\u9047\u5230 {&#8220;status&#8221;:2,&#8221;message&#8221;:&#8221;\u53c2\u6570\u9519\u8bef&#8221;} \u7684\u62a5\u9519\uff0c\u672c\u4eba\u68c0\u67e5\u5750\u6807\u4e5f\u65e0\u5f02\u5e38\uff0c\u7b26\u5408\u9ed8\u8ba4\u7684\u62a5\u8bfbbd09II\u5750\u6807\u6807\u51c6\uff0c\u95ee\u9898\u51fa\u73b0\u5728\u54ea\u91cc\u5462\uff1f<\/span><\/p><p>\u89e3\u51b3\u65b9\u5f0f\uff1a\u7ecf\u8fc7\u7ec6\u5fc3\u7422\u78e8\u540e\uff0c\u53d1\u73b0\u8bf7\u6c42\u53c2\u6570\u7684origin\u3001destination\u662f\u4f7f\u7528<span class=\"has-inline-color has-vivid-red-color\">(\u7eac\u5ea6,\u7ecf\u5ea6)<\/span>\u7684\u65b9\u5f0f\uff0c\u5e76\u4e0d\u662f\u6211\u4eec\u65e5\u5e38\u8ba4\u77e5\u7684(\u7ecf\u5ea6,\u7eac\u5ea6)\u7684\u65b9\u5f0f\uff0c\u6240\u4ee5\u8bf4\u8fd9\u662f\u4e00\u4e2a\u5751\uff0c\u7a0d\u4e0d\u6ce8\u610f\u5c31\u51fa\u9519\u3002<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u505a\u4e00\u4e2a\u9879\u76ee\u9700\u8981\u7528\u5230\u767e\u5ea6\u5730\u56fe\u516c\u4ea4\u7ebf\u8def\u89c4\u5212\uff0c\u4e8e\u662f\u5206&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/blog.mrfai.cn\/index.php\/2019\/06\/18\/81\/\">Continue reading<span class=\"screen-reader-text\">PHP\u8c03\u7528\u767e\u5ea6\u5730\u56fe\u516c\u4ea4\u8def\u7ebf\u89c4\u5212API\u7684\u6d41\u7a0b\u548c\u8e29\u5751<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[5],"class_list":["post-81","post","type-post","status-publish","format-standard","hentry","category-php","tag-php","entry"],"_links":{"self":[{"href":"https:\/\/blog.mrfai.cn\/index.php\/wp-json\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mrfai.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mrfai.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mrfai.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mrfai.cn\/index.php\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":14,"href":"https:\/\/blog.mrfai.cn\/index.php\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/blog.mrfai.cn\/index.php\/wp-json\/wp\/v2\/posts\/81\/revisions\/113"}],"wp:attachment":[{"href":"https:\/\/blog.mrfai.cn\/index.php\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mrfai.cn\/index.php\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mrfai.cn\/index.php\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}