使用curl模拟不同ip多线程采集函数

使用curl模拟不同ip多线程采集函数

function strCutByStr(&$str, $findStart, $findEnd = false, $encoding = 'utf-8'){  

        if(is_array($findStart)){  
            if(count($findStart) === count($findEnd)){  
                foreach($findStart as $k => $v){  
                    if(($result = strCutByStr($str, $v, $findEnd[$k], $encoding)) !== false){  
                        return $result;  
                    }  
                }  
                return false;  
            }else{  
                return false;  
            }  
        }  
           
        if(($start = mb_strpos($str, $findStart, 0, $encoding)) === false){  
            return false;  
        }  
           
        $start += mb_strlen($findStart, $encoding);  
           
        if($findEnd === false){  
            return mb_substr($str, $start, NULL, $encoding);  
        }  
           
        if(($length = mb_strpos($str, $findEnd, $start, $encoding)) === false){  
            return false;  
        }  
           
        return mb_substr($str, $start, $length - $start, $encoding);  
    }  
function curl_multi($urls) {  
    if (!is_array($urls) or count($urls) == 0) {  
        return false;  
    }   
    $num=count($urls);  
    $curl = $curl2 = $text = array();  
    $handle = curl_multi_init();  


    function createCh($url) {  
        $ch = curl_init();  
$ip = rand(10,30).".".rand(10,50).".".rand(1,253).".".rand(1,250);
$headers['CLIENT-IP'] = $ip;  
$headers['X-FORWARDED-FOR'] = $ip; 
 
$headerArr = array();  
foreach( $headers as $n => $v ) {  
    $headerArr[] = $n .':' . $v;   
}


        curl_setopt ($ch, CURLOPT_HTTPHEADER, $headerArr); 
        curl_setopt ($ch, CURLOPT_URL, $url);  
        curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0");//设置头部  
        curl_setopt ($ch, CURLOPT_REFERER, $url); //设置来源  
        curl_setopt ($ch, CURLOPT_ENCODING, "gzip"); // 编码压缩  
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);//是否采集301、302之后的页面  
        curl_setopt ($ch, CURLOPT_MAXREDIRS, 5);//查找次数,防止查找太深  
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查  
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在         
        curl_setopt ($ch, CURLOPT_TIMEOUT, 20);  
 //       curl_setopt ($ch, CURLOPT_HEADER, 0);
        return $ch;  
    }  
    foreach($urls as $k=>$v){  
        $url=$urls[$k];  
        $curl[$k] = createCh($url);  
        curl_multi_add_handle ($handle,$curl[$k]);  
    }  
    $active = null;  
    do {  
        $mrc = curl_multi_exec($handle, $active);  
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);  
  
    while ($active && $mrc == CURLM_OK) {  
        if (curl_multi_select($handle) != -1) {  
            usleep(100);  
        }  
        do {  
            $mrc = curl_multi_exec($handle, $active);  
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);  
    }   
  
    foreach ($curl as $k => $v) {  
        if (curl_error($curl[$k]) == "") {  


            $text[$k] = (string) curl_multi_getcontent($curl[$k]);  
 $text[$k] = strCutByStr($text[$k], '<title>', '</title>');
$file="0501.txt"; 
$data=fopen($file,"a"); 
fwrite($data," $text[$k] \r\n"); 
fclose($data); 


        }  
        curl_multi_remove_handle($handle, $curl[$k]);  
        curl_close($curl[$k]);  
    }   
    curl_multi_close($handle);  
    return $text;  
}   
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 PHP 模拟多线程发送 curl 请求,可以使用多进程或多线程的方式来实现。这里介绍两种方式: 1.使用多进程 使用 PHP 的 pcntl_fork() 函数可以创建一个子进程,让子进程去执行 curl 请求,从而达到模拟多线程的效果。代码示例: ``` <?php $urls = [ 'http://www.example.com', 'http://www.example.net', 'http://www.example.org', ]; foreach ($urls as $url) { $pid = pcntl_fork(); // 创建子进程 if ($pid == -1) { die('fork failed'); } elseif ($pid) { // 父进程,继续执行 } else { // 子进程,执行 curl 请求 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); curl_close($ch); echo $output; // 处理 curl 响应 exit(); // 子进程退出 } } // 等待所有子进程完成 while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child $status completed\n"; } ``` 上述代码将创建多个子进程,每个子进程执行一个 curl 请求,完成后退出。父进程则继续等待所有子进程完成后结束。 2.使用多线程 使用 PHP 的 pthreads 扩展可以实现多线程。代码示例: ``` <?php class CurlThread extends Thread { public $url; public $result; public function __construct($url) { $this->url = $url; } public function run() { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $this->result = curl_exec($ch); curl_close($ch); } } $urls = [ 'http://www.example.com', 'http://www.example.net', 'http://www.example.org', ]; $threads = []; foreach ($urls as $url) { $thread = new CurlThread($url); $threads[] = $thread; $thread->start(); // 启动线程 } foreach ($threads as $thread) { $thread->join(); // 等待线程完成 echo $thread->result; // 处理线程的 curl 响应 } ``` 上述代码将创建多个线程,每个线程执行一个 curl 请求,完成后处理响应。线程的执行顺序不确定,但是由于每个线程都是独立的,所以可以实现真正的多线程效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值