php - How to send multiple requests in one curl open connection

384

i have an array with approximately 45 k usernames in in i want to hit a url using curl that would give me a response pertaining to those usernames.The issue is i want to achieve it in less time.

$username=['123','456','789'....] //upto 45k entries
for($i=0;$i<sizeof($username);$i++)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://abc.com.pk/hxc/get_user_details.php?uname='.$username[$i]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
curl_setopt($ch, CURLOPT_HTTPGET, true);
$result = curl_exec($ch); 
curl_close($ch);
}

The above code depicts what i am doing right now but as usernames are in large numbers it takes alot of time to return all the responses.Is there any way i can achieve it in less time.

55

Answer

Solution:

Have a look at https://github.com/php-curl-class/php-curl-class , it speeds up our curl requests a lot.

It has multi-curl support enabled and it's very easy to use.

As for your question on the time, you can set the time out using

Curl::setTimeout($seconds)

Or in the case of MultiCurl

MultiCurl::setTimeout($seconds)

You can extend the timeout as much time as needed.

929

Answer

Solution:

You can use curl-multi-init and curl-multi-exec so that your requests are processed asynchronously.

People are also looking for solutions to the problem: php - Symfony FOSUserBundle : keep other forms' data after login

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.