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....

<?php
/**
 * 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
    $url.=$order.'&rs=&gs=0&sc=';
    //Increase order splicing
    if($zf){
        $url.=$zf.','.$zfPaiMing;
    }else{
        $url.='zzf';
    }
    $today = date('Y-m-d');
    $lastYear = date('Y-m-d',strtotime('-1 year'));
    $url.="&st=desc&sd=$today&ed=$lastYear&qdii=|&tabSubtype=,,,,,&pi=1&pn=$needNum&dx=1&v=0.5339311461808629";
    return $url;
}

/** curl Request a separate request
 * @param $url
 * @param string $method
 * @param array $requestData
 * @return mixed
 */
function curlRequest($url,$method='get',$requestData=[])
{
    try{
        $curlHandle = curl_init();
        curl_setopt($curlHandle,CURLOPT_URL,$url);
        curl_setopt($curlHandle,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curlHandle,CURLOPT_RETURNTRANSFER,true);
        if($method=='post'){
            curl_setopt($curlHandle, CURLOPT_POST, true);
            curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $requestData);
        }
        $response = curl_exec($curlHandle);
        curl_close($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)
{
    try{
        //Assemble curl
        $allCurlHandle = [];
        $curlNum=0;
        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);
            if($method[$curlNum]=='post'){
                curl_setopt($allCurlHandle[$key] , CURLOPT_POST, true);
                curl_setopt($allCurlHandle[$key] , CURLOPT_POSTFIELDS, $requestData[$curlNum]);
            }
            $curlNum++;
        }
        // Create batch cURL handle
        $mh = curl_multi_init();
        // Add handle

        foreach ($allCurlHandle as $value){
            curl_multi_add_handle($mh,$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
//var_dump(curlRequest(buildJiJinUrl($urlOrderBy['all'],$urlZf['zzf'],10,10)));
$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
var_dump($info);

Tags: curl PHP Windows

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