PHP_api接口教程(11)
发布时间:2021-06-06
发布时间:2021-06-06
}
// 关闭各个句柄
curl_multi_remove_handle($mh, $ch1); curl_multi_remove_handle($mh, $ch2); curl_multi_close($mh);
这里要做的就是打开多个 cURL 句柄并指派给一个批处理句柄。然后你就只需在一个 while 循环里等它执行完毕。
这个示例中有两个主要循环。第一个 do-while 循环重复调用 curl_multi_exec() 。这个函 数是无隔断(non-blocking)的,但会尽可能少地执行。它返回一个状态值,只要这个值等于常 量 CURLM_CALL_MULTI_PERFORM ,就代表还有一些刻不容缓的工作要做(例如,把对应 URL 的 http 头信息发送出去)。也就是说,我们需要不断调用该函数,直到返回值发生改变。
而接下来的 while 循环,只在 $active 变量为 true 时继续。这一变量之前作为第二个参 数传给了 curl_multi_exec() ,代表只要批处理句柄中是否还有活动连接。接着,我们调用 curl_multi_select() ,在活动连接(例如接受服务器响应)出现之前,它都是被“屏蔽”的。这个 函数成功执行后,我们又会进入另一个 do-while 循环,继续下一条 URL
<?php
/*************************************************
Snoopy - the PHP net client
Author: Monte Ohrt <monte@>
Copyright (c): 1999-2000 ispi, all rights reserved Version: 1.01
* This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. *
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * Lesser General Public License for more details. *
* You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software
See the GNU