【问题标题】:Convert eBay Link Generator results into bitly.com link将 eBay 链接生成器结果转换为 bitly.com 链接
【发布时间】:2017-12-03 23:20:42
【问题描述】:

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

是这样的:

  1. 用户搜索例如:ocz vertex
  2. 点击“提交”,得到以下格式的结果

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

(无法修复 ocz 和顶点词之间生成的链接中的空格)

现在,结果很好,但我想通过 bitly.com 帐户使用 their API 缩短它。
基本上我希望它生成完整的 eBay 链接结果并将其转换为小的 bitly.com 链接(例如http://ebay.to/2scU91k)并在我的 bitly 帐户上查看该链接。

流程如下:

  • 用户搜索像ocz vertex这样的词
  • 点击“提交”
  • 获取 ebay.to 短链接(实际进程在后台, 转换为 rover.ebay.com 地址,然后转换为 ebay.to 使用我的 bitly.com 凭据)

我找到了that 和that,尤其是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

【问题讨论】:

  • 您的 ebay 链接生成链接似乎已损坏,甚至更多。当用户搜索“ocz vertex”时,只需对其进行 url_encode。这不会破坏您的链接,并且正确的链接将被传递给 bitly 并返回。 url_encode 用户的搜索输入
  • 你能告诉我具体如何实现吗?
  • 你能修复 ebay 链接生成器的链接吗?所以我可以检查并正确告诉你

标签: php hyperlink generator bit.ly


【解决方案1】:

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

  • 从“注册应用程序”仪表板获取 API 密钥或从 oauth API 获取 oauth 令牌
  • 使用您的令牌和 URL 向 /v3/shorten API 端点发出请求

第一步:获取令牌

您只需执行一次。 Bitly 的文档列出了一些使用curl 的示例,我认为这是最简单的方法。你也可以do it with PHP。只需发出这个 POST 请求(采取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,并使用正则表达式缩短了 ifs。我还使用了urlencode($url),这样空格就不会破坏链接。

【讨论】:

  • $ebay_url 和 $token 一直在变化。有解决办法吗?
  • @StackBuck 我不确定您的完整脚本/后端是如何工作的,您可以将令牌存储在配置文件或数据库中。对于更改的 Ebay URL,您可能需要考虑使用 sprintf 并将一些模板 URL 存储在具有关键字占位符的位置。
  • "data" 和 "url" 行需要什么参数: $short_url = $json["data"]["url"]; ?另外,该行从哪里获取搜索词?不要在代码中看到它。顺便说一句,“sprintf”是如何工作的?
  • $json 是来自 API 的解码 JSON。它被转换为具有命名索引data 的PHP 对象/数组,这又是一个数组并且具有url。原始数据如下所示:{data: { url: "http://bit.ly/..." }}。搜索词来自$url = $_POST['url'];,并插入到$ebay_url。以下是 sprint 的一些示例(也请查看 PHP 文档):stackoverflow.com/questions/1386593/…
  • 我是否必须将这些搜索词包含到数据库中?我只希望该脚本在我的 bitly 帐户下的 bitly 上创建和列出新的短链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 2021-10-05
  • 2011-09-05
  • 2010-10-07
  • 2012-08-23
  • 1970-01-01
相关资源
最近更新 更多