通过PHP获取bing.com的每日一图

网站APP 3 年前 回复

, ,

如果想调用bing.com的每日一图,通过php可以很轻松获取,下面列举两段代码,均可实现:

1、通过处理json获取数据:

$bing = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1";
$Pinfo = json_decode(file_get_contents($bing));
$img = $Pinfo->{"images"}[0]->{"url"};
$imgcopyright = $Pinfo->{"images"}[0]->{"copyright"};
$imgurl = "https://cn.bing.com".$img;
echo $imgurl;
echo $imgcopyright;

2、通过正则匹配获取数据:

$str=file_get_contents("https://cn.bing.com/HPImageArchive.aspx?idx=0&n=1");
if(preg_match("/(.+?)<\/url>/ies",$str,$matches)){
    $imgurl = "https://cn.bing.com".$matches[1];
}
else{
    $imgurl = "test.jpg";
}
echo $imgurl;

上述两段程序的效率,第一个要好些!

原文分享于https://www.psay.cn/code/78.html

支付宝打赏微信打赏

如果此文对你有帮助,欢迎打赏作者。

发表评论

欢迎回来 (打开)

(必填)