以下是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_id数组。类似的东西:
[4087,4088,4089,4090,4091,4092,4093,4094,4095,4096,4097]
我的目标是:在foreach中检查哪些ID与从unread_entries.json获取的未读项目的ID匹配。对于匹配的ID(所以,未读的项目)什么都不做,对于所有其他人,显示一个“READ”的图像。
答案 0 :(得分:1)
BONUS ROUND:
更新了包含用于缓存JSON请求的函数的示例:
define('CACHE_PATH', '/tmp'); // Might want to change this
define('CACHE_SECS', 1000);
define('API_USERNAME', 'username');
define('API_PASSWORD', 'password');
$entries = fetchJSON('https://api.feedbin.me/v2/entries.json', API_USERNAME, API_PASSWORD, CACHE_SECS);
$unread_msgs = fetchJSON('https://api.feedbin.me/v2/unread_entries.json', API_USERNAME, API_PASSWORD, CACHE_SECS);
foreach ($entries as $obj) {
$is_read = !in_array($obj->id, $unread_msgs); // Read if not present in unread
$feedbin_title = $obj->title;
$feedbin_url = $obj->url;
$output = '<li><a href="'.$feedbin_url.'">'. $feedbin_title;
if ($is_read) {
$output .= ' <img src="icon-read.png" title="READ" />';
}
$output .= '</a></li>';
echo $output;
}
/** Return a JSON decoded object/array */
function fetchJSON($json_url, $username = null, $password = null, $cache_secs = null) {
$cache_file = CACHE_PATH.'/'.sha1($json_url).'.json';
$data = null;
// Check if we need to request new content
if (!$cache_secs || !file_exists($cache_file) || filemtime($cache_file) < time() - $cache_secs) {
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
);
// If username given add to curl opts
if (!empty($username)) {
$options[CURLOPT_USERPWD] = $username . ":" . $password;
}
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$data = curl_exec($ch); // Getting JSON result string
curl_close($ch);
if ($cache_secs) {
file_put_contents($cache_file, $data);
}
} else {
// Got data from the cache
$data = file_get_contents($cache_file);
}
return !empty($data) ? json_decode($data) : null;
}
答案 1 :(得分:0)
$ids = [4087,4088,4089,4090,4091,4092,4093,4094,4095,4096,4097];
$id = 4087;
if (isset($ids[$id]) )
{
echo "do something\n";
}
else
{
echo "do something else\n";
}