寒假在家無聊想看一下淘寶相關開發怎麼樣,註冊了個淘寶開發者,然後想調api的時候,發現還要很多很複雜的步驟,而且有的還收費,太尼瑪坑爹了,於是自己嘗試抓了一些淘寶的數據,做了自己的api。
其中獲取商品人氣,也就是收藏人數,目前只支持淘寶商品,不支持天貓,獲取人氣、商品名是淘寶和天貓都支持的,但是由於是抓取的淘寶的緩存文件,所以。 。 。有可能會出現一些問題,但我測試過程中還沒遇到問題。
如果大家在使用過程中發現問題,歡迎與我聯繫,我會進行改進。
我的聯絡方式:
同時,我也正在計劃做一些其他的,例如已經實現的商品圖片,我覺得用處不大。 。 。所以,下一版再加。
好了,不廢話了,,上源碼
<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></code>
以上就介紹了php實作抓取淘寶商品價格人氣源碼,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。