php curl 获取下载地址

声明

本分享纯属为了技术分享,禁止商用!!!禁止商用!!!禁止商用!!!
未经本人允许,如有发现,违者必究!!!

调用

// apk 只能通过数组传递
$urls=[
    'http://www.pipikun.com/url.php?url=aHR0cHM6Ly9hcGt0eGRsLnZpdm8uY29tLmNuL2FwcHN0b3JlL2RldmVsb3Blci9hcGsvMjAyMjA2MjgvMjAyMjA2MjgxNzIxNDgxM3o1aC5hcGs=',
];
$timer=count($urls);
ini_set('max_execution_time', $timer+2);
curl_getapk_multi($urls,$timer);

方法

function curl_getapk_multi($urls,$timer=1,$method='get') {
    $mh=curl_multi_init();
    foreach ($urls as $v) {
        $ch=curl_init();
        $opts=array(
            CURLOPT_URL => $v,
            CURLOPT_HEADER => 0,
            CURLOPT_CUSTOMREQUEST => strtoupper($method),
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => 0,
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_CONNECTTIMEOUT => $timer,
            CURLOPT_TIMEOUT => $timer,
            CURLOPT_FOLLOWLOCATION => 1,
        );
        curl_setopt_array($ch,$opts);
        curl_multi_add_handle($mh,$ch);
    }
    $active=null;
    $apks=array();
    static $ok=0;
    static $err=0;
    $t1=microtime(true);
    do {
        curl_multi_exec($mh,$active);
        while ($done=curl_multi_info_read($mh)) {
            $info=curl_getinfo($done['handle']);
            if (curl_errno($done['handle']) || $info['http_code'] != '200') {
                $url='';
                $err++;
            } else {
                $url=$info['url'];
                $ok++;
            }
            $apks[]=$url;
            curl_multi_remove_handle($mh,$done['handle']);
            curl_close($done['handle']);
        }
    } while ($active>0);
    $t2=microtime(true);
    curl_multi_close($mh);
    var_dump('count: '.count($urls));
    var_dump('ok: '.$ok);
    var_dump('err: '.$err);
    var_dump('timer: '.number_format($t2-$t1,10).' s');
    var_dump($apks);
}

结果示例


string(8) "count: 1"
string(5) "ok: 1"
string(6) "err: 0"
string(21) "timer: 0.9853360653 s"
array(1) {
  [0]=>
  string(43) "https://apktxdl.vivo.com.cn/appstore/developer/apk/20220628/2022062817214813z5h.apk"
}
posted @ 2022-04-10 03:14  苦逼的后端  阅读(1998)  评论(0编辑  收藏  举报