将 eBay Link 生成器结果转换为 bitly.com link

Convert eBay Link Generator results into bitly.com link

我管理了一个小的 PHP 脚本,它获取搜索到的 eBay 产品并将其转换为推广 eBay link。

它是这样的:

  1. 用户搜索例如:ocz vertex
  2. 点击 "Submit" 并获得以下格式的结果

http://rover.ebay.com/rover/1/711-53200-19255-0/1?icep_ff3=10&pub=5575165347&toolid=10001&campid=5337851510&customid=&icep_uq=ocz vertex&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

(无法修复link中ocz和顶点词之间生成的那个space

现在,结果不错,但我想通过 bitly.com account using their API 缩短它。
基本上,我希望它生成完整的 eBay link 结果并将其转换为小的 bitly.com link(例如 http://ebay.to/2scU91k),然后在我的网站上看到 link帐号。

流程是这样的:

我找到了 that and that and especially that,但不明白如何将结果实现为新的位转换。

这是 PHP 代码:

<!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&&copytoclip==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>
URL<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 (ereg("^\.",$url)) {
echo "<br><center><font color=\"red\">Invalid Characters.</center>";  
 Die();
}
if (ereg("\<", $url)) {
echo "<br><center><font color=\"red\">Invalid Characters.</center>";  
 Die();
}
if (ereg("\[", $url)) {
echo "<br><center><font color=\"red\">Invalid Characters.</center>";  
 Die();
}
if (ereg("\'", $url)) {
echo "<br><center><font color=\"red\">Invalid Characters.</center>";  
 Die();
}
if (ereg("\#", $url)) {
echo "<br><center><font color=\"red\">Invalid Characters.</center>";  
 Die();
}
if (ereg("\`", $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();
}
}
?>
<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">
http://rover.ebay.com/rover/1/711-53200-19255-0/1?icep_ff3=10&pub=5575165347&toolid=10001&campid=5337851510&customid=&icep_uq=<?php echo $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
</textarea>
<br>
</form>

<?php
}
?>
</body>
</html>

直播Ebay link Generator

我构建了一个无需外部库即可运行的小型解决方案。它基本上归结为:

  • 从 "Register an application" 仪表板获取 API 密钥或从 oauth API
  • 获取 oauth 令牌
  • 使用您的令牌和 URL
  • /v3/shorten API 端点发出请求

第一步:获取令牌

您只需执行一次。 Bitly 的文档列出了几个示例,您可以如何使用 curl, I think that is the easiest way. You could also do it with PHP. Simply make this POST request (taken from here):

curl -u "username:password" -X POST "https://api-ssl.bitly.com/oauth/access_token"

结果是这样的:

e663e30818201d28dd07803e57333bed4f15803a

那是你的令牌。

第二步:提出请求

将令牌和 url 编码的 URL 插入到 /v3/shorten 端点的 HTTP 请求中:

<?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 超时或可能发生的其他错误(例如令牌到期,即 currently not an issue)。

完整代码:

<!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&&copytoclip==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>

注意:为了与 PHP7 兼容,我将您的 ereg 调用更改为 preg_match,并使用正则表达式缩短了 if。我还使用 urlencode($url) 这样空格就不会破坏 link.