我构建了一个无需外部库即可工作的小型解决方案。它本质上可以归结为:
- 从“注册应用程序”仪表板获取 API 密钥或从 oauth API 获取 oauth 令牌
- 向以下人员提出请求
/v3/shorten
带有您的令牌和 URL 的 API 端点
第一步:获取token
您只需执行一次此操作。 Bitly 的文档列出了一些示例,您可以使用curl https://curl.haxx.se/,我认为这是最简单的方法。你也可以用 PHP 来做 https://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php。只需发出此 POST 请求(采取从这里 https://dev.bitly.com/authentication.html#basicauth):
curl -u "username:password" -X POST "https://api-ssl.bitly.com/oauth/access_token"
结果是这样的:
e663e30818201d28dd07803e57333bed4f15803a
那是你的令牌。
第二步:提出请求
将令牌和 url 编码的 URL 插入到 HTTP 请求中/v3/shorten https://dev.bitly.com/links.html#v3_shorten端点:
<?php
$ebay_url = "http://rover.ebay.com/rover/1/711-53200-19255-0/1?icep_ff3=10&pub=5575165347&toolid=10001&campid=5337851510&customid=&icep_uq=ocz%20vertex&icep_sellerId=&icep_ex_kw=&icep_sortBy=15&icep_catId=&icep_minPrice=&icep_maxPrice=&ipn=psmain&icep_vectorid=229466&kwid=902099&mtid=824&kw=lg"
$token = "e663e30818201d28dd07803e57333bed4f15803a"; // change this
$endpoint = "https://api-ssl.bitly.com/v3/shorten?access_token=".$token."&longUrl=".urlencode($ebay_url)."";
$result = file_get_contents($endpoint);
$json = json_decode($result, true);
$short_url = $json["data"]["url"];
echo $short_url;
?>
API调用的结果是JSON,需要解码。文档中有一个示例。此代码示例未考虑 API 超时或可能发生的其他错误(例如令牌过期,这是目前不是问题 https://stackoverflow.com/questions/38116492/bitly-generic-access-token-expiration-time).
完整代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="css/screen.css">
<style type="text/css">
body{
margin:0px;
font-size:0.7em;
font-family:trebuchet ms;
color:#222;
}
#mainContainer{
width:840px;
margin:5px;
}
table,tr,td{
vertical-align:top;
}
.textInput{
width:300px;
}
html{
margin:0px;
}
.formButton{
width:75px;
}
textarea,input,select{
font-family:helvetica;
}
i{
font-size:0.9em;
}
</style>
<script language="Javascript">
<!--
var copytoclip=1
function HighlightAll(theField) {
var tempval=eval("document."+theField)
tempval.focus()
tempval.select()
if (document.all&©toclip==1){
therange=tempval.createTextRange()
therange.execCommand("Copy")
window.status="Contents highlighted and copied to clipboard!"
setTimeout("window.status=''",1800)
}
}
//-->
</script>
</head>
<table width="80%" height="100px" align="center" style="margin:0 auto"><tr><td align="center">
<h2>Link Generator Online</h2>
</td><tr></table>
<table width="80%" align="center" style="margin:0 auto"><tr><td align="center">
</div>
</td><td valign="top">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<br>
Insert keywords:<br>
<input type=text style="font-size: 13px; font-family: tahoma,arial; font-weight: bold; color: #000000; BORDER: #555 1px solid ; BACKGROUND-COLOR: #FFF" input name="url" size="20">
<br>
<br>
<input type="SUBMIT" name="submit" VALUE="Submit">
</form>
</td></tr></table>
<?php
if(isset($_POST['submit'])){
$url = $_POST['url'];
$name = array($url);
foreach ($name as $name) {
if (preg_match("/^[\.\<\[#`]/",$url)) {
echo "<br><center><font color=\"red\">Invalid Characters.</center>";
Die();
}
if (!strlen($url)) {
echo "<br><center><font color=\"red\">Empty Field.</center>";
Die();
}
if (strlen($url) > 100) {
echo "<br><center><font color=\"red\">The field cannot contain more than 150 characters.</center>";
Die();
}
}
$ebay_url = "http://rover.ebay.com/rover/1/711-53200-19255-0/1?icep_ff3=10&pub=5575165347&toolid=10001&campid=5337851510&customid=&icep_uq=".urlencode($url)."&icep_sellerId=&icep_ex_kw=&icep_sortBy=15&icep_catId=&icep_minPrice=&icep_maxPrice=&ipn=psmain&icep_vectorid=229466&kwid=902099&mtid=824&kw=lg";
$token = "e663e30818201d28dd07803e57333bed4f15803a";
$endpoint = "https://api-ssl.bitly.com/v3/shorten?access_token=".$token."&longUrl=".urlencode($ebay_url);
$result = file_get_contents($endpoint);
$json = json_decode($result, true);
$short_url = $json["data"]["url"];
?>
<br>
<center>
<form name="vini">
<a class="highlighttext" href="javascript:HighlightAll('vini.select1')">Select All</a><br>
<textarea name="select1" rows=3 cols=75 style="font-family:tahoma;color:#555;border:1px dashed #ccc">
<?php echo $short_url; ?>
</textarea>
<br>
</form>
<?php
}
?>
</body>
</html>
注意:我改变了你的ereg
打电话给preg_match
为了与 PHP7 兼容并缩短了if
s 带有正则表达式。我也用过urlencode($url)
这样空格就不会破坏链接。