心语Blog - 三级联动
type: channel
link: https://xinyu19.com/tag/三级联动/
items: PHP获取京东三级联动数据

PHP获取京东三级联动数据
type: item
date: 2019-06-09T13:00:00+08:00
description: 网上的三级联动数据大多只有省市县，且数据可能并不完整，由此可以借用京东的三级联动数据，除了可以获得省市县的数据外还可以获取主干街道的数据，和京东下单时所填写的配送地址一样比较全面，适合电商网站使用。数据源http://fts.jd.com/area/get?fid=' . $fid . '&callback=getAreaList_callbackF&sceneval=2通过以上URL，替换中间的$fid可以获取到不同省、直辖市的下主要区域。下面是全国主要省、直辖市的id $baseArea = [ '1' => '北京', '2' => '上海', '3' => '天津', '4' => '重庆', '5' => '河北', '6' => '山西', '7' => '河南', '8' => '辽宁', '9' => '吉林', '10' => '黑龙江', '11' => '内蒙古', '12' => '江苏', '13' => '山东', '14' => '安徽', '15' => '浙江', '16' => '福建', '17' => '湖北', '18' => '湖南', '19' => '广东', '20' => '广西', '21' => '江西', '22' => '四川', '23' => '海南', '24' => '贵州', '25' => '云南', '26' => '西藏', '27' => '陕西', '28' => '甘肃', '29' => '青海', '30' => '宁夏', '31' => '新疆', '32' => '台湾', '84' => '钓鱼岛', '52993' => '港澳' ];例如这样：代码<?php /** * @author 心语难诉 * @version 1.0 * @url https://xinyu19.com * @filename JDArea.php * @des 京东 省、市、县、街道数据获取 */ class JDArea{ /** * [$baseArea 全国省、直辖市ID] */ public static $baseArea = [ '1' => '北京', '2' => '上海', '3' => '天津', '4' => '重庆', '5' => '河北', '6' => '山西', '7' => '河南', '8' => '辽宁', '9' => '吉林', '10' => '黑龙江', '11' => '内蒙古', '12' => '江苏', '13' => '山东', '14' => '安徽', '15' => '浙江', '16' => '福建', '17' => '湖北', '18' => '湖南', '19' => '广东', '20' => '广西', '21' => '江西', '22' => '四川', '23' => '海南', '24' => '贵州', '25' => '云南', '26' => '西藏', '27' => '陕西', '28' => '甘肃', '29' => '青海', '30' => '宁夏', '31' => '新疆', '32' => '台湾', '84' => '钓鱼岛', '52993' => '港澳' ]; /** * [getAll 循环获取所有数据] */ public static function getAll(){ $areaArr = []; foreach (self::$baseArea as $key => $value) { $areaArr[$key]['id'] = $key; $areaArr[$key]['name'] = $value; $areaArr[$key]['area'] = self::requestApi($key); foreach ($areaArr[$key]['area'] as $k => $v) { echo "数据获取成功：{$areaArr[$key]['name']}----{$v['name']}\r\n"; $province = $areaArr[$key]['name']; $city = $v['name']; $areaArr[$key]['area'][$k]['county'] = self::requestApi($v['id']); foreach ($areaArr[$key]['area'][$k]['county'] as $item) { echo "数据获取成功：{$province}----{$city}----{$item['name']}\r\n"; } } } return $areaArr…