Rumah >pembangunan bahagian belakang >tutorial php >php实现抓取淘宝商品价格人气源码

php实现抓取淘宝商品价格人气源码

WBOY
WBOYasal
2016-07-29 09:04:331838semak imbas

寒假在家无聊想看一下淘宝相关开发怎么样,注册了个淘宝开发者,然后想调api的时候,发现还要很多很复杂的步骤,而且有的还收费,太尼玛坑爹了,于是自己尝试抓了一些淘宝的数据,做了自己的api。

其中获取商品人气,也就是收藏人数,目前只支持淘宝商品,不支持天猫,获取人气、商品名是淘宝和天猫都支持的,但是由于是抓取的淘宝的缓存文件,所以。。。有可能会出现一些问题,但我测试过程中还没遇到问题。

如果大家在使用过程中发现问题,欢迎与我联系,我会进行改进。

我的联系方式:

  • 微博:http://weibo.com/578013333
  • 邮箱:leiflyy@outlook.com

同时,我也正在计划做一些其他的,比如已经实现的获取商品图片,我觉得用处不大。。。所以,下一版再加。

好了,不废话了,,上源码

<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); }); });

以上就介绍了php实现抓取淘宝商品价格人气源码,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn