次のように、API サーバーの http リクエストを curl と並行して実行できます。
//create the multiple cURL handle
$mh = curl_multi_init();
// gather curl handlers into an array for later use and add them to the multi request
$requests = array();
foreach ($feed as $url2) {
$url=$url2['url'];
$title=$url2['name'];
// strpos can return 0 which will evaluate to false even if the needle is in fact in the beginning of the haystack
if (strpos($url, $key) !== false || strpos($title,$key) !== false) {
$ch = curl_init("http://api.site.com/web?uri=".$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_multi_add_handle($mh, $ch);
$requests[] = $ch;
}
}
// execute the requests
$running=null;
do {
// exec the multy request, update the $running variable
while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mh, $running));
// break the loop if we are done
if (!$running) {
break;
}
// block until something interesting happens on a request
while (($res = curl_multi_select($mh)) === 0);
// if its an error, do something about it!
if ($res === false) {
// handle select errors here!
break;
}
// repeat forever (or until we break out)
} while (true);
// loop trough the results
foreach ($requests as $ch) {
// load resposne
$xml = simplexml_load_string(curl_multi_getcontent($ch));
// do your original processing
echo "<li>".$xml->filter->filterResponse->filteredText."</li>";
// clean up curl handler
curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh);
実際のデータなしでこれをテストすることはできませんが(構文は別として)、アイデアはわかります。の使用法はstrpos()
私には奇妙に見えます。針が干し草の山の先頭にあるが、false に評価さ0
れた場合、reutnr (数字のゼロ)になる可能性があります。これが必要かどうかはわかりません。0 == true