尝试使用 curl_multi_init 获取页面信息。但是当我尝试使用 curl_multi_getcontent() 获取信息时 - 30 秒后的页面。下。我应该如何正确使用 curl_multi_getcontent()?谢谢
class Grab
{
public function getData()
{
$sessions = array('111', '222', '333', '444', '555');
$handle = curl_init();
foreach($sessions as $sId) {
$sessionId = $sId;
echo $sessionId.'<br/>';
$url = 'https://www.mypage.com?id='.$sessionId.'&test=1';
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_FRESH_CONNECT, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_FAILONERROR, true);
$sResponse = $this->curlExecWithMulti($handle);
}
}
function curlExecWithMulti($handle) {
// In real life this is a class variable.
static $multi = NULL;
// Create a multi if necessary.
if (empty($multi)) {
$multi = curl_multi_init();
}
// Add the handle to be processed.
curl_multi_add_handle($multi, $handle);
// Do all the processing.
$active = NULL;
do {
$ret = curl_multi_exec($multi, $active);
} while ($ret == CURLM_CALL_MULTI_PERFORM);
while ($active && $ret == CURLM_OK) {
if (curl_multi_select($multi) != -1) {
do {
$mrc = curl_multi_exec($multi, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
**$res = curl_multi_getcontent($handle); // - very slow**
$this->printData($res);
// Remove the handle from the multi processor.
curl_multi_remove_handle($multi, $handle);
return TRUE;
}
public function printData($res)
{
$oPayment = json_decode($res);
var_dump($oPayment);
var_dump($errorno);
echo '<br/>---------------------<br/>';
}
}
$grab = new Grab;
$grab->getData();