0

这是 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(因此,未读的项目)什么都不做,对于所有其他的,显示一个显示“已读”的图像。

4

2 回答 2

1

奖金回合:

更新示例以包含用于缓存 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;
}
于 2013-06-06T23:02:46.440 回答
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";
}
于 2013-06-06T22:55:55.333 回答