search
HomeBackend DevelopmentPHP TutorialPHP implements grabbing Taobao product price popularity source code

I was bored at home during the winter vacation and wanted to see what Taobao-related development was like. I registered as a Taobao developer, and then when I wanted to adjust the API, I found that there were many complicated steps, and some of them were charged. It was so annoying, so I tried to capture some Taobao data and made my own API.

Getting product popularity, that is, the number of collectors, currently only supports Taobao products, not Tmall. Obtaining popularity and product names are supported by both Taobao and Tmall, but because it is a cached file of Taobao, so. . . There may be some problems, but I haven't encountered any problems during testing.

If you find any problems during use, please contact me and I will make improvements.

My contact information:

  • Weibo: http://weibo.com/578013333
  • E-mail: leiflyy@outlook.com

At the same time, I am also planning to do some other things, such as the achieved acquisition I don’t think the product pictures are very useful. . . So, I’ll add it to the next edition.

Okay, no more nonsense, here’s the source code

<code><span><span><?php </span><span>/**
 * Created by PhpStorm.
 * User: leif
 * Date: 16/1/26
 * Time: 10:17
 * email: leiflyy@outlook.com
 */</span><span>/**
 *  实现传入宝贝的id,返回宝贝的链接,支持淘宝
 *<span> @param</span> $id 宝贝的id
 *<span> @return</span> string 返回的宝贝的链接
 */</span><span><span>function</span><span>getTbLink</span><span>(<span>$id</span>)</span>{</span><span>$url</span>=<span>"https://item.taobao.com/item.htm?spm=a1z10.4-c.w5003-12641268955.30.0lDnKZ&id="</span>.<span>$id</span>.<span>"&scene=taobao_shop"</span>;
    <span>return</span><span>$url</span>;
}


<span>/**
 * 实现传入宝贝的id,获取宝贝的商品名,支持淘宝和天猫
 *<span> @param</span> $id  宝贝的id
 *<span> @return</span> mixed  宝贝的商品名
 */</span><span><span>function</span><span>getNameById</span><span>(<span>$id</span>)</span>{</span><span>$url</span>=<span>"http://hws.m.taobao.com/cache/wdetail/5.0/?id="</span>.<span>$id</span>;
    <span>$content</span>=file_get_contents(<span>$url</span>);
    <span>$content_ori</span>=strip_tags(<span>$content</span>);
    <span>$content_arr</span>=json_decode(<span>$content_ori</span>,<span>true</span>);
    <span>$detail</span>=json_decode(<span>$content_arr</span>[<span>'data'</span>][<span>'apiStack'</span>][<span>'0'</span>][<span>'value'</span>],<span>true</span>);
    <span>$success_sym</span>=<span>$detail</span>[<span>'ret'</span>][<span>'0'</span>];<span>//成功则返回"SUCCESS::调用成功";</span><span>if</span>(<span>$success_sym</span>==<span>"SUCCESS::调用成功"</span>){
        <span>$name</span>=<span>$content_arr</span>[<span>'data'</span>][<span>'itemInfoModel'</span>][<span>'title'</span>];
        <span>return</span><span>$name</span>;
    }<span>else</span>{
        <span>return</span><span>"<script type="text/javascript">alert('宝贝不存在!');</script>"</span>;
    }

}

<span>/**
 * 实现传入宝贝id,获取宝贝价格,支持淘宝和天猫
 *<span> @param</span> $id   宝贝的id
 *<span> @return</span> mixed 返回的宝贝的价格或价格区间
 */</span><span><span>function</span><span>getPriceById</span><span>(<span>$id</span>)</span>{</span><span>$url</span>=<span>"http://hws.m.taobao.com/cache/wdetail/5.0/?id="</span>.<span>$id</span>;
    <span>$content</span>=file_get_contents(<span>$url</span>);
    <span>$content_ori</span>=strip_tags(<span>$content</span>);
    <span>$content_arr</span>=json_decode(<span>$content_ori</span>,<span>true</span>);
    <span>$pro_detail</span>=json_decode(<span>$content_arr</span>[<span>'data'</span>][<span>'apiStack'</span>][<span>'0'</span>][<span>'value'</span>],<span>true</span>);
    <span>$success_sym</span>=<span>$pro_detail</span>[<span>'ret'</span>][<span>'0'</span>];<span>//成功则返回"SUCCESS::调用成功";</span><span>if</span>(<span>$success_sym</span>==<span>"SUCCESS::调用成功"</span>){
        <span>$pro_price</span>=<span>$pro_detail</span>[<span>'data'</span>][<span>'itemInfoModel'</span>][<span>'priceUnits'</span>][<span>'0'</span>][<span>'price'</span>];
        <span>return</span><span>$pro_price</span>;
    }<span>else</span>{
        <span>return</span><span>"<script type="text/javascript">alert('宝贝不存在!');</script>"</span>;
    }
}

<span>/**
 *  实现传入宝贝id,获取宝贝的收藏人数(人气),支持淘宝
 *<span> @param</span> $id  宝贝id
 *<span> @return</span> mixed   返回的宝贝的收藏人数(人气)
 */</span><span><span>function</span><span>getPopById</span><span>(<span>$id</span>)</span>{</span><span>$url</span>=getTbLink(<span>$id</span>);
    <span>$urlinfo</span> = parse_url(<span>$url</span>);
    parse_str(<span>$urlinfo</span>[<span>'query'</span>], <span>$query</span>);
    <span>$id</span> = <span>$query</span>[<span>'id'</span>];
    <span>$data</span> = file_get_contents(<span>$url</span>);
    <span>$start</span> = strpos(<span>$data</span>, <span>'counterApi'</span>);
    <span>$start</span> = strpos(<span>$data</span>, <span>": "</span>, <span>$start</span>);
    <span>$end</span> = strpos(<span>$data</span>, <span>"',"</span>, <span>$start</span>);
    <span>$api</span> = <span>'https:'</span> . substr(<span>$data</span>, <span>$start</span> + <span>3</span>, <span>$end</span> - <span>$start</span> - <span>3</span>) . <span>'&callback=jsonp107'</span>;
    <span>$response</span> = file_get_contents(<span>$api</span>);
    <span>$response</span> = substr(<span>$response</span>, <span>9</span>, -<span>2</span>);
    <span>$arr</span> = json_decode(<span>$response</span>, <span>true</span>);
    <span>$popularity</span>=<span>$arr</span>[<span>'ICCP_1_'</span>.<span>$id</span>];
    <span>return</span><span>$popularity</span>;
}</span></span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the PHP source code for capturing Taobao product prices and popularity, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

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