【问题标题】:is there any better way to use Youtube PHP API有没有更好的方法来使用 Youtube PHP API
【发布时间】:2012-12-05 08:41:06
【问题描述】:

我正在使用 youtube php Zend API 库。

在此 API 中,我首先发送请求以获取临时/确认代码。

然后是获取访问令牌的请求。

在此之后我想获取用户信息然后另一个请求向

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

对于当前用户它返回带有userId的url

最后我从那个 xml 格式的 url 获取用户信息。

我受够了这么多请求,也需要很长时间。

有没有其他方法可以通过减少curl/ajax 请求的数量来获得这些东西。

【问题讨论】:

  • 是不是需要查询多个用户,耗时太长?
  • 是的,我已经提到过这个问题

标签: php ajax curl youtube youtube-api


【解决方案1】:

您可以使用curl_multi_* 为不同的用户并行请求。它不会加快每个用户的流程,但由于您可以并行处理 10-30 个或更多请求,它会加快整个交易的速度。

唯一的麻烦是您需要为每个请求单独的 cookie 文件。下面是开始使用的示例代码:

$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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 2021-05-07
    • 2019-12-08
    • 2013-01-01
    • 1970-01-01
    • 2015-04-07
    相关资源
    最近更新 更多