关于php curl异步并发请求http

先来看下同步的代码以及请求时间。

$start_time=date("h:i:sa");
for ($i=0; $i <100 ; $i++) { 
	$urls[]="http://www.downxia.com/downinfo/2315".$i.".html";
	GetTitle(geturl("http://www.downxia.com/downinfo/2315".$i.".html"));
}
function geturl($url){
       
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        
        $output = curl_exec($ch);
        curl_close($ch);

        return $output;
}
function GetTitle($output){

	preg_match('/<title>.*<\\/title>/i',$output,$matches);
	var_dump($matches[0]);
}
$end_time=date("h:i:sa");
echo '开始时间是:'.$start_time;
echo '结束时间是:'.$end_time;

关于php curl异步并发请求http

最下面可以看到时间花了27秒。

接下来看下php curl 异步并发请求http的代码以及花费时间。

$start_time=date("h:i:sa");

$urls=[];
for ($i=0; $i <100 ; $i++) { 
	$urls[]="http://www.downxia.com/downinfo/2315".$i.".html";
}
var_dump($urls);
// GetTitle('klasjdkla<title>313asds12</title>');

rolling_curl($urls,'GetTitle');

function GetTitle($output){

	preg_match('/<title>.*<\\/title>/i',$output,$matches);
	var_dump($matches[0]);
}
$end_time=date("h:i:sa");

echo '开始时间是:'.$start_time;
echo '结束时间是:'.$end_time;

function rolling_curl($urls, $callback, $custom_options = null)
{//多个url访问

    // make sure the rolling window isn't greater than the # of urls
    $rolling_window = 5;
    $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window;

    $master   = curl_multi_init();
    $curl_arr = array();

    // add additional curl options here
    $std_options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_MAXREDIRS      => 5
        );
    $options = ($custom_options) ? ($std_options + $custom_options) : $std_options;

    // start the first batch of requests
    for ($i = 0; $i < $rolling_window; $i++) {
        $ch                   = curl_init();
        $options[CURLOPT_URL] = $urls[$i];
        curl_setopt_array($ch, $options);
        curl_multi_add_handle($master, $ch);
    }

    do {
        while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);
        if ($execrun != CURLM_OK) {
            break;
        }

        // a request was just completed -- find out which one
        while ($done = curl_multi_info_read($master)) {
            $info = curl_getinfo($done['handle']);
            if ($info['http_code'] == 200) {
                $output = curl_multi_getcontent($done['handle']);

                // request successful.  process output using the callback function.
                $callback($output);

                // start a new request (it's important to do this before removing the old one)
                $ch                   = curl_init();
                $options[CURLOPT_URL] = $urls[$i++]; // increment i
                curl_setopt_array($ch, $options);
                curl_multi_add_handle($master, $ch);

                // remove the curl handle that just completed
                curl_multi_remove_handle($master, $done['handle']);
            } else {
                // request failed.  add error handling.
            }
        }
    } while ($running);

    curl_multi_close($master);
    return true;
}

关于php curl异步并发请求http

才花了3秒?实际上我感觉应该是花了5秒,因为启动比同步要慢,开始的时候卡了2秒。

http请求效率,毋庸置疑是异步远远高于同步。

核心请求代码如下:(这是老外写的,有点小问题,最后的提示undefined offset)

function rolling_curl($urls, $callback, $custom_options = null)
{//多个url访问

    // make sure the rolling window isn't greater than the # of urls
    $rolling_window = 5;
    $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window;

    $master   = curl_multi_init();
    $curl_arr = array();

    // add additional curl options here
    $std_options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_MAXREDIRS      => 5
        );
    $options = ($custom_options) ? ($std_options + $custom_options) : $std_options;

    // start the first batch of requests
    for ($i = 0; $i < $rolling_window; $i++) {
        $ch                   = curl_init();
        $options[CURLOPT_URL] = $urls[$i];
        curl_setopt_array($ch, $options);
        curl_multi_add_handle($master, $ch);
    }

    do {
        while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);
        if ($execrun != CURLM_OK) {
            break;
        }

        // a request was just completed -- find out which one
        while ($done = curl_multi_info_read($master)) {
            $info = curl_getinfo($done['handle']);
            if ($info['http_code'] == 200) {
                $output = curl_multi_getcontent($done['handle']);

                // request successful.  process output using the callback function.
                $callback($output);

                // start a new request (it's important to do this before removing the old one)
                $ch                   = curl_init();
                $options[CURLOPT_URL] = $urls[$i++]; // increment i
                curl_setopt_array($ch, $options);
                curl_multi_add_handle($master, $ch);

                // remove the curl handle that just completed
                curl_multi_remove_handle($master, $done['handle']);
            } else {
                // request failed.  add error handling.
            }
        }
    } while ($running);

    curl_multi_close($master);
    return true;
}

修改一下。只要在新增url的时候加个判断就好了。// 当$i等于$urls数组大小时不用再增加了。

function rolling_curl($urls, $callback, $custom_options = null)
{//多个url访问

    // make sure the rolling window isn't greater than the # of urls
    $rolling_window = 5;
    $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window;

    $master   = curl_multi_init();
    $curl_arr = array();

    // add additional curl options here
    $std_options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_MAXREDIRS      => 5
        );
    $options = ($custom_options) ? ($std_options + $custom_options) : $std_options;

    // start the first batch of requests
    for ($i = 0; $i < $rolling_window; $i++) {
        $ch                   = curl_init();
        $options[CURLOPT_URL] = $urls[$i];
        curl_setopt_array($ch, $options);
        curl_multi_add_handle($master, $ch);
    }

    do {
        while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);
        if ($execrun != CURLM_OK) {
            break;
        }

        // a request was just completed -- find out which one
        while ($done = curl_multi_info_read($master)) {
            $info = curl_getinfo($done['handle']);
            if ($info['http_code'] == 200) {
                $output = curl_multi_getcontent($done['handle']);

                // request successful.  process output using the callback function.
                $callback($output);

                // start a new request (it's important to do this before removing the old one)
                // 当$i等于$urls数组大小时不用再增加了
                if($i<sizeof($urls)){
                    $ch                   = curl_init();
                    $options[CURLOPT_URL] = $urls[$i++]; // increment i
                    curl_setopt_array($ch, $options);
                    curl_multi_add_handle($master, $ch);
                }
                // remove the curl handle that just completed
                curl_multi_remove_handle($master, $done['handle']);
            } else {
                // request failed.  add error handling.
            }
        }
    } while ($running);

    curl_multi_close($master);
    return true;
}

以上,结束。记录一下,以免自己忘记。

关于关于php curl异步并发请求http的文章就分享到这,如果对你有帮助欢迎继续关注我们哦

本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/44696.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
php学习php学习订阅用户
上一篇 2022年6月27日 00:31
下一篇 2022年6月27日 00:31

相关推荐

  • php的成员方法(函数)

    一个需求 当我们需要让对象完成某个任务(比如计算,比如上传下载文件,分页等等), 就需要在类中定义函数, 这时我们就将这样的函数称为 成员方法. 举例说明: 代码:

    2018年4月9日
    0212
  • PHP8中的新函数:array_key_first()的新应用方法。

    随着PHP8的发布,新函数也随之而来。其中,array_key_first()是在PHP7.3中已经出现,但其却在PHP8中得到了新的应用方法,让我们一起来了解一下。array_key_first()的定义首先,让我们来看看array_key_first()的定义…

    2023年5月21日
    00
  • PHP8.0中的自动加载库:Composer

    作为一门流行的服务器端脚本语言,PHP已经发展了数十年。在这个过程中,它积累了大量的社区资源和第三方库,这些资源和库可以方便地应用于各种项目中。当然,如果要在PHP项目中重复使用这些库,解决依赖问题是至关…

    2023年5月19日
    00
  • PHP中的水平扩展。

    PHP是一种广泛应用于Web开发的脚本语言,它的流行度和应用范围都在不断扩大。但在PHP的应用中,我们有时候会遇到一些性能问题,比如PHP编写的应用程序在高并发下响应速度缓慢,甚至会出现崩溃的情况。为了解决这些…

    2023年5月28日
    02
  • PHP中使用Redis实现分布式锁智能切换。

    分布式系统中,由于多个节点同时对同一资源进行操作,容易出现并发冲突的问题。为了解决这个问题,我们通常使用分布式锁来控制对共享资源的访问。Redis是一种高效的分布式缓存,可以用来实现分布式锁。本文将介绍如…

    2023年5月23日
    01
  • php in_array函数用法(实例)

    函数介绍:in_array() 函数用于搜索数组中是否存在指定的值。如果在数组中找到值则返回 TRUE,否则返回 FALSE。(推荐教程:php图文教程)函数语法:bool in_array(mixed $needle, array $haystack[, bool $strict …

    2022年6月14日
    0130
  • 说说Linux curl命令详解 用法指南。

    本文详解Linux curl命令的用法,包括基本语法、常用选项和实例。 Linux curl命令详解 用法指南 curl是一个利用URL规则在命令行下工作的文件传输工具,它支持很多协议,包括HTTP、HTTPS、FTP等,它的基本功能是从远…

    2024年7月25日
    00
  • PHP入门指南:SVN版本管理。

    作为一种常用的服务器端脚本语言,PHP凭借其开源、跨平台的优势,被广泛应用于Web开发领域。而在多人协作的开发中,版本控制是一个不可或缺的工具,它可以有效地管理源代码的修改与更新,避免因团队成员之间代码不…

    2023年5月23日
    04

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息