开发接口
目录 宝船网数据 API 开发接口... I 说明... II 1 数据 API... 1 1.1 获取加密种子... 1 1.1.1 接口功能介绍... 1 1.1.2 接口参数说明... 1 1.1.3 接口 URL... 1 1.1.4 返回结果说明... 1 1.2 船舶查询服务... 2 1.2.1 接口功能介绍... 2 1.2.2 接口参数说明... 2 1.2.3 接口 URL... 2 1.2.4 返回结果说明... 2 1.3 船舶基本信息查询服务... 3 1.3.1 接口功能介绍... 3 1.3.2 接口参数说明... 3 1.3.3 接口 URL... 3 1.3.4 返回结果说明... 3 1.4 船舶最新船位查询服务... 4 1.4.1 接口功能介绍... 4 1.4.2 接口参数说明... 4 1.4.3 接口 URL... 5 1.4.4 返回结果说明... 5 1.5 船舶历史轨迹查询服务... 6 1.5.1 接口功能介绍... 6 1.5.2 接口参数说明... 6 1.5.3 接口 URL... 6 1.5.4 返回结果说明... 6 1.6 获取指定区域船舶服务... 7 1.6.1 接口功能介绍... 7 1.6.2 接口参数说明... 7 1.6.3 接口 URL... 7 1.6.4 返回结果说明... 8 1.7 台风服务... 9 1.7.1 接口功能介绍... 9 1.7.2 接口参数说明... 9 1.7.3 接口 URL... 9 1.7.4 返回结果说明... 9 1.8 台风详情服务... 10 1.8.1 接口功能介绍... 10 1.8.2 接口参数说明... 10 1.8.3 接口 URL... 10 1.8.4 返回结果说明... 10 2 调用示例... 15
2.1 java 版... 15 2.2 php 版... 17 2.3 c# 版... 18 2.4 python 版... 21 2.5 JS 版... 22
修订记录 修订日期 版本号 修订描述 修订人 批准人 2015-05-04 V1.0 数据 API 接口定义 唐洁 2015-10-30 V1.1 增加台风信息接口 唐洁 2015-11-05 V1.2 修改船舶查询接口 (1.2), 对于返回值超过 100 条的, 返回的状态码不变, 但按排序优先级返回前 100 条数据 修改历史轨迹查询接口 (1.5), 放开限制, 可查任意时间段一个月区间的轨迹. 修改区域船舶接口 (1.6), 接口增加一个可选参数船位时效性 shiptimeliness( 单位小时 ), 如不传, 默认船位点时间在 4 小时之类的都返回. 唐洁 I
说明 API 是 Application Program Interface 的缩写, 即应用程序接口 宝船网数据 API 由 Http 接口组成的 使用这套数据 API 接口, 宝船网会给每个 API 客户分配一个唯一 key, 调用数据 api 时先获取加密种子, 然后使用 md5 算法加密该 ( 加密种子 + key), 调用每个数据 api 都需要当前加密后的报文 (32 位 md5 算法加密的字符串 ) 当作请求参数传递, 用户可以快捷的获取到船舶数据 该文档主要面向项目经理 开发技术人员, 便于了解 API 接口规范和调用 II
1 数据 API 编码方式 返回值说明 调用方式 UTF-8 返回值使用 JSON 方式 建议使用 HTTP POST 方式 1.1 获取加密种子 1.1.1 接口功能介绍 此方法返回年月日字符串, 如 20150324 客户在调用 api 接口时, 用此字符串和分发的 key 组合并进行 md5 加密生成新字符串来传递给接口, 如分发的 key 为 :e10adc3949ba59abbe56e057f20f883e, 则用户在使用接口时, 需要先调用该接口得到 20150324, 然后 md5(20150324+e10adc3949ba59abbe56e057f20f883e) 生成的 key, 调用数据 API 每个接口都传入该 key 1.1.2 接口参数说明 参数名 无 称 参数格式参数含义备注 1.1.3 接口 URL URL 请求地址 :http://api.myships.com/dataapiserver/getcomkey 1.1.4 返回结果说明 " seedstr":"20150324","status":0 返回参数 status seedstr 返回状态 加密种子字符串 参数说明 1
返回状态码说明 : 返回参数参数说明 -1 后台查询数据失败, 请联系管理员 0 成功 1.2 船舶查询服务 1.2.1 接口功能介绍 根据 MMSI 船名等模糊查船, 目的是获取船舶唯一 ID, 对于返回值超过 100 条的, 按排序优先级返回前 100 条数据 1.2.2 接口参数说明 参数名称 参数格式 参数含义 备注 key key=5cdd7336161c4ce2a27a21d770b8aa 53 Api 用户 32 位字符串 key( 使用 md5 加密后的 必填参数 字符 ) term term = 福远渔 船名或 MMSI 必填参 数 1.2.3 接口 URL URL 请求地址 : http://api.myships.com/dataapiserver/getshipid?term= 福远渔 &key=5cdd7336161c4ce2a27a21d770b8aa53 1.2.4 返回结果说明 "result":["callsign":"413321","shipid":"412704230","shipname":"fu YUAN YU 38","imo":" 9132961"],"status":0 返回参数 参数说明 status 返回状态 result 返回信息 (json 字符串数组 ) shipid 船舶 id shipname callsign 船舶名称 船舶呼号 2
imo Imo 号 返回状态码说明 : 返回参数参数说明 -1 后台查询数据失败, 请联系管理员 0 成功 1 Key 不存在, 请重新获取 key 再调用, 请与管理员联系 2 API 接口权限过期, 请与管理员联系 3 用户 key 已被禁用, 请与客户管理员联系 4 当前 api 用户已经没有权限, 请与管理员联系 5 请求参数 term 为空 6 请求参数 key 为空 7 没有查询到任何数据 8 查询结果集数据集合大于 100 条, 按排序优先级返回前 100 条数据 1.3 船舶基本信息查询服务 1.3.1 接口功能介绍 根据船舶 ID, 获取最新的船舶基本静态信息 ;( 单一 / 批量, 批量最多不超过 20 条 ) 1.3.2 接口参数说明 参数名称参数格式参数含义备注 key key=5cdd7336161c4ce2a27a21d770b8aa53 Api 用户 32 位字符串 key( 使用 md5 加密后的字符 ) shipid shipid =420368290 船舶 ID( 批量用逗号分隔, 表示 多条船舶 ID) 必填参数 必填参数 1.3.3 接口 URL URL 请求地址 : http://api.myships.com/dataapiserver/getshipbasicinformation?shipid=420368290&key=5cdd7336161c4ce2a27a21d 770b8aa53 1.3.4 返回结果说明 "result":[ "callsign":"413321","mmsi":"412704230","shipname":"fu YUAN YU 38","imo":" 9132961","shiptype":" 渔船 3
-Y","length":"136","breadth":"21","eta":"2015-03-26 15:00","dest_port":"QING DAO","draught":"6.0"],"status":0 返回参数 参数说明 status 返回状态 result 返回信息 (json 字符串数组 ) mmsi mmsi shipname callsign imo shiptype length breadth eta dest_port draught 船舶名称船舶呼号 Imo 号船舶类型船长船宽预到时间目的地吃水 返回状态码说明 : 返回参数参数说明 -1 后台查询数据失败, 请联系管理员 0 成功 1 Key 不存在, 请重新获取 key 再调用, 请与管理员联系 2 API 接口权限过期, 请与管理员联系 3 用户 key 已被禁用, 请与客户管理员联系 4 当前 api 用户已经没有权限, 请与管理员联系 5 请求参数 shipid 为空 6 请求参数 key 为空 7 没有查询到任何数据 8 批量查询结果不能超过 20 条 1.4 船舶最新船位查询服务 1.4.1 接口功能介绍 根据船舶 ID, 获取最新船位 ;( 单一 / 批量, 批量最多不超过 20 条 ) 1.4.2 接口参数说明 参数名称参数格式参数含义备注 key key=5cdd7336161c4ce2a27a21d770b8aa53 Api 用户 32 位字符串 key( 使用 md5 加密后的字符 ) 必填参数 4
shipid shipid =420368290 船舶 ID( 批量用逗号分隔, 表示 多条船舶 ID) 必填参数 1.4.3 接口 URL URL 请求地址 : http://api.myships.com/dataapiserver/getshiplatest?shipid=420368290&key=5cdd7336161c4ce2a27a21d770b8aa53 1.4.4 返回结果说明 "result":["postime":"1426572584","longitude":"2944204","latitude":"31424344","course":"71.5","heading":"71", "speed":"8.8","navstatus":" 在航 ( 主机推动 )","mmsi":" 413378420"],"status":0 返回参数 参数说明 status 返回状态 result 返回信息 (json 字符串数组 ) postime 位置时间 (UTC 时间 ) mmsi mmsi longitude 经度 ( 经度 :1/10000 分 ) latitude 纬度 ( 纬度 :1/10000 分 ) course 航向 ( 单位度 ) heading 航首向 ( 单位度 ) speed 船速 ( 单位节 ) navstatus 船行状态 返回状态码说明 : 返回参数参数说明 -1 后台查询数据失败, 请联系管理员 0 成功 1 Key 不存在, 请重新获取 key 再调用, 请与管理员联系 2 API 接口权限过期, 请与管理员联系 3 用户 key 已被禁用, 请与客户管理员联系 4 当前 api 用户已经没有权限, 请与管理员联系 5 请求参数 shipid 为空 6 请求参数 key 为空 7 没有查询到任何数据 8 批量查询结果不能超过 20 条 5
1.5 船舶历史轨迹查询服务 1.5.1 接口功能介绍 根据船舶 ID 查单船历史轨迹, 最早可查时间和轨迹间隔作限制 可查任意时间段的轨迹数据, 查询开始结束日 期区间最多 30 天 1.5.2 接口参数说明 参数名称 参数格式 参数含义 备注 key key=5cdd7336161c4ce2a27a21d770b8aa53 Api 用户 32 位字符串 key( 使用 必填参数 md5 加密后的字符 ) shipid shipid =420368290 船舶 ID 必填参数 starttime starttime=2014-11-02 15:21:43(UTC 时间 ) 开始时间 ( 年 - 月 - 日时 : 分 : 秒 ) 必填参数 endtime endtime=2014-11-09 15:21:43(UTC 时间 ) 结束时间 ( 年 - 月 - 日时 : 分 : 秒 ) 必填参数 1.5.3 接口 URL URL 请求地址 : http://api.myships.com/dataapiserver/getshiphistortrack?shipid=420368290&key=5cdd7336161c4ce2a27a21d770b8 aa53&starttime=2014-11-02 15:21:43&endTime=2014-11-09 15:21:43 1.5.4 返回结果说明 "result":["postime":"1426572584","longitude":"2944204","latitude":"31424344","course":"71.5","heading":"71", "speed":"8.8","mmsi":" 413378420"],"status":0 返回参数 参数说明 status 返回状态 result 返回信息 (json 字符串数组 ) postime 位置时间 (UTC 时间 ) mmsi mmsi longitude 经度 ( 经度 :1/10000 分 ) latitude 纬度 ( 纬度 :1/10000 分 ) course 航向 ( 单位度 ) heading 航首向 ( 单位度 ) speed 船速 ( 单位节 ) 6
返回状态码说明 : 返回参数参数说明 -1 后台查询数据失败, 请联系管理员 0 成功 1 Key 不存在, 请重新获取 key 再调用, 请与管理员联系 2 API 接口权限过期, 请与管理员联系 3 用户 key 已被禁用, 请与客户管理员联系 4 当前 api 用户已经没有权限, 请与管理员联系 5 请求参数 shipid 为空 6 请求参数 key 为空 7 请求参数 starttime 为空或 starttime 不符合规定格式 8 请求参数 endtime 为空或 endtime 不符合规定格式 9 查询间起始间隔不能超过 30 天 10 没有查询到任何数据 1.6 获取指定区域船舶服务 1.6.1 接口功能介绍 根据指定区域内船舶数据接口服务 1.6.2 接口参数说明 参数名称 参数格式 参数含义 备注 key key=5cdd7336161c4ce2a27a21d770b8aa53 Api 用户 32 位字符串 key( 使用 md5 加密后的字符 ) xmin xmin =116234293( 度 *60 万 ) 经度最小值 必填参数 ymin ymin =26234293( 度 *60 万 ) 纬度最小值 必填参数 xmax xmax =117234293( 度 *60 万 ) 经度最大值 必填参数 ymax ymax =27234293( 度 *60 万 ) 纬度最大值 必填参数 shiptimeliness shiptimeliness=12( 单位为小时 ) 多少小时内的船位数据, 大于 0 且小于等于 24, 可以传小数 选填参数, 如不传, 接口后台默认为 4 小时内 1.6.3 接口 URL URL 请求地址 : 7
http://api.myships.com/dataapiserver/getshipbyrect?key=5cdd7336161c4ce2a27a21d770b8aa53&xmin=-55559784 &ymin=17280428&xmax=-54423523&ymax=17590854 1.6.4 返回结果说明 "total":1, "status":0,"result": ["breadth": 7, "course": 290, "shipname": "DIAMOND CUTTER","postime": "1426572584", "speed": 9, "length": 30, "heading": 0, "longitude": "-55349721", "mmsi": 367134300,"latitude": "17414995"] 返回参数 参数说明 status 返回状态 result 返回信息 (json 字符串数组 ) total 返回数据总条数 mmsi Mmsi longitude 经度 ( 经度 :1/10000 分 ) latitude 纬度 ( 经度 :1/10000 分 ) course 船向 ( 单位度 ) heading 船艏向 ( 单位度 ) speed 船速 ( 单位度 ) postime 位置时间 (utc 时间 ) shipname 船名 length 船长 ( 单位米 ) breadth 船宽 ( 单位米 ) 返回状态码说明 : 返回参数参数说明 -1 后台查询数据失败, 请联系管理员 0 成功 1 Key 不存在, 请重新获取 key 再调用, 请与管理员联系 2 API 接口权限过期, 请与管理员联系 3 用户 key 已被禁用, 请与客户管理员联系 4 当前 api 用户已经没有权限, 请与管理员联系 5 请求参数 key 为空 6 请求参数 xmin 为空 7 请求参数 ymin 为空 8 请求参数 xmax 为空 9 请求参数 ymax 为空 10 矩形区域的经度差应小于 0.5 度 11 矩形区域的纬度差应小于 0.5 度 12 查询结果集数据集合大于 2000 条 13 请求参数 xmin 不合法 14 请求参数 ymin 不合法 8
15 请求参数 xmax 不合法 16 请求参数 ymax 不合法 17 请求参数 _shiptimeliness 不合法 18 请求参数 _shiptimeliness 应在大于 0 并小于等于 24 19 没有查询到任何数据 1.7 台风服务 1.7.1 接口功能介绍 获取最近一周内台风列表接口服务 1.7.2 接口参数说明 参数名称 参数格式 参数含义 备注 key key=5cdd7336161c4ce2a27a21d770b8aa53 Api 用户 32 位字符串 key( 使用 必填参数 md5 加密后的字符 ) daysnumber daysnumber=2 获取几天内的台风信息列表 选填参数, 如不传后台默认为 2 天内 1.7.3 接口 URL URL 请求地址 : http://api.myships.com/dataapiserver/getnewesttyphoons?key=5cdd7336161c4ce2a27a21d770b8aa53&daysnum ber=2 1.7.4 返回结果说明 "result":["id":265,"stormname":"chapala","receivetime":"2015-10-30 13:46:41","id":264,"stormName":"FOUR","receiveTime":"2015-10-29 19:46:19"],"status":0 返回参数 参数说明 result 返回信息 (json 字符串数组 ) status 状态码 id 台风 id stormname 台风名称 receivetime 台风发布日期 (UTC- 时间 ) 9
返回状态码说明 : 返回参数参数说明 -1 后台查询数据失败, 请联系管理员 0 成功 1 Key 不存在, 请重新获取 key 再调用, 请与管理员联系 2 API 接口权限过期, 请与管理员联系 3 用户 key 已被禁用, 请与客户管理员联系 4 当前 api 用户已经没有权限, 请与管理员联系 5 请求参数 key 为空 6 请求参数 daysnumber 为空 7 没有查询到任何数据 1.8 台风详情服务 1.8.1 接口功能介绍 根据台风 ID 获取台风详情数据接口服务 1.8.2 接口参数说明 参数名称 参数格式 参数含义 备注 id id =75 台风 ID 必填参数 key key=5cdd7336161c4ce2a27a21d770b8aa53 Api 用户 32 位字符串 key( 使用 md5 加密后的字符 ) 必填参数 1.8.3 接口 URL URL 请求地址 : http://api.myships.com/dataapiserver/gettyphoonbystormid?id=75&key=5cdd7336161c4ce2a27a21d770b8aa53 1.8.4 返回结果说明 "result": [ "rad_nwq50kt": "", "movespeed": 6, "lon": -169.3, 10
,, "maxwindgust": 45, "typhoonforecasttime": "2015-11-29 18", "movedirection": 145, "timedif": "0", "rad_nwq34kt": 80, "typhoontime": "2015-11-29 18", "id": 333, "history": "0", "maxwindsustain": 35, "lat": -17.9, "airpressure": 997, "rad_nwq64kt": "" "rad_nwq50kt": "", "movespeed": 13, "lon": -170.60001, "maxwindgust": 50, "typhoonforecasttime": "2015-11-29 06", "movedirection": 130, "timedif": "0", "rad_nwq34kt": 85, "typhoontime": "2015-11-29 06", "id": 333, "history": "1", "maxwindsustain": 40, "lat": -16.5, "airpressure": 994, "rad_nwq64kt": "" "rad_nwq50kt": "", "movespeed": 10, "lon": -171.89999, "maxwindgust": 50, "typhoonforecasttime": "2015-11-29 00", "movedirection": 140, "timedif": "0", "rad_nwq34kt": 85, "typhoontime": "2015-11-29 00", "id": 333, "history": "1", "maxwindsustain": 40, "lat": -15.7, "airpressure": 994, "rad_nwq64kt": "" 11
,,, "rad_nwq50kt": "", "movespeed": 7, "lon": -172.8, "maxwindgust": 50, "typhoonforecasttime": "2015-11-28 18", "movedirection": 115, "timedif": "0", "rad_nwq34kt": 85, "typhoontime": "2015-11-28 18", "id": 333, "history": "1", "maxwindsustain": 40, "lat": -14.7, "airpressure": 994, "rad_nwq64kt": "" "rad_nwq50kt": "", "movespeed": 11, "lon": -173.39999, "maxwindgust": 50, "typhoonforecasttime": "2015-11-28 06", "movedirection": 115, "timedif": "0", "rad_nwq34kt": 65, "typhoontime": "2015-11-28 06", "id": 333, "history": "1", "maxwindsustain": 40, "lat": -14.2, "airpressure": 994, "rad_nwq64kt": "" "rad_nwq50kt": "", "movespeed": 9, "lon": -174.39999, "maxwindgust": 50, "typhoonforecasttime": "2015-11-28 00", "movedirection": 125, "timedif": "0", "rad_nwq34kt": 65, "typhoontime": "2015-11-28 00", "id": 333, 12
,,, "history": "1", "maxwindsustain": 40, "lat": -13.7, "airpressure": 994, "rad_nwq64kt": "" "rad_nwq50kt": "", "movespeed": 13, "lon": -174.89999, "maxwindgust": 45, "typhoonforecasttime": "2015-11-27 18", "movedirection": 125, "timedif": "0", "rad_nwq34kt": 55, "typhoontime": "2015-11-27 18", "id": 333, "history": "1", "maxwindsustain": 35, "lat": -13.4, "airpressure": 997, "rad_nwq64kt": "" "rad_nwq50kt": "", "movespeed": 6, "lon": -169.2, "maxwindgust": 45, "typhoonforecasttime": "2015-11-29 21", "movedirection": 145, "timedif": "3", "rad_nwq34kt": 80, "typhoontime": "2015-11-29 18", "id": 333, "maxwindsustain": 35, "lat": -18.2, "airpressure": 997, "rad_nwq64kt": "" "rad_nwq50kt": "", "movespeed": "", "lon": -169.14999, "maxwindgust": 40, "typhoonforecasttime": "2015-11-30 00", "movedirection": "", 13
"timedif": "6", "rad_nwq34kt": "", "typhoontime": "2015-11-29 18", "id": 333, "maxwindsustain": 30, "lat": -18.4, "airpressure": 1000, "rad_nwq64kt": "", "rad_nwq50kt": "", "movespeed": "", "lon": -169, "maxwindgust": 40, "typhoonforecasttime": "2015-11-30 06", "movedirection": "", "timedif": "12", "rad_nwq34kt": "", "typhoontime": "2015-11-29 18", "id": 333, "maxwindsustain": 30, "lat": -18.9, "airpressure": 1000, "rad_nwq64kt": "" ], "status": "0" 返回参数 参数说明 status 状态码 result 返回信息 (json 字符串数组 ) id 台风 id typhoontime 台风点时间 (UTC- 时间 ) typhoonforecasttime 预报点时间 (UTC- 时间 ) timedif history 预报点时间 = 台风点时间 +timedif,(timdif 为 0 代表当前台风点 不为 0 代表当前台风点以后的预报数据 ) 0 为当前台风点,1 为历史台风点, 没有 history 值的为当前台风点的预报点 lon 经度 ( 单位度 ) lat 纬度 ( 单位度 ) movedirection 移向 ( 单位度 ) 14
movespeed 移速 ( 单位节 ) airpressure 最低中心气压 (Mb) maxwindsustain 最大持续风速 ( 单位节 ) maxwindgust 最大阵风速 ( 单位节 ) rad_nwq34kt rad_nwq50kt rad_nwq64kt 34kt 风圈半径 (nm) 50kt 风圈半径 (nm) 64kt 风圈半径 (nm) 返回状态码说明 : 返回参数参数说明 -1 后台查询数据失败, 请联系管理员 0 成功 1 Key 不存在, 请重新获取 key 再调用, 请与管理员联系 2 API 接口权限过期, 请与管理员联系 3 用户 key 已被禁用, 请与客户管理员联系 4 当前 api 用户已经没有权限, 请与管理员联系 5 请求参数 key 为空 6 请求参数 id 不合法 7 请求参数 id 为空 8 没有查询到任何数据 2 调用示例 2.1 java 版 public final static String MD5(String s) char hexdigits[]='0','1','2','3','4','5','6','7','8','9','a','b','c', 'd','e','f'; try byte[] btinput = s.getbytes(); // 获得 MD5 摘要算法的 MessageDigest 对象 MessageDigest mdinst = MessageDigest.getInstance("MD5"); // 使用指定的字节更新摘要 15
mdinst.update(btinput); // 获得密文 byte[] md = mdinst.digest(); // 把密文转换成十六进制的字符串形式 int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) byte byte0 = md[i]; str[k++] = hexdigits[byte0 >>> 4 & 0xf]; str[k++] = hexdigits[byte0 & 0xf]; return new String(str); catch (Exception e) e.printstacktrace(); return null; /** * 1.1 船舶查询服务 * * @throws Exception */ public static void getshipid() throws Exception String url = "http://api.myships.com/dataapiserver/getshipid"; // 请求地址 户的 key String comkey=getcomkey(); // 获取加密种子 JSONObject json=jsonobject.fromobject(comkey); String key="4297f44b13955235245b2497399d7a93";// 宝船网分配给用 HttpClient httpclient = new HttpClient(); Map<String, Object> map = new HashMap<String, Object>(); map.put("key", MD5(json.getString("seedStr")+key)); map.put("term", " 浙岱渔 171"); HttpMethod method = postmethod(url, map); httpclient.executemethod(method); String response = method.getresponsebodyasstring(); 16
System.out.println(response); private static HttpMethod postmethod(string url, Map<String, Object> params) throws IOException PostMethod post = new PostMethod(url); post.setrequestheader("content-type", "application/x-www-form-urlencoded;charset=utf-8"); List<NameValuePair> list = new ArrayList<NameValuePair>(); if (params!=null&&!params.isempty()) Set<String> keyset = params.keyset(); for (String key : keyset) NameValuePair param = new NameValuePair(key, (String) params.get(key)); list.add(param); NameValuePair[] array = new NameValuePair[list.size()]; list.toarray(array); post.setrequestbody(array); post.releaseconnection(); return post; 注 : 本示例依赖 commons-beanutils-1.7.0.jar commons-codec-1.3.jar commons-collections-3.2.jar commons-httpclient-3.1.jar commons-lang-2.5.jar commons-logging-1.1.1.jar ezmorph-1.0.4.jar json-lib-2.1-jdk15.jar java 调用数据 API 示例工程下载地址 : http://share.weiyun.com/803c6c02de809fd95986e9295ab509f1 2.2 php 版 <?php $key='32e8220e20984aaeb565f1f59f496b1c'; // 宝船网分给客户的 key // 先调用加密种子, 获取加密种子 $url='http://api.myships.com/dataapiserver/getcomkey'; 17
?> $html = file_get_contents($url); $obj=json_decode($html); $seedstr=$obj->seedstr; $md5key=md5($seedstr. $key); $term=' 福远渔 '; $term=mb_convert_encoding( $term, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' ); $getshipidurl='http://api.myships.com/dataapiserver/getshipid?term='.$term.'&key='.$md5key; $getshipidreutrn = file_get_contents($getshipidurl); echo $getshipidreutrn; 2.3 c# 版 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Net; using System.IO; using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Security.Cryptography; namespace MvcApplication.Controllers [HandleError] public class HomeController : Controller private string key = "853df97e193a48a0b202cec6810bd23c"; // 宝船网分给客户的 key,09.24 过期 private static Encoding DEFAULT_ENCODING = Encoding.GetEncoding("UTF-8"); //private static string ACCEPT = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; // private static string CONTENT_TYPE = "application/x-www-form-urlencoded"; private static string USERAGENT = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.2;.NET CLR 2.0.50727;.NET CLR 3.0.04506.648;.NET CLR 3.5.21022;.NET CLR 3.0.4506.2152;.NET CLR 3.5.30729; msn OptimizedIE8;ZHCN)"; public ActionResult Index() //ViewData["Message"] = "Welcome to ASP.NET MVC!"; string d = getcomkey(); 18
JObject o = JObject.Parse(d); string seedstr = (string)o["seedstr"]; string md5key = MD5(seedstr + key); string rtnstr = getshipid(" 东海救 101", md5key); ViewData["Message"] = rtnstr; Console.WriteLine(rtnstr); return View(); /// <summary> /// 调用 getshipid 接口获取结果 /// </summary> /// <param name="shipname"></param> /// <param name="key"></param> /// <returns></returns> private string getshipid(string ShipName, string md5key) string term = HttpUtility.UrlEncode(ShipName); string getshipidurl = "http://api.myships.com/dataapiserver/getshipid?term=" + term + "&key=" + md5key; return GetHtmlContent(getshipidurl); /// <summary> /// 返回 16 进制的 md5 字符串 /// </summary> /// <param name="mystring"></param> /// <returns></returns> public static string MD5(string mystring) byte[] textbytes = System.Text.Encoding.Default.GetBytes(myString); try System.Security.Cryptography.MD5CryptoServiceProvider crypthandler; crypthandler = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] hash = crypthandler.computehash(textbytes); string ret = ""; foreach (byte a in hash) if (a < 16) ret += "0" + a.tostring("x"); // 兼容 php 的 md5 else ret += a.tostring("x"); return ret; 19
catch throw; /// <summary> /// 获取宝船网的加密种子 /// </summary> /// <returns></returns> private string getcomkey() string url = "http://api.myships.com/dataapiserver/getcomkey"; return GetHtmlContent(url); /// <summary> /// 获取网址 HTML /// </summary> /// <param name="url"> 网址 </param> /// <returns> </returns> /// public static string GetHtmlContent(string url) return GetHtmlContent(url, DEFAULT_ENCODING); /// <summary> /// 获取网址 HTML /// </summary> /// <param name="url"> 网址 </param> /// <returns> </returns> public static string GetHtmlContent(string url, Encoding encoding) string html = string.empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.useragent = USERAGENT; request.credentials = CredentialCache.DefaultCredentials; WebResponse response = request.getresponse(); using (Stream stream = response.getresponsestream()) using (StreamReader reader = new StreamReader(stream, encoding)) html = reader.readtoend(); 20
reader.close(); return html; public ActionResult About() return View(); 注 : 本示例依赖 ASP.NET MVC 3, Newtonsoft.Json c# 调用数据 API 示例工程下载地址 : http://share.weiyun.com/21c52f5fe2668228db3c9f0af06f43f5 ASP.NET MVC 3 下载地址 : http://share.weiyun.com/51efe720bd05093851e483f46a888d4d http://share.weiyun.com/f22fb4b10c007cf836bdb9d14cbb19fb Newtonsoft.Json 下载地址 : http://share.weiyun.com/f84ac5f8a910f17f007942a549850c55 2.4 python 版 #coding:utf-8 import urllib2, urllib import md5 import json # 调用 getcomkey 获取加密种子 def getcomkey(): f = urllib2.urlopen(url='http://api.myships.com/dataapiserver/getcomkey'); encodedjson = json.loads(f.read()); key="48c71f17f7b2430db190fb065e8c1296"; seedstr=encodedjson["seedstr"]; m1 = md5.new(); m1.update(seedstr+key); return m1.hexdigest(); # 调用 1.1 船舶查询服务 data = 'term' : ' 东海救 115', 'key' : getcomkey() 21
getshipidreturnstr = urllib2.urlopen(url='http://api.myships.com/dataapiserver/getshipid', data=urllib.urlencode(data)) print getshipidreturnstr.read(); # 调用 1.2 船舶基本信息查询服务 data = 'shipid' : '413046050', 'key' : getcomkey() getshipbasicreturnstr = urllib2.urlopen(url='http://api.myships.com/dataapiserver/getshipbasicinforma tion', data=urllib.urlencode(data)) print getshipbasicreturnstr.read(); # 调用 1.3 船舶最新船位查询服务 data = 'shipid' : '413046050', 'key' : getcomkey() getshiplatestreturnstr = urllib2.urlopen(url='http://api.myships.com/dataapiserver/getshiplatest', data=urllib.urlencode(data)) print getshiplatestreturnstr.read(); # 调用 1.4 船舶历史轨迹查询服务 data = 'shipid' : '413046050', 'key' : getcomkey(),'starttime' : '2015-09-12 15:21:43','endTime' : '2015-09-14 15:21:43' getshiphistortrackreturnstr = urllib2.urlopen(url='http://api.myships.com/dataapiserver/getshiphistortrack', data=urllib.urlencode(data)) print getshiphistortrackreturnstr.read(); # 调用 1.5 获取指定区域船舶服务 data = 'key' : getcomkey(),'xmin' : '73012333','ymin' : '18799453','xmax' : '73192333','ymax' : '18979453' getshipbyrectreturnstr = urllib2.urlopen(url='http://api.myships.com/dataapiserver/getshipbyrect', data=urllib.urlencode(data)) print getshipbyrectreturnstr.read(); 注 : 本示例依赖 ptyhon2.7 DataApi.py 下载地址 : http://share.weiyun.com/4d171a02bf5d60fef5ba3ca3aeff5c5a 2.5 JS 版 var querykey = "48c71f17f7b2430db190fb065e8c1296"; 22
/** * # 调用 getcomkey 获取加密种子 * * **/ function getcomkey() var seed; var url = "http://api.myships.com/dataapiserver/getcomkey"; $.getjson(url, function(data) if (data.status == 0) seed = data.seedstr; ); return seed; /** * # 调用 1.1 船舶查询服务 * * **/ function getshipid() $.ajaxsettings.async = false; var seed = getcomkey(); var key = hex_md5(seed + querykey); var url = "http://api.myships.com/dataapiserver/getshipid"; $.getjson(url, term : ' 东海救 115', key : key, function(data) alert(json.stringify(data)); ); 注 : 本示例依赖 jquery.js,md5.js,json2.js jquery.js 下载地址 : http://share.weiyun.com/02c0b7586f78224e3c8defd62c4e71d6 md5.js 下载地址 : http://share.weiyun.com/7403b55bf232717f15be212d6b7694ba json2.js 下载地址 : http://share.weiyun.com/8367edfba61107f4fc3b38b23c39990b 23