I was able to achieve parallel execution of curl requests using curl_multi. But i'm having issue with the response. Even though the requests are getting executed parallel the response is received only after all the URL's are executed.
my requirement is to return the response as soon each request is executed.
Can someone point me the solution.
<?php
$price = urlencode("select * from html where url=\"http://www.flipkart.com/karwar- cuisine-traditional-recipes-small-coastal-town-karnataka/p/itmdfrx4z4erg9ya?pid=9788179917558\" and xpath=\"//span[contains(@class,'pprice')]/text()\"");
$desc = urlencode("select * from html where url=\"http://www.flipkart.com/karwar-cuisine-traditional-recipes-small-coastal-town-karnataka/p/itmdfrx4z4erg9ya?pid=9788179917558\" and xpath=\"//div[@class='item_desc_text line']\"");
$author = urlencode("select * from html where url=\"http://www.flipkart.com/karwar-cuisine-traditional-recipes-small-coastal-town-karnataka/p/itmdfrx4z4erg9ya?pid=9788179917558\" and xpath=\"//div[contains(@class,'secondary-info')]//a/text()\"");
$title = urlencode("select * from html where url=\"http://www.flipkart.com/karwar-cuisine-traditional-recipes-small-coastal-town-karnataka/p/itmdfrx4z4erg9ya?pid=9788179917558\" and xpath=\"//div[contains(@class,'mprod-summary-title')]//h1/text()\"");
$url1 = "https://query.yahooapis.com/v1/public/yql?q={$price}&format=json";
$url2 = "https://query.yahooapis.com/v1/public/yql?q={$desc}&format=json";
$url3 = "https://query.yahooapis.com/v1/public/yql?q={$author}&format=json";
$url4 = "https://query.yahooapis.com/v1/public/yql?q={$title}&format=json";
$nodes = array(
$url1,
$url2,
$url3,
$url4
);
$node_count = count($nodes);
$curl_arr = array();
$master = curl_multi_init();
for ($i = 0; $i < $node_count; $i++) {
$url = $nodes[$i];
$curl_arr[$i] = curl_init($url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($master, $curl_arr[$i]);
}
do {
curl_multi_exec($master, $running);
} while ($running > 0);
for ($i = 0; $i < $node_count; $i++) {
$results[] = curl_multi_getcontent($curl_arr[$i]);
}
print_r($results);
?>