首页 > php教程 > PHP源码 > 正文

基于php的12306火车票查询接口调用代码实例

PHP中文网
发布: 2016-05-22 18:27:05
原创
2217人浏览过

代码描述:基于php的12306火车票查询接口调用代码实例

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

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

<!--?php

// +----------------------------------------------------------------------

// | JuhePHP [ NO ZUO NO DIE ]

// +----------------------------------------------------------------------

// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.

// +----------------------------------------------------------------------

// | Author: Juhedata <info@juhe.cn-->

// +----------------------------------------------------------------------

   

//----------------------------------

// 12306火车票查询调用示例代码 - 聚合数据

// 在线接口文档:http://www.juhe.cn/docs/22

//----------------------------------

   

header('Content-type:text/html;charset=utf-8');

   

   

//配置您申请的appkey

$appkey = "*********************";

   

   

   

   

//************1.站到站查询(含票价)************

$url = "http://apis.juhe.cn/train/s2swithprice";

$params = array(

      "start" => "",//出发站

      "end" => "",//终点站

      "key" => $appkey,//应用APPKEY(应用详细页查询)

      "dtype" => "",//返回数据的格式,xml或json,默认json

);

$paramstring = http_build_query($params);

$content = juhecurl($url,$paramstring);

$result = json_decode($content,true);

if($result){

    if($result['error_code']=='0'){

        print_r($result);

    }else{

        echo $result['error_code'].":".$result['reason'];

    }

}else{

    echo "请求失败";

}

//**************************************************

   

   

   

   

//************2.12306订票②:车次票价查询************

$url = "http://apis.juhe.cn/train/ticket.price.php";

$params = array(

      "train_no" => "",//列次编号,对应12306订票①:查询车次中返回的train_no

      "from_station_no" => "",//出发站序号,对应12306订票①:查询车次中返回的from_station_no

      "to_station_no" => "",//出发站序号,对应12306订票①:查询车次中返回的to_station_no

      "date" => "",//默认当天,格式:2014-12-25

      "key" => $appkey,//应用APPKEY(应用详细页查询)

);

$paramstring = http_build_query($params);

$content = juhecurl($url,$paramstring);

$result = json_decode($content,true);

if($result){

    if($result['error_code']=='0'){

        print_r($result);

    }else{

        echo $result['error_code'].":".$result['reason'];

    }

}else{

    echo "请求失败";

}

//**************************************************

   

   

   

   

//************3.车次查询************

$url = "http://apis.juhe.cn/train/s";

$params = array(

      "name" => "",//车次名称,如:G4

      "key" => $appkey,//应用APPKEY(应用详细页查询)

      "dtype" => "",//返回数据的格式,xml或json,默认json

);

$paramstring = http_build_query($params);

$content = juhecurl($url,$paramstring);

$result = json_decode($content,true);

if($result){

    if($result['error_code']=='0'){

        print_r($result);

    }else{

        echo $result['error_code'].":".$result['reason'];

    }

}else{

    echo "请求失败";

}

//**************************************************

   

   

   

   

//************4.站到站查询************

$url = "http://apis.juhe.cn/train/s2s";

$params = array(

      "start" => "",//出发站

      "end" => "",//终点站

      "traintype" => "",//列车类型,G-高速动车 K-快速 T-空调特快 D-动车组 Z-直达特快 Q-其他

      "key" => $appkey,//应用APPKEY(应用详细页查询)

      "dtype" => "",//返回数据的格式,xml或json,默认json

);

$paramstring = http_build_query($params);

$content = juhecurl($url,$paramstring);

$result = json_decode($content,true);

if($result){

    if($result['error_code']=='0'){

        print_r($result);

    }else{

        echo $result['error_code'].":".$result['reason'];

    }

}else{

    echo "请求失败";

}

//**************************************************

   

   

   

   

//************5.12306实时余票查询************

$url = "http://apis.juhe.cn/train/yp";

$params = array(

      "key" => $appkey,//应用APPKEY(应用详细页查询)

      "dtype" => "",//返回数据的格式,xml或json,默认json

      "from" => "",//出发站,如:上海虹桥

      "to" => "",// 到达站,如:温州南

      "date" => "",//出发日期,默认今日

      "tt" => "",//车次类型,默认全部,如:G(高铁)、D(动车)、T(特快)、Z(直达)、K(快速)、Q(其他)

);

$paramstring = http_build_query($params);

$content = juhecurl($url,$paramstring);

$result = json_decode($content,true);

if($result){

    if($result['error_code']=='0'){

        print_r($result);

    }else{

        echo $result['error_code'].":".$result['reason'];

    }

}else{

    echo "请求失败";

}

//**************************************************

   

   

   

   

//************6.12306订票①:查询车次************

$url = "http://apis.juhe.cn/train/ticket.cc.php";

$params = array(

      "from" => "",//出发站名称:如上海虹桥

      "to" => "",//到达站名称:如温州南

      "date" => "",//默认当天,格式:2014-07-11

      "tt" => "",//车次类型,默认全部,如:G(高铁)、D(动车)、T(特快)、Z(直达)、K(快速)、Q(其他)

      "key" => $appkey,//应用APPKEY(应用详细页查询)

);

$paramstring = http_build_query($params);

$content = juhecurl($url,$paramstring);

$result = json_decode($content,true);

if($result){

    if($result['error_code']=='0'){

        print_r($result);

    }else{

        echo $result['error_code'].":".$result['reason'];

    }

}else{

    echo "请求失败";

}

//**************************************************

   

   

   

   

//************7.火车票代售点查询************

$url = "http://apis.juhe.cn/train/dsd";

$params = array(

      "province" => "",//省份,如:浙江

      "city" => "",//城市,如:温州

      "county" => "",//区/县,如:鹿城区

      "key" => $appkey,//应用APPKEY(应用详细页查询)

      "dtype" => "",//返回数据的格式,xml或json,默认json

);

$paramstring = http_build_query($params);

$content = juhecurl($url,$paramstring);

$result = json_decode($content,true);

if($result){

    if($result['error_code']=='0'){

        print_r($result);

    }else{

        echo $result['error_code'].":".$result['reason'];

    }

}else{

    echo "请求失败";

}

//**************************************************

   

   

   

   

//************8.列车站点列表************

$url = "http://apis.juhe.cn/train/station.list.php";

$params = array(

      "key" => $appkey,//应用APPKEY(应用详细页查询)

      "dtype" => "",//返回数据的格式,xml或json,默认json

);

$paramstring = http_build_query($params);

$content = juhecurl($url,$paramstring);

$result = json_decode($content,true);

if($result){

    if($result['error_code']=='0'){

        print_r($result);

    }else{

        echo $result['error_code'].":".$result['reason'];

    }

}else{

    echo "请求失败";

}

//**************************************************

   

   

   

   

   

/**

 * 请求接口返回内容

 * @param  string $url [请求的URL地址]

 * @param  string $params [请求的参数]

 * @param  int $ipost [是否采用POST形式]

 * @return  string

 */

function juhecurl($url,$params=false,$ispost=0){

    $httpInfo = array();

    $ch = curl_init();

   

    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );

    curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );

    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );

    curl_setopt( $ch, CURLOPT_TIMEOUT , 60);

    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    if( $ispost )

    {

        curl_setopt( $ch , CURLOPT_POST , true );

        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );

        curl_setopt( $ch , CURLOPT_URL , $url );

    }

    else

    {

        if($params){

            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );

        }else{

            curl_setopt( $ch , CURLOPT_URL , $url);

        }

    }

    $response = curl_exec( $ch );

    if ($response === FALSE) {

        //echo "cURL Error: " . curl_error($ch);

        return false;

    }

    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );

    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );

    curl_close( $ch );

    return $response;

}

登录后复制
铁路12306
铁路12306

铁路12306是一款由中铁程科技有限责任公司官方发行推出的购票订票软件。这款软件能够帮助用户随时随地查询火车高铁的信息,不管是想要购票还是退票改签都可以在这里进行操作,有需要的小伙伴快来保存下载体验吧!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号