简体   繁体   English

在Wordpress中将第三方Web服务API实现为“页面”

[英]Implement 3rd party Web Service API as 'Pages' in Wordpress

I'm using building out a section of a Wordpress site that uses an API built on top of an older database that needs to remain in tact. 我正在构建Wordpress网站的一部分,该部分使用建立在需要保持完好的旧数据库之上的API。 The API is really simple and will be accessed only in two ways: 该API非常简单,只能通过两种方式访问​​:

mysite.com/monkeys/     <-- This corresponds to calling the API as '/api/monkeys/list'
mysite.com/monkey/william    <-- Calling the API as '/api/monkeys/:name'

Because the data from the API is controlled by a 3rd party and does not need to be managed by Wordpress, I'm wondering how best I can build this out so that Wordpress doesn't throw 404s/redirect when I try to hit these urls. 因为来自API的数据是由第三方控制的,并且不需要由Wordpress进行管理,所以我想知道如何最好地构建它,以使当我尝试访问这些url时Wordpress不会抛出404 /重定向。

Furthermore, if I decide to cache these calls (the API is clunky and slow) how might I integrate them into a Wordpress search? 此外,如果我决定缓存这些调用(API笨拙且缓慢),我如何将它们集成到Wordpress搜索中?

Please let me know if I can clarify anything - thanks! 请让我知道是否可以澄清任何事情-谢谢!

You can add an endpoint . 您可以添加一个端点 The following example only detects endpoints on the root ( EP_ROOT ). 以下示例仅检测根( EP_ROOT )上的端点。 If we visit example.com/monkeys/ it's considered your /api/monkeys/list . 如果我们访问example.com/monkeys/则将其视为您的/api/monkeys/list And when visiting example.com/monkeys/name it's your /api/monkeys/:name . 当访问example.com/monkeys/name它就是您的/api/monkeys/:name

If you really want to separate a monkey from the band, add another endpoint. 如果您真的想将猴子与乐队分开,请添加另一个端点。

add_action( 'init', function()
{
    add_rewrite_endpoint( 'monkeys', EP_ROOT );
});

add_filter( 'query_vars', function( $vars )
{
    $vars[] = 'monkeys';
    return $vars;
});

This code requires refreshing the permalinks manually in Settings >> Permalinks >> Save , but it can be auto refreshed on plugin activation . 此代码需要在Settings >> Permalinks >> Save手动刷新永久Settings >> Permalinks >> Save ,但是可以在激活插件时自动刷新

To test the endpoint, I used this: 为了测试端点,我使用了以下命令:

add_action( 'wp_footer', function(){
    // set default as 'empty', so we can differentiate the root from no query var
    $monkeys = get_query_var( 'monkeys','empty' ); 
    if( 'empty' === $monkeys ) {
        printf( '<h1 style="font-size:4em">%s</h1>', 'NOT SET' );
    }
    else if( '' === $monkeys ) {
        printf( '<h1 style="font-size:4em">%s</h1>', 'Endpoint root.' );
    } 
    else {
        printf( '<h1 style="font-size:4em">Sent query: %s</h1>', urldecode( $monkeys ) );
    }
});

Why don't you just create those resources ... they won't be 404 then ... 你为什么不只是创建这些资源......他们不会是404,然后 ...

We've used this approach successfully on OMBE; 我们已经在OMBE上成功使用了这种方法。 we simply created scripts (in your case they'd be "monkey" and "monkeys") which are wrappers for something else (like the API in your case) and used an Apache ForceType directive (pointing to application/x-httpd-php5) to handle them. 我们只是创建了脚本(在您的情况下为“ monkey”和“ monkeys”),将它们包装为其他内容(例如您的情况下的API),并使用了Apache ForceType指令(指向application / x-httpd-php5 )来处理它们。

HTH, HTH,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM