[Minor functions] ThinkPHP develops the Guangzhou Public Transit Real-Time query function

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn how to query the required data through the real-time bus query of yangyang Network (formerly known as "Guangzhou Asian transportation network, for example, the current location of line search, line site, and bus
### URL

Http://www.chenhaizan.cn/bus

### Ideas




Extract data based on the bus query rules on the official website

### Display page

/**
* Likes blogs and likes life v1.0
* ==============================================
* Bus query
* ++
* The world's major events will be fine, and the world's difficulties will be easy.
* If you know how to think independently, PHPer is not a grassroots. What you do is to commit to the database.
* ==============================================
* $ Author: Chen haizan QQ: 526199364 $
*/
Class BusAction extends HomeAction {
// Search for the bus name and address
Public $ getBusNameUrl = 'HTTP: // www.gzyyjt.com/ajax/getBusNames.ashx? Key = ';
// Obtain the bus route address
Public $ getBusLineUrl = 'HTTP: // www.gzyyjt.com/Bus_station.aspx? Bn = ';

/**
* Initialization
*/
Public function _ initialize (){
Parent: _ initialize ();
}

/**
* Display page
*/
Public function index (){
$ This-> assign ('here _ action', 'guangzhou real-time bus query ');
$ This-> display ('html: bus ');
}
}
### Obtain the bus route name

/**
* Get the public transit name
*/
Public function getBusNames (){
// Verify the bus name
$ Name = $ this-> getName ();
// Get the public transit name
$ Content = file_get_contents ($ this-> getBusNameUrl. $ name );
// Does not exist
If (empty ($ content )){
$ This-> error ('this line does not exist! ');
}
// Convert to an array
$ Content = explode (',', $ content );
// Set the return status
$ Data ['status'] = in_array ($ name, $ content )? 2:1;
$ Data ['data'] = in_array ($ name, $ content )? $ Name: $ content;
// Return data
$ This-> ajaxReturn ($ data );
}

Public function getName (){
$ Name = I ('name ');

If (empty ($ name )){
$ This-> error ('enter the line name! ');
}
Return $ name;
}
Directly use the 'file _ get_contents () 'function to obtain official data. The official data returned is in the format of line 1, line 2...

Convert the obtained data to an array, and then determine whether the search parameter is in the array. If yes, status 2 is returned, so that the front-end can directly send a request to the background to obtain the site;

Otherwise, all queried lines are displayed, allowing the user to select.

### Obtain bus routes

/**
* Get bus lines
*/
Public function getBusLine (){
// Verify the bus name
$ Name = $ this-> getName ();
// Obtain the bus route from the database
$ Bus = M ('bus ');
$ Line = $ bus-> where (array ('name' => $ name)-> field (true)-> find ();
// This line does not exist in the Database
If (empty ($ line ['line']) {
// Load SimpleHtmlDom class
Import ("@. ORG. Util. SimpleHtmlDom ");
$ Content = file_get_html ($ this-> getBusLineUrl. $ name );

If (empty ($ content )){
$ This-> error ('this line does not exist! ');
}

$ Data = array ();
// Obtain the line name
Foreach ($ content-> find ('. show_businfo. lines') as $ v ){
Foreach ($ v-> find ('A') as $ I ){
$ Data [] = $ I-> plaintext;
}
Break;
}
If (empty ($ data )){
$ This-> error ('this line does not exist! ');
}

$ _ Data = array (
'Name' => $ name,
'Line' => implode (',', $ data ),
);

If ($ line ['id']> 0 ){
// Update operations if an ID exists
$ Bus-> where (array ('id' => $ line ['id'])-> save ($ _ data );
} Else {
// Add operation
$ Bus-> add ($ _ data );
}

} Else {
// If yes, the count is increased by 1.
$ Bus-> where (array ('id' => $ line ['id'])-> setInc ('Count ');
$ Data = explode (',', $ line ['line']);
}
// Return data
$ Data ['data'] = $ data;
$ Data ['status'] = 1;
$ This-> ajaxReturn ($ data );
}
First determine whether the database has data, if not, use the [SimpleHtmlDom] (http://sourceforge.net/projects/simplehtmldom/) class from the official get site data...

> Of course, you can also use the regular expression matching method to obtain site data. Here you can use the 'simplehtmldom 'class to speed up...

> For details about how to use the 'simplehtmldom 'class, go to the official website to learn more...

> Or use the query site interface provided by other third parties, but the site name must be the same as the official website, because the passed parameters must correspond to, or else an error will occur...

If yes, the count is added with 1, which is used to judge the most queried results later...

The retrieved data is stored in the database and returned to the page.

Check the query on the official website. The page displays the back-and-forth route of the line. Here we only need to obtain one of them... therefore, the 'Break' statement is added to the program 'foreach...



### Obtain the current bus location

/**
* Get the current location of the bus
*/
Public function getBusPosition (){
$ Line = I ('line ');
$ Name = I ('name ');
$ Start = I ('start ');

If (empty ($ line) | empty ($ name) | empty ($ start )){
$ This-> error ('error, incomplete information! ');
}
// Obtain information
$ Url = 'HTTP: // www.gzyyjt.com/Bus_station.aspx? Bn = '. $ line.' & sn = '. $ name.' & SPNs = '. $ start;
$ Content = file_get_contents ($ url );
// Obtain the required data through regular expression matching.
Preg_match_all ("/. val \ (\) = '". $ start. "' & (\ d +) <= min/", $ content, $ matches );
// If yes, status 1 is returned.
If (isset ($ matches [1] [0]) {
$ Data ['position'] = $ matches [1] [0];
$ Data [status] = 1;
// If a user logs on, the query information is saved.
If (! Empty (self: $ Cache ['mid ']) {
$ Where = array (
'Mid '=> self: $ Cache ['mid'],
'Line' => $ line,
'Name' => $ name,
'Start' => $ start,
);
// Query count plus 1
$ Count = M ('buscount')-> where ($ where)-> setInc ('Count ');
If (empty ($ count )){
// If the Count fails to increase, the initial data is not saved. Add the data here.
$ Where ['Count'] = 1;
M ('buscount')-> add ($ where );
}
}
} Else {
// If yes, status 2 is returned.
$ Data [status] = 2;
}

$ This-> ajaxReturn ($ data );
}
Use 'file _ get_contents () 'to retrieve the entire page 'html', and then use regular matching to obtain the data we need.

Here we cannot use the 'simplehtmldom 'class, because the design on the official website is different. It refers to writing data in the JQ Statement, which is loaded on HTML after the page is run, so you can only use regular expressions...

You can see the following in the HTML of the page:



Write Regular Expression matching to obtain numbers.

If it matches, status 1 is returned.

If not, status 2 is returned, prompting you to click query again

### Conclusion

It took three days and nights to make it almost done, and now I don't want to change it too much...

I still want to add records for user queries and do not open them to non-users, but it is not necessary to think about it. This function will be available here. After all, this function is of little use, and I just used it myself...

And the official website has a query, and China Telecom and China Unicom already have a real-time bus query APP...

This is a little thing you learn and make fun of yourself...

Query .rar (11.53 KB download: 153 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.