gpt4 book ai didi

php - 理解 php curl_multi_exec

转载 作者:IT王子 更新时间:2023-10-29 01:14:02 25 4
gpt4 key购买 nike

我正在尝试理解 curl_multi_exec。我在这里复制了一部分手册示例。所以我想知道,它是如何工作的?我猜第一个循环发送 http 请求?但它随后是一个循环内的循环,使用带有看似未记录的标志的函数..

我想并行下载 +=70 个 url +=。

http://www.php.net/manual/en/function.curl-multi-exec.php

<?php
...
$active = null;
//execute the handles
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
...
?>

最佳答案

您可以浏览描述此示例的两篇文章。

PHP and curl_multi_exec

First, here's the high level. There are two outer loops. The first one is responsible for clearing out the curl buffer right now. The second one is responsible for waiting for more information, and then getting that information. This is an example of what is called blocking I/O. We block execution of the rest of the program until the network I/O is done. While this isn't the most preferable way in general to handle network I/O, it's really our only choice in single-threaded, synchronous PHP.

Doing curl_multi_exec the right way

First the $mrc variable and from the manual we learn that the response is a cURL code defined in the cURL Predefined Constants. In essence it is a regular response and as with any other PHP function curl_multi_exec is no different and only returns a response once it is finished. Which means there should be only ONE response. In a perfect world this single response is 0 (zero) or equal to the predefined constant CURLM_OK.

关于php - 理解 php curl_multi_exec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15559157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com