doubinei1457 2013-06-06 22:45
浏览 39
已采纳

匹配从2个输入(JSON)获取的ID,如果是肯定的话,做一些事情

Here's a sample of a JSON entry output from Feedbin's entries.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"
  }
]

Here's my function which authenticates with Feedbin's API, retrieves the JSON document, and prints title and URL for the first 5 results.

<?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>';
    }
?>

It works like a charm. What I'd love to try is mixing this with unread_entries.json, which retuns an array of entry_ids of unread items. Something like:

[4087,4088,4089,4090,4091,4092,4093,4094,4095,4096,4097]

My goal is: check within the foreach which IDs match with the IDs of unread items taken from unread_entries.json. For the IDs that match (so, unread items) do nothing, for ALL THE OTHERS, display an image which says "READ".

  • 写回答

2条回答 默认 最新

  • duanguanya3052 2013-06-06 23:02
    关注

    BONUS ROUND:

    Updated example to include a function for caching JSON requests:

    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条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?