<?php
/**
 * Created by PhpStorm.
 * User: zhezhao
 * Date: 2016/10/20
 * Time: 10:51
 */
$url_arr = array();
for ($i=0;$i<1000;$i++){
    $url_arr[] = "http://www.baidu.com/s?wd=".$i;
}
echo "start:".date("Y-m-d H:i:s").PHP_EOL;
$start_time = microtime(true);
$workers = array();
for ($i=0;$i<200;$i++){
    $process = new swoole_process('getContents',true);
    $process->start();
    $process->write($i);
    $workers[] = $process;
}
//主进程数据结果
foreach ($workers as $process){
    echo $process->read();
    echo PHP_EOL;
}
echo "end:".date("Y-m-d H:i:s").PHP_EOL;
$end_time = microtime(true);
echo $end_time - $start_time.PHP_EOL;
function getContents(swoole_process $worker){
    $i = $worker->read();
    global $url_arr;
    $n = 5;
    for($j=$i*$n;$j<=$i*$n+($n-1);$j++)
    {
    $res = execCurl($url_arr[$j],$j);
        echo $res.PHP_EOL;
    }
/*
    $res1 = execCurl($url_arr[($i*5)],$i*5);
    $res2 = execCurl($url_arr[($i*5+1)],$i*5+1);
    $res3 = execCurl($url_arr[($i*5+2)],$i*5+2);
    $res4 = execCurl($url_arr[($i*5+3)],$i*5+3);
    $res5 = execCurl($url_arr[($i*5+4)],$i*5+4);
    echo $res1.PHP_EOL.$res2.PHP_EOL.$res3.PHP_EOL.$res4.PHP_EOL.$res5;
*/
}
function execCurl($url,$num){
    $result = get_data($url); 
    file_put_contents('sw/'.$num.'.html',$result);
    return "handle ".$url." finished";
}

function get_data($url){

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    //curl_setopt($ch, CURLOPT_POST, true );
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $tmpInfo=null;
    $tmpInfo = curl_exec($ch);
    if($tmpInfo){
        curl_close($ch);
        return $tmpInfo;
    } else { 
        $error = curl_errno($ch);
        curl_close($ch);
        //throw new WxPayException("curl出错,错误码:$error");
    }
   return $tmpInfo;
}

 

posted on 2017-09-26 16:00  宇宙神奇  阅读(433)  评论(0编辑  收藏  举报