Speculation fund lost, grab data out....

Ah ~ ~ I didn't want to take some dead wages and go to buy funds, but I lost money all the time. After work, I was going to do some data research. As a result, the interface provided by XXX platform still received money, but I had to go to other websites to ask for it .
(interested friends leave email to design an investment fund algorithm that suits them?)
Because it is written under windows, there is no sub process in php for grabbing multiple contents. curl_multi is used, and the speed is OK, so I will use it....

 * Fund data capture
 * User: zyy
 * Date: 2017/12/22
 * Time: 16:13

//Fund Classification [all, index type, stock type, mixed type]
$urlOrderBy = ['all'=>'all','zs'=>'zs', 'gp'=>'gp','hh'=>'hh'];
//Growth period [weekly growth, monthly growth, March growth, June growth, annual growth, three-year growth]
$urlZf = ['zzf'=>'zzf','1yzf'=>'1yzf','3yzf'=>'3yzf','6yzf'=>'6yzf','1nzf'=>'1nzf','3nzf'=>'3nzf'];

/** Get fund data URL
 * @param string $order [Sort type, reference variable $urlOrderBy]
 * @param string $zf  [Increase type, reference variable $urlZf]
 * @param int $zfPaiMing [The number required for the increase ranking is limited only when the increase type parameter is set. If the weekly increase is not set, the $needNum will be invalid]
 * @param int $needNum [Required items]
 * @return string
function buildJiJinUrl($order,$zf,$zfPaiMing,$needNum=4000){
    $url = 'http://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft=';
    //Classified splicing
    //Increase order splicing
    $today = date('Y-m-d');
    $lastYear = date('Y-m-d',strtotime('-1 year'));
    return $url;

/** curl Request a separate request
 * @param $url
 * @param string $method
 * @param array $requestData
 * @return mixed
function curlRequest($url,$method='get',$requestData=[])
        $curlHandle = curl_init();
            curl_setopt($curlHandle, CURLOPT_POST, true);
            curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $requestData);
        $response = curl_exec($curlHandle);
        return $response;
    }catch (\Exception $e){
        exit('request was aborted:'.$e->getMessage());

/** curl Request bulk request
 * @param array $url
 * @param array $method
 * @param array $requestData
 * @return array
function curlRequests(array $url, array $method,array $requestData)
        //Assemble curl
        $allCurlHandle = [];
        foreach ($url as $key=>$value){
            $allCurlHandle[$key] = curl_init();
            curl_setopt($allCurlHandle[$key] ,CURLOPT_URL,$value);
            curl_setopt($allCurlHandle[$key] ,CURLOPT_SSL_VERIFYPEER,false);
            curl_setopt($allCurlHandle[$key] ,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($allCurlHandle[$key] , CURLOPT_POST, true);
                curl_setopt($allCurlHandle[$key] , CURLOPT_POSTFIELDS, $requestData[$curlNum]);
        // Create batch cURL handle
        $mh = curl_multi_init();
        // Add handle

        foreach ($allCurlHandle as $value){

        $response = [];
        do {// polling
            while (($execrun = curl_multi_exec($mh, $running)) == CURLM_CALL_MULTI_PERFORM){
                if ($execrun != CURLM_OK) { return $response; }
            while ($done = curl_multi_info_read($mh)) { // Once a request is completed, find it out and process it. Because the bottom layer of curl is select, the maximum limit is 1024
//                $response [] = curl_getinfo ($done ['handle ']; / / get information from request
                $response[] =  curl_multi_getcontent($done['handle']);
                curl_multi_remove_handle($mh, $done['handle']);  // Delete curl handle after the request is completed
            if ($running) {// When there is no data, block it, give the CPU use right, and avoid the CPU 100% caused by the data of the above do dead cycle idling
                $rel = curl_multi_select($mh, 1);
                if($rel == -1){   usleep(1000);  }
            if( $running == false){  return $response; }
        } while (true);
        return $response;
    }catch (\Exception $e){
        exit('request was aborted:'.$e->getMessage());

//For example, the URL will return to the top 10 of this week's increase
$url1 = buildJiJinUrl($urlOrderBy['zs'],$urlZf['zzf'],1,1);
$url2 = buildJiJinUrl($urlOrderBy['gp'],$urlZf['zzf'],1,1);
$url3 = buildJiJinUrl($urlOrderBy['hh'],$urlZf['zzf'],1,1);
$url4 = buildJiJinUrl($urlOrderBy['zs'],$urlZf['zzf'],3,3);

$urls = [$url1,$url2,$url3,$url4];
$methods =['get','get','get','get'];
$info = curlRequests($urls,$methods,array());

//Fetching multiple data at a time

Tags: curl PHP Windows

Posted on Mon, 04 May 2020 06:47:30 -0700 by ruraldev