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

2024-01-10

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

事情是这样的:

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

顶点&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 和顶点词之间生成的链接中的空格)

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

这个过程会是这样的:

  • 用户搜索类似的术语ocz vertex
  • 点击“提交”
  • 获取 ebay.to 短链接(而真正的过程在后台, 使用我的转换为 rover.ebay.com 地址,然后转换为 ebay.to bitly.com 凭证)

I found that https://dev.bitly.com/links.html and that https://dev.bitly.com/code_libraries.html尤其是that http://www.yiiframework.com/extension/bitly-url-shortener,但不明白如何将结果实现为新的位转换。

这是 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>

See on live: eBay 链接生成器 http://coreneto.com/delete/generator/index.php


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

  • 从“注册应用程序”仪表板获取 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&&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>

注意:我改变了你的ereg打电话给preg_match为了与 PHP7 兼容并缩短了ifs 带有正则表达式。我也用过urlencode($url)这样空格就不会破坏链接。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 eBay 链接生成器结果转换为 bitly.com 链接 的相关文章

  • MVC和依赖注入,被迫使用单例Controller?

    我正在致力于构建一个根据 MVC 原则运行并利用依赖注入的 PHP 框架 我想我已经把前端控制器部分放下了 有一个工作路由器实例化控制器实例并根据请求的 URI 调用适当的操作 接下来是依赖注入 我想实现一个使用反射解决依赖关系的容器 这样
  • 在 wampserver 2.2 上安装 php_imagick.dll PHP 扩展

    我使用的是 32 位操作系统的 Windows 7 我安装了 ImageMagick 6 8 7 Q16Link https www imagemagick org script download php windows我能够从命令行 转换
  • PHP strtotime() 未返回正确的月份

    由于当前月份 年份是 2012 年 1 月 为什么以下代码返回 2011 年 12 月而不是 2011 年 11 月 echo date F Y strtotime 2 months 如果有影响的话 这是在 PHP 5 3 0 上 要获得您
  • 在 PHP 中设置 HTTP 响应代码(在 Apache 下)

    给出以下两种在 PHP 中设置 HTTP 响应代码的方法 具体来说 在 Apache 下 方法一 http response code 404 方法二 header HTTP 1 0 404 Not Found 我的问题是 除了这个事实之外
  • laravel cron 使用错误

    Laravel 错误 cron 使用 usr bin php home sitevk artisan 计划 运行 1 gt gt dev null 2 gt 1 应用 控制台 内核 use Illuminate Console Schedu
  • 在 Blogger 中使用相对链接

    我正在使用博主 当我需要在我的博客文章中提到一个链接并且该链接实际上是我自己的博客文章的链接时 我在其旁边提到标签 www my blog name blogspot in 12 2013 how to do html if i chang
  • php动态创建子域的问题

    你好 我通过以下代码在 php 中创建子域 function subd host port ownername passw request sock fsockopen localhost 2082 if sock print Socket
  • 使用控制器通过 codeigniter 处理返回的自定义 css 和 javascript 文件

    我正在开发一个 php codeigniter 项目 我正在考虑创建一个专门用于处理返回自定义 css 和 javascript 文件的控制器 在之前的项目中 我在视图文件的标头中包含了外部 CSS 和 JS 文件 但它们本质上必须是静态的
  • 使用 php 脚本的电子邮件管道

    你好 我想将所有电子邮件 到达我的收件箱 转发到 php 脚本并检索电子邮件内容并将其保存在文件中 因此 我正确地添加了具有管道路径的电子邮件转发器 转发地址 电子邮件受保护 cdn cgi l email protection 管道到程序
  • php 检查文件是否存在于外部域中(从子域访问)

    我有一个网站http www reelfilmlocations co uk http www reelfilmlocations co uk 上述网站有一个管理区域 其中上传图像并在 uploads images 目录的子文件夹中创建不同
  • 将具有值的产品属性添加到 Woocommerce 中的产品

    我正在使用此代码添加自定义属性 attributes array array name gt Size options gt array S L XL XXL position gt 1 visible gt 1 variation gt
  • AFNetworking 上传图片

    我看过一些例子 但我认为我的问题可能出在 PHP 中 我正在尝试使用 AFNetworking 将图像从 iPhone 上传到服务器 这是我的 obj c 代码 IBAction uploadButtonClicked id sender
  • 如何在php中根据url从mysql获取数据?

    我在 mysql 数据库中有一个页表 其中包含 page name title content author 字段 我想用 php 来获取它http www domain com index php page page name http
  • 为什么 PHPUnit 将一些右大括号显示为未被覆盖?

    我正在使用 PHPUnit 3 6 7 PHP CodeCoverage 1 1 1 和 Xdebug 2 1 2 当我让 PHPUnit 将代码覆盖率统计信息写入 clover 样式的 XML 文件时 它偶尔会显示一个右花括号 表示测试未
  • 检查一个类是否是另一个类的子类

    我想在不创建实例的情况下检查一个类是否是另一个类的子类 我有一个类 它接收类名作为参数 作为验证过程的一部分 我想检查它是否属于特定的类系列 以防止安全问题等 有什么好的方法可以做到这一点吗 is subclass of http php
  • 警告:mysqli_query() 期望参数 1 为 mysqli,在 中给出 null

    我正在尝试构建一个简单的自定义 CMS 但出现错误 警告 mysqli query 期望参数 1 为 MySQLi 在中给出的为 null 为什么我会收到此错误 我的所有代码都已经是 MySQLi 并且我使用两个参数 而不是一个 con m
  • PHP 的 mb_internal_encoding 实际上是做什么的?

    根据 PHP 网站 http www php net manual en function mb internal encoding php它这样做 coding 是用于 HTTP 输入的字符编码名称 字符编码转换 HTTP输出字符编码 转
  • 简单的颜色变化

    我正在创建一个用户界面 用户可以在其中更改页面的颜色值 我想要的是获取分配给其背景颜色的值并将其变亮一定程度 我只是想获得一条亮点线 而不必每次都制作新图像 示例 用户将背景颜色设置为 ECECEC 现在我希望某个元素边框变成 F4F4F4
  • preg_match 所有以@开头的单词?

    我对正则表达式不太确定 所以我不得不问你 如何用 PHP 判断字符串中是否包含以 开头的单词 例如我有一个像 This is for codeworxx 这样的字符串 我很抱歉 但我没有任何起点 希望你能帮忙 谢谢 萨沙 好的 谢谢你的结果
  • 一些基本的 PHP 问题 [已关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我只是有一些基本的 php 问题来加深我对学习的理解 但我找不到简单的答案 我有一个 php ajax 应用程序 它生成 mysql

随机推荐