Curl acquisition Yahoo bidding ranking based on keywords _ PHP Tutorial

Source: Internet
Author: User
Curl Acquisition obtains Yahoo bidding rankings based on keywords. I have previously written curl batch processing and collected data. here I paste the complete version. the code is very simple. let's talk about the code. thank you for your advice !!! The code is only written to the retrieved link. As for the data collected by the curl batch processing, the complete version is pasted here. the code is very simple. if you do not need to talk about the code, please kindly advise !!!

The code only writes to the obtained link. As for the key of the array behind the ranking, it is not the rank...

1
 6 * @ copyright 2014.04.26 By chujiu 7 * @ version 0.2.1 2014.04.26 8 */9 10 class DataCollectionRank {11 12 const PAGE = 10; 13 public $ path = ''; 14 public $ main = 91; 15 16 // add a curl handle to return resource 17 private function _ gather_data ($ keyword) {18 if (empty ($ keyword )) {19 return ''; 20} 21 $ chs = array (); // handle 22 $ mh = curl_multi_init (); 23 for ($ I = 1; $ I <= $ this-> main; $ I + = self: PAGE) {24 $ url =' http://search.yahoo.co.jp/search?p= '. Urlencode ($ keyword ). '& tid = top_ga1_sa & ei = UTF-8 & aq =-1 & oq = '. urlencode ($ keyword ). '& pstart = 1 & fr = top_ga1_sa & B = '. $ I; 25 $ ch = curl_init (); 26 // Set option 27 curl_setopt_array ($ ch, array (28 CURLOPT_URL => $ url, 29 CURLOPT_HEADER => false, 30 rows => false, 31 CURLOPT_RETURNTRANSFER => true, 32 CURLOPT_TIMEOUT => 30, 33 CURLOPT_AUTOREFERER => true 34) 35); 36 curl_multi_add_handle ($ mh, $ ch ); // Add a batch processing handle 37 $ chs ['handle'] [$ I] ['ch'] = $ ch; 38 $ chs ['handle'] [$ I] ['URL'] = $ url; 39} 40 $ chs ['MH '] = $ mh; 41 return $ chs; 42} 43 44 // process CURL request 45 public function exec_curl_get_data ($ keyword, $ path) {46 $ error = ''; 47 $ this-> path = $ path; 48 $ chs = $ this-> _ gather_data ($ keyword); 49 if (empty ($ chs) return ''; 50 // execute batch processing handle 51 $ active = null; 52 do {53 $ mrc = curl_multi_exec ($ chs ['MH '], $ ac Tive); 54 // $ info = curl_multi_info_read ($ chs ['MH ']); 55} while ($ active> 0 ); 56 // Get Data 57 $ responses = array (); 58 foreach ($ chs ['handle'] as $ k => $ ch) {59 if (curl_error ($ ch ['ch']) {60 $ error. = "\ n ". 'error prompt :'. curl_error ($ ch ['ch']). '------- URL :'. $ ch ['URL']. '-------- Time :'. date ('Y-d-m H: I: S', time ()). "\ n"; 61} else {62 $ responses [$ k] ['data'] = curl_multi_getcontent ($ ch ['ch']); 63} 64 65 // cur Rochelle multi_info_read ($ mh); 66 // close current handler 67 curl_multi_remove_handle ($ chs ['MH '], $ ch ['ch']); 68 curl_close ($ ch ['ch']); 69} 70 // close curl batch processing 71 curl_multi_close ($ chs ['MH ']); 72 $ str = ''; 73 if ($ error! = '') {74 $ this-> _writeFile('get_rank_log.txt ', $ error,' AB + '); 75} 76 foreach ($ responses as $ val) {77 if (! Empty ($ val ['data']) {78 $ str. = $ this-> _ get_keyword_link_preg ($ val ['data']); 79} 80} 81 $ str = substr ($ str, 0,-1 ); 82 $ contents = explode ('|', $ str); 83 return $ contents; 84} 85 86 // data filtering link 87 private function _ get_keyword_link_preg ($ str) {88 $ res = ''; 89 if (empty ($ str) {90 return''; 91} 92 $ arr = explode ('

', $ Str); 93 $ arr1 = explode ('

', $ Arr [1]); 94 $ arr2 = preg_replace ('#

[\ S \ S] + # ', '', $ arr1 [0]); 95 $ arr3 = preg_replace ('#

[\ S \ S] + # ', '', $ arr2); 96 $ arr4 = preg_replace ('#[\ S \ S] +?# ', '', $ Arr3); 97 if (preg_match_all (' # href = \"(.*?) \ "> # ', $ Arr4, $ arr5 )! = False) {98 foreach ($ arr5 [1] as $ val) {99 $ res. = urldecode ($ val ). '|'; 100} 101} 102 return $ res; 103} 104 105 // write the file 106 public function _ writeFile ($ fileName, $ data, $ method = "rb +", $ iflock = 1, $ check = 1, $ chmod = 1) {107 $ check & @ strpos ($ this-> path. '/'. $ fileName ,'.. ')! = False & exit ('2017 Forbidden! '); 108 @ touch ($ this-> path. '/'. $ fileName); 109 $ handle = @ fopen ($ this-> path. '/'. $ fileName, $ method); 110 if ($ iflock) {111 @ flock ($ handle, LOCK_EX); 112} 113 $ fw = @ fwrite ($ handle, $ data ); 114 if ($ method = "rb +") ftruncate ($ handle, strlen ($ data); 115 fclose ($ handle ); 116 $ chmod & @ chmod ($ this-> path. '/'. $ fileName, 0777); 117} 118} 119?>

1 function array_unique_fb ($ array) {2 $ temp = array (); 3 $ data = array (); 4 foreach ($ array as $ value) {5 $ value = join (",", $ value); // dimension reduction, you can also use implode, convert a one-dimensional array to a comma-connected string 6 $ temp [] = $ value; 7} 8 $ temp = array_flip ($ temp); // remove duplicate strings, that is, the repeated one-dimensional array 9 foreach ($ temp as $ k => $ value) {10 $ temp [$ k] = explode (",", $ value ); // re-assemble the split array 11} 12 foreach ($ temp as $ key => $ value) {13 $ data [$ key] ['keyword'] = $ value [0]; 14 $ data [$ key] ['domain '] = $ value [1]; 15} 16 return $ data; 17}

SUCCESS !!! The code is only written to get the link. as...

Related Article

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.