这是 Feedbin 的entries.json的 JSON 条目输出示例:
[
{
"id": 2077,
"title": "Objective-C Runtime Releases",
"url": "http:\/\/mjtsai.com\/blog\/2013\/02\/02\/objective-c-runtime-releases\/",
"author": "Michael Tsai",
"content": "<p><a href=\"https:\/\/twitter.com\/bavarious\/status\/297851496945577984\">Bavarious<\/a> created a <a href=\"https:\/\/github.com\/bavarious\/objc4\/commits\/master\">GitHub repository<\/a> that shows the differences between versions of <a href=\"http:\/\/www.opensource.apple.com\/source\/objc4\/\">Apple\u2019s Objective-C runtime<\/a> that shipped with different versions of Mac OS X.<\/p>",
"summary": "Bavarious created a GitHub repository that shows the differences between versions of Apple\u2019s Objective-C runtime that shipped with different versions of Mac OS X.",
"published": "2013-02-03T01:00:19.000000Z",
"created_at": "2013-02-04T01:00:19.127893Z"
}
]
这是我的函数,它使用 Feedbin 的 API 进行身份验证,检索 JSON 文档,并打印前 5 个结果的标题和 URL。
<?php
// JSON URL which should be requested
$json_url = 'https://api.feedbin.me/v2/entries.json';
$username = 'username'; // authentication
$password = 'password'; // authentication
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => $username . ":" . $password // authentication
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$result = curl_exec($ch); // Getting JSON result string
$cache_feedbin = '/BLAHBLAH/'.sha1($json_url).'.json';
if(file_exists($cache_feedbin) && filemtime($cache_feedbin) > time() - 1000){
// if a cache file newer than 1000 seconds exist, use it
$data_feedbin = file_get_contents($cache_feedbin);
} else {
$data_feedbin = $result;
file_put_contents($cache_feedbin, $data_feedbin);
}
foreach (array_slice(json_decode($data_feedbin), 0, 5) as $obj) {
$feedbin_title = $obj->title;
$feedbin_url = $obj->url;
echo '<li><a href="', $feedbin_url, '">', $feedbin_title, '</a></li>';
}
?>
它就像一个魅力。我想尝试将它与unread_entries.json混合,它会重新调整未读项目的 entry_ids 数组。就像是:
[4087,4088,4089,4090,4091,4092,4093,4094,4095,4096,4097]
我的目标是:在 foreach 中检查哪些 ID 与取自 unread_entries.json 的未读项目的 ID 匹配。对于匹配的 ID(因此,未读的项目)什么都不做,对于所有其他的,显示一个显示“已读”的图像。