今日私のcurl_multi()
コードが実際にシリアルで動作しているというヒントがありました。私の希望がcURL要求を並列化することが目的だったときです。私のcurl_multi()の使用はまだシリアルですか?もしそうなら、私はどのように並列化できますか?
私のコードはまだシリアルですか?もしそうなら、私はどのように並列化できますか?
ここに関連するコードは次のとおり
/**
* Returns the cURL responses given multiple target URLs
* @param array $targetUrls Array of target URLs for cURL
*
* @return array cURL Responses
*/
private function getCurlMultiResponses($targetUrls)
{
// Cache the count
$count = count($targetUrls);
// Create the multiple cURL handles
for($i = 0; $i < $count; $i++) {
$ch[$i] = curl_init($targetUrls[$i]);
curl_setopt($ch[$i], CURLOPT_POST, FALSE);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, TRUE);
}
// Initialize the multiple cURL handle
$mh = curl_multi_init();
// Add the handles to the curl_multi handle
for($i = 0; $i < $count; $i++) {
curl_multi_add_handle($mh, $ch[$i]);
}
$running = null;
// Execute the handles
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
$responses = array();
// Remove the handles and return the response
for($i = 0; $i < $count; $i++) {
curl_multi_remove_handle($mh, $ch[$i]);
$responses[$i] = curl_multi_getcontent($ch[$i]);
}
// Close the multiple cURL handle
curl_multi_close($mh);
return $responses;
}
また、http拡張子(http://php.net/manual/en/book.http.php)を試してください。それには並列処理のための要求プールがあります。また、cURLとは異なり、中間APIではありません。 (curlはHTTPクライアントとしてだけではなく、SSH、FTP、IMAPなどの間でサポートしています) – mario
@mario:http拡張機能が並列処理を処理できることは分かりませんでした。私はいくつかの点を見ていく必要があります(私の気持ちは低いです)が、心に留めておく価値があります!ありがとう。 –