[文章作者:张宴 本文版本:v1.0 最后修改:2012.02.16 转载请注明原文链接:http://blog.zyan.cc/taobaoke_click_urls/]

  根据淘宝商品 num_iid 批量生成淘宝客(什么是淘宝客?)链接的 PHP 文件内容如下。

  淘宝 API 有调用次数限制,一次 API 调用,可以最大返回40个商品的淘宝客链接,因此,在本函数内,如果需要批量生成的淘宝商品 num_iid 数大于40,将按照40个一次,分多次调用。如果调用淘宝 API 查询过的商品 num_iid,不管其是否有淘宝客链接(有些商品没有淘宝客推广链接),都将利用 Memcached 缓存起来,下次直接查缓存,不会重复调用淘宝 API。

<?php
require_once(dirname(__FILE__).'/TopSdk.php'); //引用淘宝开放平台 API SDK

function object2Array($d)
{
        if (is_object($d))
        {
            $d = get_object_vars($d);
        }

        if (is_array($d))
        {
            return array_map(__FUNCTION__, $d);
        }
        else
        {
            return $d;
        }
}


/*********************************************
* 函数名:get_taobaoke_link ($num_iids)
* 函数用途:通过淘宝商品 num_iids 获取其对应的淘宝客手机版链接
* 创建时间:2012-02-14
* 创建人:张宴 net@zyan.cc
* 参数说明:
*    $num_iids   淘宝商品ID(支持多个商品)数组,示例如下:
*          $num_iids[] = "13583512568";
*          $num_iids[] = "10809380078";
*          $num_iids[] = "10809380079";  
* 返回值:
*    下标为淘宝商品 num_iid ,值为淘宝客链接 click_url 的二维数组。如果无淘宝客链接,click_url 为空字符串,示例如下:
*          array(3) {
*            ["13583512568"]=>
*            string(191) "http://auction1.wap.taobao.com/auction/item_detail-0db2-13583512568.jhtml?tks=jUTwPLMDtUUNEZhqfEuTZqkZhGw1LA7%2BzCJBXCj27NpurHxjZN70Amg0DVaFU61pfnHwW%2FI4MZGm%0Awgb69kbb1NL8uwtu%2BDnyAunBCVDP"
*            ["10809380078"]=>
*            string(187) "http://auction1.wap.taobao.com/auction/item_detail-0db2-10809380078.jhtml?tks=jUTwPLMDtUUNEGWhOOgVVuX%2BJKYt7fesyuZjEe7hvmpTJxYDfK8i1Wpvfl7lwI7nzD9W8M352v6E%0AyuUtsKun81AGltKzJWCYPiVDiOeC"
*            ["10809380079"]=>
*            string(0) ""
*          }  
*********************************************/
function get_taobaoke_link ($num_iids) {
  $memcache = new Memcache;
  $memcache->connect('127.0.0.1', 11911); //Memcached 缓存服务器地址
  $click_urls = $memcache->get($num_iids);
  
  foreach ($num_iids AS $num_iid) {
    if (!isset($click_urls[$num_iid])) {
      $tbapi_num_iids_arr[] = $num_iid;
    }
  }
  
  if (!empty($tbapi_num_iids_arr)) {
    $numbers = count($tbapi_num_iids_arr);
    $numbers_max = 40; //淘宝 API 限制最大返回40条记录
    if ($numbers > 0) {
      $numbers_times = ceil($numbers / $numbers_max); //第一层循环的循环次数
      $numbers_start = 0;
      $numbers_end = $numbers_max;
      for ($numbers_i = 1; $numbers_i <= $numbers_times; $numbers_i++) {
        for ($numbers_j = $numbers_start; $numbers_j < $numbers_end; $numbers_j++) {
          if ($numbers_j >= $numbers) {
            break;
          }
          $tbapi_num_iids_arr_sp[] = $tbapi_num_iids_arr[$numbers_j];
        }
        
        $numbers_start = $numbers_start + $numbers_max;
        $numbers_end = $numbers_end + $numbers_max;
        
        $tbapi_num_iids = implode(",", $tbapi_num_iids_arr_sp);
        $c = new TopClient;
        $c->appkey = 12498835; //淘宝开放平台 API 接口 App Key
        $c->secretKey = "745db5f8e316f9f1aa8310a7568d6566"; //淘宝开放平台 API 接口 App Secret
        $c->format = "json";
        $req = new TaobaokeItemsConvertRequest;
        $req->setFields("num_iid,click_url");
        $req->setNumIids($tbapi_num_iids);
        $req->setPid(29509662); //淘宝联盟(阿里妈妈)PID
        $req->setIsMobile("true"); //如果要生成手机页面的淘宝客链接,选择 true;网页版选择 false
        $resp = $c->execute($req);
        $res = object2Array($resp);
  
        if (isset($res["taobaoke_items"]["taobaoke_item"])) {
          $links = $res["taobaoke_items"]["taobaoke_item"];
          foreach ($links as $value) {
            $memcache->set($value["num_iid"], $value["click_url"], MEMCACHE_COMPRESSED, 0);
            $click_urls[(string)$value["num_iid"]] = $value["click_url"];
          }
        }
        
        unset($tbapi_num_iids_arr_sp);
        unset($tbapi_num_iids);
        unset($resp);
        unset($res);
        unset($links);
        unset($value);
      }
    }
  }
  
  foreach ($num_iids AS $num_iid) {
    if (!isset($click_urls[$num_iid])) {
      $memcache->set($num_iid, "", MEMCACHE_COMPRESSED, 0);
      $click_urls[(string)$num_iid] = "";
    }
  }  
  
  $memcache->close();
  return $click_urls;
}

//演示
$num_iids[] = "13583512568";
$num_iids[] = "10809380078";
$num_iids[] = "10809380079";
$click_urls = get_taobaoke_link ($num_iids);
var_dump($click_urls);
?>




  淘宝开放平台(http://open.taobao.com/) PHP SDK 下载:


  申请淘宝 API 的 App Key 和 App Secret ,可以到 http://my.open.taobao.com/ 进行。

  点击在新窗口中浏览此图片




技术大类 » PHP/JS/Shell | 评论(219) | 引用(0) | 阅读(100707)
Sally Edwards Email Homepage
2022-9-1 15:21
Great postDownload the latest and completely free <a href=https://dzwonkitones.com/>dzwonek na telefon</a> for your phone at dzwonkitones.com
Loretta Tucker Email Homepage
2022-9-1 15:23
Great postDownload the latest and completely free ring tone for your phone at dzwonkitones.com]dzwonek na telefon
Evan Jenkins Email Homepage
2022-9-1 17:57
download trending songs as ringtone for your phone by downloading ringtone at tonosmp3gratis.com to make your phone tonos para celular
capcutforpc
2022-10-16 17:07
Get capcut to create videos to use [url=https://capcutforpc.live/]
world Email Homepage
2022-10-17 22:20
To improve user experience, the interface itself needs to be polished a bit more. World777 Nevertheless, customer service is easy to use, and you can usually have the majority of your questions answered by using the live chat feature. The ability to interact with other users is an intriguing feature that aids in query answering and raises the website's engagement level.https://world777app.com/
HP 58A vs 58X, Which One Should I Buy? Email Homepage
2022-10-24 14:39
Learn about the printer and shop for cheap tonerCanon Color imageClASS MF743Cdw Review
Canon 055H Email Homepage
2022-10-24 14:41
These high-quality Canon 055 Canon 055H Black toner cartridges are at low prices.
DannyCobb Email Homepage
2022-11-15 10:13
Your forum is awesome. Go right here whatsapp aero oficial to experience the trending chat app
SLOTPG Homepage
2023-2-25 12:50
play online Earn extra income at home chill on holiday allow you to earn money anytime, anywhere through an online service system that supports playing from all devices, whether computers or mobile phones Just apply with us at พีจีสล็อต and you are ready to win. Make money online immediately with a collection of games from the PG slot camp.
BETFLIK Homepage
2023-2-25 15:17
BETFLIK.GG รวมเกมออนไลนื ทำเงินได้จริง มีมากกว่า 500 เกมรวมเกมจากค่าย เกมออนไลน์ ทำเงินได้จริงชั้นนำประจำปี 2023 ล่าสุด สมัครสมาชิกวันนี้กับทาง BETFLIK รับเครดิตฟรี นำไปใช้ได้กับทุกเกมแน่นอน
slot Email Homepage
2023-5-26 15:13
Honey, let's treat ourselves to a delightful gaming session with the visually stunning slot games on TGA's website. The CG graphics will leave us in awe.
slot Email Homepage
2023-5-26 15:15
My dear, let's escape into a world of fantasy and excitement by playing the top-rated slot games on TGA. The captivating CG visuals will transport us to another realm.
slot Email Homepage
2023-5-26 15:16
Sweetheart, join me in unlocking the thrills of the newest slot games on TGA's platform. The CG graphics will dazzle our eyes and elevate our gaming adventure.
KKKok Email Homepage
2023-5-31 21:22
Indulge in the delightful company of our adorable hostesses at PG SLOT where every moment spent here is filled with joy and satisfaction.
LucyGray Email
2023-6-3 23:22
Collection of games and applications apk android completely free for mobile phones that I found, you can refer and relax every day.
tga สล็อตเว็บตรง Email
2023-7-1 20:09
Can you tga สล็อตเว็บตรง provide details on the 100% direct website slot and its unique features?
ufabet wallet Email
2023-7-1 20:28
What ufabet wallet measures are in place to ensure the visuals and graphics of the slot game are stunning and luxurious?
สล็อตเว็บตรง
2023-7-26 18:32
Are you สล็อตเว็บตรง enticed by the convenience of instant access to the direct web slots without the need for downloads or installations?
ufatwin88
2023-7-26 18:32
What makes ufatwin88 the direct web slots a perfect pastime for players seeking relaxation, entertainment, and the chance to win big?
tga-bet.com
2023-7-26 18:33
Have you tga-bet.com explored the comprehensive customer support and assistance available on the direct web platform?
分页: 10/11 第一页 上页 5 6 7 8 9 10 11 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]