PHP train ticket query api,12306 official API

Source: Internet
Author: User
Tags foreach date array end json net query ticket

I'm doing my micro-mail when I need the train ticket to check things, registration of things and so on too much, today's simple to do a test to see if you can get the data how to deal with how fast, because it is a personal do not go to other sites to find interfaces, on their own to do the next! Let's put the way out!

<?php/** * Train ticket Query method simple attempt * @copyright Widuu * @license http://www.widuu.com * @lastmodify 2013-6-20/F Unction Json_array ($json) {if ($json) {foreach (array) $json as $k => $v) {$data [$k] =!is_string ($v)? Json_array
			($v): $v;
		return $data;
		} function Doget ($start, $end, $time)//Get Fetch data using {if (empty ($time)) {$time = date (' y-m-d ', Time ());
				}else{if (substr ($time, 0,1)!=0) {$time = date (' Y-0 ', Time ()). $time;
			Echo substr ($time, 0, 1);
			}else{$time = date (' Y ', Time ()). $time; }} $name = Include ("..
		/name.php ");
		$star = $name [$start];
		$end = $name [$end]; $url = "http://dynamic.12306.cn/otsquery/query/queryremanentticketaction.do?method=queryleftticket& orderrequest.train_date={$time}&orderrequest.from_station_telecode={$star}&orderrequest.to_station_ telecode={$end}&orderrequest.train_no=&trainpasstype=qb&trainclass=qb%23d%23z%23t%23k%23qt%23& Includestudent=00&seattypeandnum=&orderreqUest.start_time_str=00%3a00--24%3a00 "; $optionget = Array (' http ' => Array (' method ' => ' get ', ' header ' => ' user-agent:mozilla/4.0 compatible; MSIE 7.0; Windows NT 6.0; SLCC1. NET CLR 2.0.50727; Media Center PC 5.0. NET CLR 3.5.21022;. NET CLR 3.0.04506; CIBA) \r\naccept:*/*\r\nreferer:http://dynamic.12306.cn/otsquery/query/queryremanentticketaction.do?method=init 
		"));
		$file = File_get_contents ($url, False, Stream_context_create ($optionget));
	Return Json_array (Json_decode ($file));
  	$result = doget ("Beijing", "Langfang", "6-23");
	$result = Strip_tags ($result [' datas ']);
	$return _str = Str_replace (" ", "", $result);
	$return _str = str_replace ("\\n", "", $return _str);
	$a = explode (",", $return _str);
	$name =array ();
	$c = Array_chunk ($a, 16);
	Array_pop ($c); foreach ($c as $k => $v) {$str = "Rest ticket:<br> Business seat:". $v [5]. ", principal seat:". $v [6]. ", first-class:". $v [7]. ", Second-class:". $v [8]. ", Advanced soft Sleeper:". $v [9]
		. ", Soft Sleeper:". $v [10]. ", Hard Sleeper:". $v [11]. ", Soft-seat:". $v [12]. ", Hard seat:" $v [13]. ", no seats:". $v [14]. ", Other:". $v [15]; $str = Preg_replace ("/, Hard seat \:--,/"," ", $str);
		$str = Preg_replace ("/Business seat \:--,/", "", $str);
		$str = Preg_replace ("/, principal seat \:--,/", "", $str);
		$str = Preg_replace ("/, first-class \:--,/", "", $str);
		$str = Preg_replace ("/, Second-class \:--,/", "", $str);
		$str = Preg_replace ("/, Advanced soft sleeper \:--,/", "", $str);
		$str = Preg_replace ("/, soft sleeper \:--,/", "", $str);
		$str = Preg_replace ("/, Hard Sleeper \:--,/", "", $str);
		$str = Preg_replace ("/, Soft-seat \:--,/", "", $str);
		$str = Preg_replace ("/, Hard seat \:--,/", "", $str);
		$str = Preg_replace ("/, Other \:--/", "", $str);
	echo "Train: {$v [1]}, Station: {$v [2]}, Arrival: {$v [3]}, Duration: {$v [4]},<br>{$str}<br>];" } http://www.widuu.com/archives/06/682.html


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.