使用百度的接口獲取美女圖片,并用瀑布流的形式展示到自己的頁面中。
github項(xiàng)目地址:https://github.com/craryprimitiveman/pretty
最終效果如下:
點(diǎn)開百度圖片的美女頻道,http://image.baidu.com/channel?c=%e7%be%8e%e5%a5%b3#%e7%be%8e%e5%a5%b3
打開chrome的調(diào)試工具或者firefox的firebug工具,將滾動條拖到最下面,可以再network中看到如下請求:
其參數(shù)如下:
通過分析,推斷并驗(yàn)證了其中字段的含義,col表示頻道,tag表示的是全部的美女,也可以是其他tag(其內(nèi)容如下圖),pn表示從第幾張圖片開始,rn表示獲取多少張
其返回值如下:
分析imgs中的內(nèi)容,選出自己想要的屬性,我確定要使用shareurl。
這樣我們就可以用php的curl根據(jù)百度的api獲取到美女圖片了。
在php中可以這樣寫:
<?php require_once './requests/library/requests.php'; requests::register_autoloader(); $page = $_get['page']; $pagesize = 10; $options = array('host'=>'image.baidu.com'); $url = 'http://image.baidu.com/data/imgs?sort=0&pn=' . $page * $pagesize . '&rn=' . $pagesize . '&col=美女&tag=全部&tag3=&p=channel&from=1'; $response = requests::get($url, $options); echo $response->body; ?>
其中的requests 是一個(gè)php的http請求的庫,地址:http://requests.ryanmccue.info/
其中的美女也可以替換成其他頻道。
之后就是去展示圖片,使用了一個(gè)瀑布流的插件實(shí)現(xiàn)。
最終的代碼都放到了我的github上,項(xiàng)目地址:https://github.com/craryprimitiveman/pretty
只不過github上的col的參數(shù)是攝影~~
當(dāng)然小伙伴們也可以換成其他和諧資源,自由發(fā)揮下吧。