如何使用cURL与PHP同时打开多个URL?
发布时间:2021-02-21 05:48:00  所属栏目:Linux  来源:互联网 
            导读:这是我现在的代码: $SQL = mysql_query(SELECT url FROM urls) or die(mysql_error()); //Query the urls tablewhile($resultSet = mysql_fetch_array($SQL)){ //Put all the urls into one variable //
                
                
                
            | 
                         这是我现在的代码: $SQL = mysql_query("SELECT url FROM urls") or die(mysql_error()); //Query the urls table
while($resultSet = mysql_fetch_array($SQL)){ //Put all the urls into one variable
                // Now for some cURL to run it.
            $ch = curl_init($resultSet['url']); //load the urls
            curl_setopt($ch,CURLOPT_TIMEOUT,2); //No need to wait for it to load. Execute it and go.
            curl_exec($ch); //Execute
            curl_close($ch); //Close it off 
        } //While loop 
 我对cURL来说比较新.通过比较新的,我的意思是这是我第一次使用cURL.目前它加载一个2秒钟,然后加载下一个2秒,然后下一个.但是,我想同时加载所有这些.我确信它是可能的,我只是不确定如何.如果有人能指出我正确的方向,我会很感激. 解决方法您以相同的方式设置每个cURL句柄,然后将它们添加到curl_multi_句柄.要查看的功能是curl_multi_ *功能 documented here.然而,根据我的经验,尝试一次加载太多的URL时出现问题(尽管我目前无法找到我的笔记),所以最后一次我使用curl_mutli_,我一次设置了一批5个URL.编辑:这是一个缩减版本的代码,我使用curl_multi_: 编辑:稍微重写和许多添加的评论,希望会有所帮助. // -- create all the individual cURL handles and set their options
$curl_handles = array();
foreach ($urls as $url) {
    $curl_handles[$url] = curl_init();
    curl_setopt($curl_handles[$url],CURLOPT_URL,$url);
    // set other curl options here
}
// -- start going through the cURL handles and running them
$curl_multi_handle = curl_multi_init();
$i = 0; // count where we are in the list so we can break up the runs into smaller blocks
$block = array(); // to accumulate the curl_handles for each group we'll run simultaneously
foreach ($curl_handles as $a_curl_handle) {
    $i++; // increment the position-counter
    // add the handle to the curl_multi_handle and to our tracking "block"
    curl_multi_add_handle($curl_multi_handle,$a_curl_handle);
    $block[] = $a_curl_handle;
    // -- check to see if we've got a "full block" to run or if we're at the end of out list of handles
    if (($i % BLOCK_SIZE == 0) or ($i == count($curl_handles))) {
        // -- run the block
        $running = NULL;
        do {
            // track the previous loop's number of handles still running so we can tell if it changes
            $running_before = $running;
            // run the block or check on the running block and get the number of sites still running in $running
            curl_multi_exec($curl_multi_handle,$running);
            // if the number of sites still running changed,print out a message with the number of sites that are still running.
            if ($running != $running_before) {
                echo("Waiting for $running sites to finish...n");
            }
        } while ($running > 0);
        // -- once the number still running is 0,curl_multi_ is done,so check the results
        foreach ($block as $handle) {
            // HTTP response code
            $code = curl_getinfo($handle,CURLINFO_HTTP_CODE);
            // cURL error number
            $curl_errno = curl_errno($handle);
            // cURL error message
            $curl_error = curl_error($handle);
            // output if there was an error
            if ($curl_error) {
                echo("    *** cURL error: ($curl_errno) $curl_errorn");
            }
            // remove the (used) handle from the curl_multi_handle
            curl_multi_remove_handle($curl_multi_handle,$handle);
        }
        // reset the block to empty,since we've run its curl_handles
        $block = array();
    }
}
// close the curl_multi_handle once we're done
curl_multi_close($curl_multi_handle); 
 考虑到你不需要任何东西从URL,你可能不需要很多的东西,但这是我如何将请求块块BLOCK_SIZE,等待每个块运行,然后继续前进,并抓住来自cURL的错误. (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
相关内容
- linux – 在ARM上构建mongo shell 3.2.x(armv7l / arm64 /
 - linux – NPTL和pthread令人困惑
 - linux – 在RPI上使用cec-client关闭电视
 - linux – Rsync失败,“文件太大”
 - linux – 如何在bash上使用filetype将文件排序到文件夹中(使
 - linux – sqlplus:加载共享库时出错:libsqlplus.so:无法
 - linux-device-driver – add_disk()在insmod上挂起
 - linux – rsync到NAS每次都会复制一切
 - 适用于团队的最佳轻量级Linux IRC服务器
 - debian – 基于“.deb”的Linux发行版是否支持Dell PERC H2
 
推荐文章
            站长推荐
            - LINUX教程:UFW使用指南:通用防火墙规则和命令
 - linux – bash / Makefile中双符号的意思是什么?
 - Getting over the dangers of rm command in Lin
 - linux – 如何在eclipse中输入unicode字符?
 - 如何从Linux中的用户空间中找到变量的物理地址?
 - linux – Perl DBI替代LongReadLen
 - 如何在不结束C程序的情况下结束ncurses?
 - linux – Systemd – 在ExecStopPost中检测服务是
 - 用于Python脚本的Desktop Launcher以错误的路径启
 - 如何在linux上创建虚拟CAN端口? (C )
 
热点阅读
            