search
HomeBackend DevelopmentPHP Tutorial PHP实现调用淘宝SDK开放接口回到天猫或淘宝商品详情

PHP实现调用淘宝SDK开放接口返回天猫或淘宝商品详情

在淘宝开发平台申请到app key及app secret并在开放平台上的app tools在线测试成功以后,就可以在自己的项目中使用淘宝SDK来调用淘宝或天猫的商品详情了,包括标题、图片、价格等等所有信息都可已返回。

?

1.整体思路:

?

首先需要下载SDK工具包(我的是在个人中心->网站[左侧边栏]->查看->网站概况页面底部有下载链接)。

?

将下载到的SDK工具包解压到某个目录下:

?

taobaosdk

?

然后在该目录下创建两个文件:index.php用于提交宝贝详情页链接和返回结果的显示,taobao.php用于处理提交过来的url并从中获 取宝贝id,然后调用淘宝SDK相关类和方法将宝贝详情以json格式返回给index.php。大致思路如此,相关代码如下:

?

2.index.php代码:

<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<input type="text" size="64"/>
<button onclick="getProductInfo()">获取商品信息</button>
<div id="product">
商品名称:<h3></h3>
商品价格:<span id="price"></span><br/>
购买链接:<span id="buylink"></span><br/>
商品图片:
<div id="images">
</div>
</div>

<script type="text/javascript">
    function getProductInfo(){
        $.post('taobao.php',{url:$("input").val()},
        function(data){
            $("h3").html(data.item.title);
            $("#price").html(data.item.price);
            $("#buylink").html(data.item.detail_url);
            //alert(data.item.item_imgs.item_img.length);
            var img_nums = data.item.item_imgs.item_img.length;
            for(var i=0;i<img class="box lazy"  src="/static/imghwm/default1.png"  data-src="http://www.daimami.com/img/2014/02/21/222655342.png" _nums;i++){
                $("#images").append("<img  class="box lazy"  src="/static/imghwm/default1.png"  data-src="http://www.daimami.com/img/2014/02/21/222655342.png" +data.item.item_imgs.item_img[i]['url']+"'    style="max-width:90%" PHP实现调用淘宝SDK开放接口回到天猫或淘宝商品详情 " >  ");
             }
        },'json');
}
</script>?

?

3.taobao.php代码:

<?php require_once('TopSdk.php');

if (isset($_POST['url'])):
    $url = $_POST['url'];
    $pattern = "/http:\/\/[\w]+.[\w]+.com\/item.htm\?.*?id=([\d]+).*?/";
    //正则提取淘宝或天猫宝贝id
    preg_match($pattern, $url, $id);
    $c = new TopClient;
    $c->appkey = your_appkey;
    $c->secretKey = 'your_secretkey';
    $c->format = 'json';
    $req = new ItemGetRequest;
    $req->setFields("detail_url,title,price,item_img");
    $req->setNumIid($id[1]);
    $resp = $c->execute($req);
    echo json_encode($resp);
endif;
?>

?

4.测试结果:

?

淘宝宝贝

?

商品链接:

http://item.taobao.com/item.htm?spm=a230r.1.14.43.SjOPaH&id=21932239468&_u=hc3uuihb673

?

获取结果:

taobao

?

天猫宝贝

?

商品链接:

http://detail.tmall.com/item.htm?spm=a230r.1.14.1.SjOPaH&id=24058364609&ad_id=&am_id=&cm_id=140105335569ed55e27b&pm_id=

?

获取结果:

tianmao

?

?

农夫庄园网站创始人,专注Web编程,此外对读书、旅行、历史、养生颇有兴趣。本着开源与共享精神,本站文章请随意转载,但请注明出处:http://www.icultivator.com/p/4139.html

?

?sdk: http://api.taobao.com/myresources/standardSdk.htm?spm=0.0.0.0.ID9FuF

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
华为GT3 Pro和GT4的差异是什么?华为GT3 Pro和GT4的差异是什么?Dec 29, 2023 pm 02:27 PM

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

修复:截图工具在 Windows 11 中不起作用修复:截图工具在 Windows 11 中不起作用Aug 24, 2023 am 09:48 AM

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool