PHP · 2010年10月18日 0

php获取Bing的壁纸

Bing.com每天都会有新的壁纸放出来,质量都很好,就是可惜小了点。我的Milestone需要960*845大小的图片作壁纸是最好的,可惜Bing的只有958*512。

今天大概看了看代码,发现Bing也是通过ajax的接口来获取一个xml的,索性写了个php的脚本,可以用来抓每天的图片地址。

 <?php
class BingImg {
    private $xml;

    public function  __construct() {
        $xml_url    = 'http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=8';
        $this->xml    = simplexml_load_file(rawurlencode($xml_url));
    }

    public function run() {
        $img = array();
        foreach($this->xml->image as $v) {
            $tmp    = 'http://www.bing.com'.$v->url;
            echo "<img src='{$tmp}' /><br>";
            $img[]    = $tmp;
        }

    }
}

$app = new BingImg();
$app->run();
?>

其中idx表示距今天多少天,例如今天是10月17,如果idx=1则获取10月16的每日壁纸。n表示获取图片的数量从第idx天起再往前几天,我测试了下,似乎n最大是8。

顺便做了个小站,历史上所有bing的壁纸合集。。
http://bingwallpaper.anerg.com/