PHP 的 cURL 函数库来请求一个 API 地址,并将返回的 JSON 数据解析为数组。

魔王  (UID: 1) 12月前 [复制链接]
帖子链接已复制到剪贴板

841 1
[{"title":"推广返利","msg":"邀请他人购买主题,您最高可获10%-20%的佣金返利。","url":"JavaScript:;","img":"https://images.pexels.com/photos/16972133/pexels-photo-16972133.jpeg?auto=compress&cs=tinysrgb&w=1600&lazy=load"},{"title":"程序使用提示","msg":"禁止使用本程序搭建非法网站,一经发现永久封禁。","url":"JavaScript:;","img":"https://images.pexels.com/photos/36464/sky-clouds-clouds-form-cumulus-clouds.jpg?auto=compress&cs=tinysrgb&w=400"}]

 

<?php 
$url = "http://www.url.com/files/api/admap.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$output = curl_exec($ch);
curl_close($ch);
$arr = json_decode($output, true);
for ($i = 0; $i < count($arr); $i++) {
	$ggatitle = $arr[$i]["title"];
	$ggamsg = $arr[$i]["msg"];
	$ggadurl = $arr[$i]["url"];
	$ggadimg = $arr[$i]["img"];
	if ($ggadimg == "JavaScript:;") {
		$ggadimg = "";
	} else {
		$ggadimg = "background-image: url(" . $ggadimg . ");background-size: cover;background-position: center;text-shadow: 0 0 5px rgb(0 0 0 / 50%);color: #ffffff;";
	}
	if ($ggadurl == "JavaScript:;") {
		$ggadurl = "";
	} else {
		$ggadurl = "style=\"color: #ffffff;\" target=\"_blank\" href=\"" . $ggadurl . "\"";
	}
	echo "<p style=\"margin-bottom: 0rem;padding: 10px 10px;margin-bottom: 10px;" . $ggadimg . "\" class=\"callout callout-success\"><a " . $ggadurl . "\">" . $ggamsg . "</a></p>";
}
?>
已有评论 (1)
提示:您必须 登录 才能查看此内容。
创建新帖