如何使用 Services_Digg2 获取 URL 的 Digg 计数?

发布于 2024-08-17 09:23:31 字数 56 浏览 5 评论 0原文

目前还不清楚这个 API 是否能够通过 URL 进行搜索,通过 G 进行搜索,但没有取得太大成功。

It isn't clear if this API is even capable of searching by URL, searched through G, without much success.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2024-08-24 09:23:31

Services_Digg2 只是 PHP 中的一个客户端库,它将所有调用转发到 API,并且 API 确实支持按 url 搜索。您需要传递的是 url 的 md5 哈希值,而不是实际的 url。 PHP 有一个 md5 函数,您可以使用它来获取url 的 md5 哈希值。

然后,API 调用将针对 story.getAll,并且您将之前计算的 md5 哈希值作为参数 link_hash 传递。

http://services.digg.com/1.0/endpoint?method=story.getAll&link_hash=a23658a0828e2fb388b7c83f61e235e6

上面的哈希值适用于 URL http://www.nytimes.com /2010/01/05/health/05docs.html 并且来自 API 的响应是:

<stories timestamp="1262740374" total="1" offset="0" count="1"> 
 <story link="http://www.nytimes.com/2010/01/05/health/05docs.html" submit_date="1262729293" diggs="70" id="18288654" comments="6" href="http://digg.com/health/For_F_D_R_Sleuths_New_Focus_on_an_Odd_Spot" promote_date="1262739603" status="popular" media="news"> 
  <description>Look closely at Roosevelt’s portraits over his 12-year presidency. In his first two terms, there is a dark spot over his left eyebrow. It seems to grow and then mysteriously vanishes sometime around 1940, leaving a small scar.  </description> 
  <title>For F.D.R. Sleuths, New Focus on an Odd Spot</title> 
  <user name="leaprinces" registered="1227624484" profileviews="23186" fullname="Princess Leia" icon="http://digg.com/users/leaprinces/l.png" /> 
  <topic name="Health" short_name="health" /> 
  <container name="Lifestyle" short_name="lifestyle" /> 
  <thumbnail originalwidth="190" originalheight="126" contentType="image/jpeg" src="http://digg.com/health/For_F_D_R_Sleuths_New_Focus_on_an_Odd_Spot/t.jpg" width="80" height="80" /> 
  <shorturl short_url="http://digg.com/d31EjiI" view_count="192" /> 
 </story> 
</stories>

此响应中的 Story 元素有一个名为 diggs 的属性,这正是您所需要的。

要通过 PHP 库实现此目的,代码应如下所示:

$url = "...";

$api = new Services_Digg2;
$stories = $api->story->getAll(array('link_hash' => md5($url)))->stories;

foreach($stories as $story) {
    // I am not sure how to access the diggs attribute
    // Maybe this works. If not, just
    // var_dump the $story object and 
    // see how to access the digg count information.
    echo $story['diggs'];
}

Services_Digg2 is just a client library in PHP that forwards all calls to the API and the API does support searching by urls. What you need to pass is the md5 hash of the url instead of the actual url. PHP has a md5 function that you can use to get the md5 hash of a url.

The API call then would be for story.getAll and you pass the previously calculated md5 hash as a parameter link_hash.

http://services.digg.com/1.0/endpoint?method=story.getAll&link_hash=a23658a0828e2fb388b7c83f61e235e6

This hash above is for the URL http://www.nytimes.com/2010/01/05/health/05docs.html and the response from the API is:

<stories timestamp="1262740374" total="1" offset="0" count="1"> 
 <story link="http://www.nytimes.com/2010/01/05/health/05docs.html" submit_date="1262729293" diggs="70" id="18288654" comments="6" href="http://digg.com/health/For_F_D_R_Sleuths_New_Focus_on_an_Odd_Spot" promote_date="1262739603" status="popular" media="news"> 
  <description>Look closely at Roosevelt’s portraits over his 12-year presidency. In his first two terms, there is a dark spot over his left eyebrow. It seems to grow and then mysteriously vanishes sometime around 1940, leaving a small scar.  </description> 
  <title>For F.D.R. Sleuths, New Focus on an Odd Spot</title> 
  <user name="leaprinces" registered="1227624484" profileviews="23186" fullname="Princess Leia" icon="http://digg.com/users/leaprinces/l.png" /> 
  <topic name="Health" short_name="health" /> 
  <container name="Lifestyle" short_name="lifestyle" /> 
  <thumbnail originalwidth="190" originalheight="126" contentType="image/jpeg" src="http://digg.com/health/For_F_D_R_Sleuths_New_Focus_on_an_Odd_Spot/t.jpg" width="80" height="80" /> 
  <shorturl short_url="http://digg.com/d31EjiI" view_count="192" /> 
 </story> 
</stories>

The story element in this response has an attribute called diggs which is what you need.

To get this through the PHP library, the code should look something like:

$url = "...";

$api = new Services_Digg2;
$stories = $api->story->getAll(array('link_hash' => md5($url)))->stories;

foreach($stories as $story) {
    // I am not sure how to access the diggs attribute
    // Maybe this works. If not, just
    // var_dump the $story object and 
    // see how to access the digg count information.
    echo $story['diggs'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文