Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any better way to use Youtube PHP API

I am using youtube php Zend API Library.

In this API first I send request to get the temporary/confirmation code.

Then an request to get the access token.

After this I want to fetch the user information then another request makes to

https://gdata.youtube.com/feeds/api/users/default  

for current user It returns the url with userId

Then finally I get the user Information from that url which is in xml form.

I am fed up by these so many requests it takes much time as well.

Is there another way to get these thing by reducing the number of curl/ajax requests.

like image 705
Muhammad Usman Avatar asked Nov 13 '22 15:11

Muhammad Usman


1 Answers

You can use curl_multi_* to do requests for different users in parallel. It won't speed up the process for every single user, but since you can do 10-30 or more requests in parallel, it will speed up the whole deal.

The only complication is that you will need separate cookie file for every request. Here's sample code to get your started:

$chs = array();
$cmh = curl_multi_init();
for ($t = 0; $t < $tc; $t++)
{
    $chs[$t] = curl_init();
    // set $chs[$t] options
    curl_multi_add_handle($cmh, $chs[$t]);
}

$running=null;
do {
    curl_multi_exec($cmh, $running);
} while ($running > 0);

for ($t = 0; $t < $tc; $t++)
{
    $contents[$t] = curl_multi_getcontent($chs[$t]);
    // work with $contencts[$t]
    curl_multi_remove_handle($cmh, $chs[$t]);
    curl_close($chs[$t]);
}
curl_multi_close($cmh);
like image 65
Ranty Avatar answered Nov 15 '22 06:11

Ranty