Wordpress - 如何使 URL 返回具有自定义内容类型的动态 JSON? [英] Wordpress - How does one make a URL return dynamic JSON with custom Content Type?

查看:50
本文介绍了Wordpress - 如何使 URL 返回具有自定义内容类型的动态 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先了解一些背景知识,我正在尝试按照此处的说明进行操作:

https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html#//apple_ref/doc/uid/TP40016308-CH12-SW1

获取适用于我的 iOS 设备网站的通用链接.我的 wordpress 站点处于共享托管环境中,因此我无权访问服务器或任何根文件.我将该文件放在我的网站 (httpdocs) 的根目录下,当我导航到该页面时,它不会加载.在谷歌搜索中,我发现了一个 stackoverflow 帖子,说明你必须配置服务器,让它知道提供内容类型为application/pkcs7-mime"的文件(使用 wordpress 配置 apple-app-site-association 文件).如前所述,我无权访问.

我有一个子域,它是一个我非常熟悉的 microsoft web api 项目.我为apple-app-site-association"创建了一个端点,并在代码中动态构建 json 响应并设置内容类型并返回它.这最终奏效了,Apple 检测到了它以及一切.这很有趣,因为我不需要服务器上的文件,因为我是即时生成的,而且我不需要在服务器上进行任何更改.

然而,问题是我需要主站点域作为通用链接,而不是 Windows 子域.主要站点是wordpress.我在想是否可以在 wordpress 中做同样的事情,在那里我导航到 mydomain.com/apple-app-site-association 并即时生成 json 和 content-type 并提供它.

我从来没有用 wordpress 写过任何代码,甚至不知道从哪里开始.

如果可能,我希望在以下方面有明确的方向:

使 mydomain.com/apple-app-site-association 指向提供 json 的自定义函数或页面.拥有使 json 动态提供的代码.具有将响应的内容类型设置为application/pkcs7-mime"的代码.当然,如果有人知道如何解决在没有这种方法的情况下无法提供共享服务器的文件,我也对此持开放态度.据我所知,这可能是假设可以做到的唯一方法.

解决方案

注意:我假设 WordPress 安装在根文件夹中.

因此,如果您希望 http://example.com/apple-app-site-association 提供 JSON 内容,例如 this 并将内容类型(Content-Type 标头)设置为 application/pkcs7-mime,这里有一些您可以选择的选项选择:

动态内容

因为您使用的是 WordPress,这对您来说可能比手动编辑 .htaccess 文件更好.

你可以使用parse_request钩子;这样,您不需要任何自定义 (WordPress) 重写规则,不需要自定义页面(page 的帖子类型),也不需要创建任何 JSON 文件.>

并确保根文件夹中没有名为 apple-app-site-association 的文件,或具有 slug apple-app-site-association<的 WordPress 页面/code>.

所以这将进入您的主题功能文件(例如 wp-content/themes/your-theme/functions.php):

<?phpadd_action( 'parse_request', 'serve_apple_app_site_association', 0 );函数 serve_apple_app_site_association( $wp ) {//检查请求是否为/apple-app-site-associationif ('apple-app-site-association' !== $wp->request ) {返回;}//JSON 数据的数组版本.$数据=数组('应用链接' =>大批('应用程序' =>大批(),'详细信息' =>大批(大批('appID' =>'9JA89QQLNQ.com.apple.wwdc','路径' =>大批('/WWDC/新闻/','/videos/wwdc/2015/*',),),大批('appID' =>'ABCD1234.com.apple.wwdc','路径' =>大批('*',),),),),);//发送标题.status_header(200);nocache_headers();header('内容类型:应用程序/pkcs7-mime');//并提供 JSON 数据.回声 wp_json_encode( $data );出口;}

静态内容

  1. 将 JSON 数据放在名为 apple-app-site-association(即无扩展名)的文件中,并将文件保存在您可以看到 wp 的根文件夹中-config.php.htaccess 文件.

  2. 将此添加到您的 .htaccess 文件中:

<文件 apple-app-site-association>标题集 Content-Type application/pkcs7-mime</文件>

如果 和/或 Header 不适合/不适合您,那么您可以使用上面的第一个选项,但使用类似 readfile() 之类的东西来读取静态文件.

或者(特别是如果您的站点不支持/不支持 URL 重写),您可以在根文件夹中创建一个名为 apple-app-site-association 的文件夹并添加 index.php 到该文件夹​​和该文件:

<?phpheader('内容类型:应用程序/pkcs7-mime');//读取静态文件或仅生成动态内容.@readfile( __DIR__ . '/apple-app-site-association.json' );出口;

Some background first, I'm trying to follow the directions here:

https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html#//apple_ref/doc/uid/TP40016308-CH12-SW1

to get universal links working with my website for iOS devices. My wordpress site is in a shared hosting environment, therefore I do NOT have access to the server or any root files. I placed the file at the root of my site (httpdocs) and when I navigate to the page it does not load. In googling around, I found a stackoverflow post stating you had to config the server to let it know to serve up that file with a content type of "application/pkcs7-mime" (Config apple-app-site-association file with wordpress). As mentioned I do not have access to that.

I have a subdomain which is a microsoft web api project that I am very familiar with. I made an endpoint for "apple-app-site-association" and dynamically built the json response in code and set the content type and returned it. This ended up working and Apple detects it and everything. This is interesting as I do NOT need a file on my server as I generate it on the fly, and I don't need to change anything server wise.

The issue however is I need the main site domain to be the universal link, not the windows subdomain. The main site is wordpress. I'm thinking if may be possible to do the same thing in wordpress where I navigate to mydomain.com/apple-app-site-association and I generate the json and content-type on the fly and serve it up.

I've never done any coding with wordpress and don't even know where to start.

If possible I would like clear direction in the following:

Making mydomain.com/apple-app-site-association lead to a custom function or page that serves up json. Have code that makes the json get served up dynamically. Have code that sets the content-type of the response to "application/pkcs7-mime". Of course if someone knows how to solve the file not being served no a shared server without this method, I'm open to that as well. From what I can see, this may be the only way assuming this can be done.

解决方案

Note: I'm assuming WordPress is installed in the root folder.

So if you want http://example.com/apple-app-site-association to serve a JSON content like this and have the content type (Content-Type header) set to application/pkcs7-mime, here are some options you can choose from:

Dynamic Content

Because you're using WordPress, this might be a better option for you than manually editing the .htaccess file.

You can use the parse_request hook; this way, you don't need any custom (WordPress) rewrite rules, no need for a custom Page (post type of page), and you don't need to create any JSON file.

And make sure there's no file named apple-app-site-association in the root folder, or a WordPress Page having the slug apple-app-site-association.

So this would go in your theme functions file (e.g. wp-content/themes/your-theme/functions.php):

<?php
add_action( 'parse_request', 'serve_apple_app_site_association', 0 );
function serve_apple_app_site_association( $wp ) {
    // Check if the request is /apple-app-site-association
    if ( 'apple-app-site-association' !== $wp->request ) {
        return;
    }

    // Array version of the JSON data.
    $data = array(
        'applinks'    => array(
            'apps'    => array(),
            'details' => array(
                array(
                    'appID' => '9JA89QQLNQ.com.apple.wwdc',
                    'paths' => array(
                        '/wwdc/news/',
                        '/videos/wwdc/2015/*',
                    ),
                ),
                array(
                    'appID' => 'ABCD1234.com.apple.wwdc',
                    'paths' => array(
                        '*',
                    ),
                ),
            ),
        ),
    );

    // Send headers.
    status_header( 200 );
    nocache_headers();
    header( 'Content-Type: application/pkcs7-mime' );

    // And serve the JSON data.
    echo wp_json_encode( $data );
    exit;
}

Static Content

  1. Place the JSON data in a file named apple-app-site-association (i.e. no extension) and save the file in the root folder where you could see the wp-config.php and .htaccess files.

  2. Add this to your .htaccess file:

<Files apple-app-site-association>
        Header set Content-Type application/pkcs7-mime
</Files>

If the <Files> and/or Header don't/doesn't work for you, then you could just use the first option above, but use something like readfile() to read the static file.

Alternatively (and specifically if URL rewriting is not available/supported on your site), you could create a folder named apple-app-site-association in the root folder and add index.php to that folder and in that file:

<?php
header( 'Content-Type: application/pkcs7-mime' );
// Read the static file or just generate a dynamic content.
@readfile( __DIR__ . '/apple-app-site-association.json' );
exit;

这篇关于Wordpress - 如何使 URL 返回具有自定义内容类型的动态 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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