Simple collection of some yahoo data _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags filetime flock
Some yahoo data is collected. I used to write similar things in my company. this time I wrote a simple collection program for my boss. Very simple .. Khan. No technical skills. Data source: c has previously written similar things in the company. this time, it helped previous superiors write a simple collection program.

Very simple .. Khan. No technical skills.
Data source: http://cn.finance.yahoo.co...
Demo address: http: // traffic02.100steps ....

(Added the data cache function .. Khan, I didn't use lite_cache. I wrote the simplest one myself ..)

The code is as follows:


Set_time_limit (0 );

$ Max_time = 3600;
Export cache_file='cache_yahoo.txt ';

$ Nowtime = time ();
If (! File_exists ($ cache_file )){
$ Filetime = 0;
} Else {
$ Filetime = filemtime ($ cache_file );
}

If ($ filetime + $ max_time <$ nowtime ){
// Update
Ob_start ();
Function get_yahoo_info ($ url ){
$ Content = file_get_contents ($ url );

$ Pattern = '|(.*)\((.*)\)(.*)| U ';
Preg_match_all ($ pattern, $ content, $ out );
$ Info ['company _ name'] = $ out [1] [0];
$ Info ['company _ stock'] = $ out [2] [0];

$ Pattern = '|Latest transaction price:(.*)| U ';
Preg_match_all ($ pattern, $ content, $ out );
$ Info ['stock _ price'] = $ out [1] [0];

$ Pattern = '|Ups and downs:(.*)| U ';
Preg_match_all ($ pattern, $ content, $ out );
$ Info ['stock _ uporlow'] = $ out [1] [0];

$ Pattern = '|Market value:(.*)| U ';
Preg_match_all ($ pattern, $ content, $ out );
$ Info ['stock _ value'] = $ out [1] [0];

Return $ info;
}
Using urls1_explode(',', file_get_contents('url.txt '));
$ I = 0;
$ Matchs = array ();
Foreach ($ urls as $ url ){
$ Rs = get_yahoo_info ($ url );
If (! Empty ($ rs )){
$ Matchs [$ I] = $ rs;
$ I ++;
$ Rs = '';
}
}

// Print_r (get_yahoo_info ('http: // cn.finance.yahoo.com/q? S = 000063. SZ '));
?>























$ I = 1;Foreach ($ matchs as $ match ){?> $ I ++;}?>

Company name

Stock code

Stock price

Stock increase

Market value


$ Content = ob_get_clean ();
$ Fp = fopen ($ cache_file, 'w ');
If (! $ Fp) echo 'failed to open the file ';
If (flock ($ fp, LOCK_EX) {// sort it to lock
If (! Fwrite ($ fp, $ content) echo 'file write failed ';
Flock ($ fp, LOCK_UN); // release lock
} Else {
// Echo "Couldn't lock the file! ";
If (! Fwrite ($ fp, $ content) echo 'file write failed ';
}
Fclose ($ fp );
// Echo 'cache time: '. time ();
Echo $ content;
} Else {
// Echo 'cache! ';
Echo file_get_contents ($ cache_file );
}
?>

Bytes. Very simple .. Khan. No technical skills. Data source: http: // c...

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.