开放的编程资料库

当前位置:我爱分享网 > PHP教程 > 正文

使用 PHP 创建 Digg URL

Digg 最近推出了一项有趣的新功能,允许用户创建 Tiny Digg URL,这些 URL 在顶部显示 Digg 横幅,允许轻松访问从页面为文章投票。虽然我喜欢偶尔访问 Digg,但我更愿意远程获取 URL。以下是使用 PHP 执行此操作的方法。

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

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

XML 响应

<?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 是一个美丽的东西,不是吗?您还可以请求 JSON 响应。

喜欢这篇 Digg 文章吗?我想你可以 Digg 它!或者您可以查看我在 DiggNation 上的精选时间!

未经允许不得转载:我爱分享网 » 使用 PHP 创建 Digg URL

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏