将JSON或XML API响应保存到Wordpress数据库

编程入门 行业动态 更新时间:2024-10-26 18:30:24
本文介绍了将JSON或XML API响应保存到Wordpress数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发项目,我试图将摄影师的Flickr帐户与他的Wordpress网站集成。

我使用Flickr API来获取他的图片URLS,描述,标签和集合以及显示每一个。问题是,每次访问,我必须通过整个API响应并解析它。必须提取所有的链接,标签,描述等。

我正在寻找一种方法来import这个API响应(XML或JSON)到wordpress数据库并使用此数据。我只有选择更新数据库(或者只是数据库上的表)一旦他更新了他的flickr帐户上的东西。此更新不需要是自动的。

解决方案

以下只是一个概念证明,信息中心与Flickr API响应的结果。

add_action('admin_menu',function() { add_menu_page('Flicker','Flicker','add_users','fck_admin','consult_flickr_api_so_7173971','http: /i.stack.imgur/s2ons.png', 2 ); }); function consult_flickr_api_so_7173971() { $ api_key ='YOUR-KEY'; $ secret_key ='YOUR-SECRET'; //不需要公开数据 $ uid ='USER-ID-TO-CONSULT'; $ url ='api.flickr/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']); } } } / ** www.flickr/services/api/ 有用的方法 - getExif - getInfo - getSizes * / function get_flickr_photo_so_7173971($ photo_id,$ method ='getSizes') { $ api_key ='YOUR-KEY'; $ flickr = wp_remote_get('api.flickr/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; } } //没有找到中等大小,只打印第一个 if($ no_print) { print_photo_so_7173971($ flickr_array [ 'sizes'] ['size'] [0]); } } } 函数print_photo_so_7173971($ size) { printf('< img src = %swidth =%sheight =%s/>< br />', $ size ['source'], $ size ['width'] , $ size ['height'] ); }

我将把这些结果的操作留给读者。 >

建议:

  • 设置cron作业以提取信息并执行操作。使用此参考:网站小部件以显示Apple App商店价格下跌?
  • 创建一个选项页面,并显示缩略图,扩展信息和按钮的结果以分派一些操作(镜像端加载,创建新文章等),请参阅此处和此处。

WordPress StackExchange中充满了不错的片段和技巧。

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.

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.

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.

解决方案

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', 'i.stack.imgur/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 = 'api.flickr/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'] ); } } } /** www.flickr/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( 'api.flickr/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.

Suggestions:

  • 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.

WordPress StackExchange is full of nice snippets and techniques.

更多推荐

将JSON或XML API响应保存到Wordpress数据库

本文发布于:2023-10-29 13:09:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539780.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   XML   JSON   Wordpress   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!