php - 将 eBay 链接生成器结果转换为 bitly.com 链接

标签 php hyperlink generator bit.ly

我管理了一个小的 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 凭据)

我找到了 thatthat特别是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

最佳答案

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

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

第一步:获取token

你只需要这样做一次。 Bitly 的文档列出了几个示例,您可以如何使用 curl 来做到这一点,我认为这是最简单的方法。你也可以do it with PHP .只需发出此 POST 请求(采取 from here ):

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

结果是这样的:

e663e30818201d28dd07803e57333bed4f15803a

那是你的 token 。

第二步:提出请求

将 token 和 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 超时或其他可能发生的错误(例如 token 过期,即 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) 这样空格就不会破坏链接。

关于php - 将 eBay 链接生成器结果转换为 bitly.com 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44838987/

相关文章:

html - 取消设置 :visited vs a:link 的背景值

javascript - SharePoint 2013 在显示窗体中打开文档

javascript - 无法解构函数生成器中的对象

php - CAKEPHP - 自引用表覆盖变量

php - Yii2:多个事件附加在 View 上

php - 根据表中其他列之间的计算值返回 MySQL 单元格值

php - 谷歌 API 登录

hyperlink - 没有 anchor 文本的链接对搜索引擎优化不利吗?

recursion - Rust 中的递归生成器

c++ - 如何在 C++20 中获得一个运行良好的生成器类模板?