/ / Zu viele Sockets von cURL Multi geöffnet - PHP, Curl, Curl-Multi, PHP-Socket

Zu viele Sockets öffnen durch cURL multi-php, curl, curl-multi, php-socket

Ich verwende cURL multi, um Daten von einigen Websites abzurufen. Mit Code:

function getURL($ids)
{
global $mh;
$curl = array();
$response = array();
$n = count($ids);
for($i = 0; $i < $n; $i++) {
$id = $ids[$i];
$url = "http://www.domain.com/?id=".$id;

// Init cURL
$curl[$i] = curl_init($url);
curl_setopt($curl[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl[$i], CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl[$i], CURLOPT_USERAGENT, "Googlebot/2.1 (http://www.googlebot.com/bot.html)");
//curl_setopt($curl[$i], CURLOPT_FORBID_REUSE, true);
//curl_setopt($curl[$i], CURLOPT_HEADER, false);
curl_setopt($curl[$i], CURLOPT_HTTPHEADER, array(
"Connection: Keep-Alive",
"Keep-Alive: 300"
));

// Set to multi cURL
curl_multi_add_handle($mh, $curl[$i]);
}

// Execute
do {
curl_multi_exec($mh, $flag);
} while ($flag > 0);

// Get response
for($i = 1; $i < $n; $i++) {
// Get data
$id = $ids[$i];
$response[] = array(
"id" => $id,
"data" => curl_multi_getcontent($curl[$i])
);

// Remove handle
//curl_multi_remove_handle($mh, $curl[$i]);
}

// Reponse
return $response;
}

Aber ich habe Problem ist CURL offen zu viele SocketsVerbindung zum Webserver herstellen. Bei jeder Verbindung wird mit cURL ein neuer Socket zum Webserver erstellt. Ich möchte, dass die aktuelle Verbindung für die nächste Verbindung aktiv bleibt. Ich möchte nicht, dass 100 URL, dann muss CURL 100 Sockets erstellen, um zu behandeln :(

Bitte hilf mir. Vielen Dank !

Antworten:

1 für die Antwort № 1

Öffnen Sie also nicht so viele Sockets. Ändern Sie Ihren Code so, dass nur X - Sockets geöffnet werden, und verwenden Sie diese Sockets dann wiederholt, bis alle Ihre Sockets geöffnet sind $ids verbraucht wurden. Das oder übergeben Sie der Funktion zunächst weniger $ ids.