digg bt_使用PHP创建Digg URL

digg bt

Digg recently came out with a sweet new feature that allows users to create Tiny Digg URLs which show a Digg banner at the top allowing easy access to vote for the article from the page. While I love visiting Digg every once in a while, I'd rather grab the URL remotely. Here's how to do so using PHP.

Digg最近推出了一项甜美的新功能,该功能使用户可以创建Tiny Digg URL,该URL在顶部显示Digg标语,从而可以轻松地从页面上为文章进行投票。 虽然我喜欢不时访问Digg,但我想远程获取URL。 这是使用PHP的方法。

PHP (The PHP)


/* function that grabs the response from digg */
function get_digg_url($url,$app_key)
{
	$return_xml = file_get_contents('http://services.digg.com/url/short/create?type=xml&appkey='.urlencode($app_key).'&url='.urlencode($url));
	$digg_url = get_match('/short_url="(.*)"/isU',$return_xml);
	return $digg_url;
}

/* function that runs a regex to scrub for the url */
function get_match($regex,$content)
{
	preg_match($regex,$content,$matches);
	return $matches[1];
}

/* important! set a fake user agent */
ini_set('user_agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');

/* url i want the digg URL for, and my app key which is a URL */
$url = 'https://davidwalsh.name/penetrated-diggnation';
$app = 'https://davidwalsh.name';

/* get the digg URL! */
$digg_url = get_digg_url($url,$app);  //returns:  http://digg.com/u1DOk


Very quick and simple. You could also use PHP's cURL library if you wanted.

非常快速和简单。 如果需要,您也可以使用PHP的cURL库。

XML回应 (The XML Response)


<?xml version="1.0" encoding="UTF-8"?>
<shorturls count="1" offset="0" timestamp="1238884894" total="1">
	<shorturl link="https://davidwalsh.name/penetrated-diggnation" short_url="http://digg.com/u1DOk" view_count="0"/>
</shorturls>


XML is a beautiful thing, isn't it? You may also request a JSON response.

XML是一件漂亮的事情,不是吗? 您也可以请求JSON响应。

Like this Digg article? I suppose you could Digg it! Or you can check out the time I was featured on DiggNation!

喜欢这篇Digg文章吗? 我想你可以挖它! 或者,您也可以查看我在DiggNation上被推荐的时间!

翻译自: https://davidwalsh.name/create-digg-url-php

digg bt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值