匹配从2个输入(JSON)中获取的id,如果为正,则执行一些操作


Match IDs taken from 2 inputs (JSON) and do something if positive

下面是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"的图像。

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;
}
$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";
}