保存JSON或XML API响应字preSS数据库 [英] Saving JSON or XML API response to a Wordpress database

查看:119
本文介绍了保存JSON或XML API响应字preSS数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的项目,我想一个摄影师的Flickr帐户用他的话语preSS网站整合。我们的想法是对Word preSS的网站Flickr的他同步。

I'm working on project and I'm trying to integrate a photographer's Flickr account with his Wordpress website. The idea is to sync the Wordpress website with his flickr.

我使用Flickr的API让他picures网址,描述,标签和套和显示everyting。问题是,每一个访问中,我不得不经历整个API响应,并解析它。要提取的所有链接,标签,说明书等。

I'm using the Flickr API to get his picures URLS, descriptions, tags and sets and display everyting. The problem is that for every visit I have to go through the whole API response and parse it. Have to extract all the links, tags, descriptions etc.

我正在寻找一种方式来进口这个API响应(XML或JSON)字preSS数据库,并使用该数据。我就必须对数据库进行更新(或者只是一个表的数据库上),一旦他更新了他的Flickr帐户的东西的选项。此更新并不需要是自动的。

I'm looking for a way to "import" this API response (XML or JSON) to the wordpress database and work with this data. I'll just have the option to update the database (or maybe just a table on the database) once he updates something on his flickr account. This update doesn't need to be automatic.

推荐答案

下面只是一个概念证明,并与Flickr的API响应的结果仪表盘创建一个菜单项。

The following is just a proof of concept and creates a menu item in the dashboard with the results of the Flickr API response.

add_action( 'admin_menu', function() 
{
    add_menu_page( 
        'Flicker', 
        'Flicker', 
        'add_users', 
        'fck_admin',
        'consult_flickr_api_so_7173971', 
        'http://i.stack.imgur.com/s2ons.png',
        2
    );  
});

function consult_flickr_api_so_7173971() 
{
    $api_key = 'YOUR-KEY';
    $secret_key = 'YOUR-SECRET'; // not needed for public data
    $uid = 'USER-ID-TO-CONSULT';
    $url = 'https://api.flickr.com/services/rest/?&method=flickr.people.getPublicPhotos&api_key=' 
         . $api_key 
         . '&user_id=' 
         . $uid 
         . '&format=json&nojsoncallback=1&per_page=15'; // maximum 500
    $flickr = wp_remote_get( $url, array( 'timeout' => 120, 'httpversion' => '1.1' ) );   
    if ( $flickr['response']['code'] == '200' )
    {        
        $flickr_array = json_decode( $flickr['body'], true );
        foreach( $flickr_array['photos']['photo'] as $photo )
        {
            echo '<h2>' . $photo['title'] . '</h2>';
            get_flickr_photo_so_7173971( $photo['id'] );
        }
    }
}

/**
http://www.flickr.com/services/api/
Useful Methods
 - getExif
 - getInfo
 - getSizes
*/
function get_flickr_photo_so_7173971( $photo_id, $method = 'getSizes' )
{
    $api_key = 'YOUR-KEY';
    $flickr = wp_remote_get( 
        'https://api.flickr.com/services/rest/?&method=flickr.photos.'
        . $method
        . '&api_key=' 
        . $api_key
        . '&photo_id='
        . $photo_id
        . '&format=json&nojsoncallback=1',
        array(
             'timeout' => 120, 
            'httpversion' => '1.1' 
        ) 
    );

    if ( $flickr['response']['code'] == '200' )
    {
        $flickr_array = json_decode( $flickr['body'], true );        
        $no_print = true;
        foreach( $flickr_array['sizes']['size'] as $size ) 
        {
            if( $size['label'] == 'Medium' ) {
                print_photo_so_7173971( $size );
                $no_print = false;
            } 
        }
        // No medium size was found, just print the first one
        if( $no_print ) 
        {
            print_photo_so_7173971( $flickr_array['sizes']['size'][0] );
        }
    }
}

function print_photo_so_7173971( $size ) 
{
    printf( 
        '<img src="%s" width="%s" height="%s" /><br />',
        $size['source'],
        $size['width'],
        $size['height']
    );
}

我将离开这些结果的操纵给读者。

I'll leave the manipulation of these results to the reader.

建议:

  • Set up a cron job to pull the information and do your stuff. Use this as reference: Website widget to display Apple App store price drops?
  • Create an options page and display the results with thumbnails, extended info and buttons to dispatch some action (image side load, create new post, etc), see here and here.

字preSS StackExchange充满美好片段和技术。

WordPress StackExchange is full of nice snippets and techniques.

这篇关于保存JSON或XML API响应字preSS数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆