简体   繁体   English

WordPress:通过 Rest API 从其他站点获取具有 BasicAuth 保护的帖子

[英]WordPress: Get posts from other site with BasicAuth protection via Rest API

I want to get post from a site with BasicAuth protection.我想从具有 BasicAuth 保护的网站上发布帖子。

To get the post from the site I'm using the following code ( from here ):要从该站点获取帖子,我使用以下代码( 来自此处):

// Disable direct file access.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Get posts via REST API.
 */
function get_posts_via_rest() {

    // Initialize variable.
    $allposts = '';
    
    // Enter the name of your blog here followed by /wp-json/wp/v2/posts and add filters like this one that limits the result to 2 posts.
    $response = wp_remote_get( 'https://www.sumydesigns.com/wp-json/wp/v2/posts?per_page=2' );

    // Exit if error.
    if ( is_wp_error( $response ) ) {
        return;
    }

    // Get the body.
    $posts = json_decode( wp_remote_retrieve_body( $response ) );

    // Exit if nothing is returned.
    if ( empty( $posts ) ) {
        return;
    }

    // If there are posts.
    if ( ! empty( $posts ) ) {

        // For each post.
        foreach ( $posts as $post ) {

            // Use print_r($post); to get the details of the post and all available fields
            // Format the date.
            $fordate = date( 'n/j/Y', strtotime( $post->modified ) );

            // Show a linked title and post date.
            $allposts .= '<a href="' . esc_url( $post->link ) . '" target=\"_blank\">' . esc_html( $post->title->rendered ) . '</a>  ' . esc_html( $fordate ) . '<br />';
        }
        
        return $allposts;
    }

}

It works.有用。 But the site I want to get the post from uses a BasicAuth protection.但是我想从中获取帖子的网站使用 BasicAuth 保护。 So there is no result.所以没有结果。

I read that the Rest API couldn't handle BasicAuth.我读到 Rest API 无法处理 BasicAuth。 And I have to use a plugin like the Basic Authentication handler而且我必须使用像基本身份验证处理程序这样的插件

But I'm not sure how to use it.但我不确定如何使用它。 Is there a way to integrate it in the code?有没有办法将它集成到代码中?

Within WordPress WP_Http are made through the wp_remote_get() function.在 WordPress 中, WP_Http是通过wp_remote_get() function 生成的。

Performs an HTTP request using the GET method and returns its response.使用 GET 方法执行 HTTP 请求并返回其响应。

In short wp_remote_get() act as a wrapper for WP_Http .简而言之wp_remote_get()充当WP_Http的包装器。

Performs an HTTP request using the GET method and returns its response.使用 GET 方法执行 HTTP 请求并返回其响应。

As per BasicAuth documentation:根据 BasicAuth 文档:

<?php

$args = array(
    'headers' => array(
        //here the credentials are referring to a wordpress account on the targeted website
        'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
    ),
);

wp_remote_get('https://...com/wp-json/wp/v2/posts', $args);
//...

You can definitely prevent api access (but that's not natively the case).您绝对可以阻止 api 访问(但本机并非如此)。

Keep in mind that, using BasicAuth will probably not be useful.请记住,使用 BasicAuth 可能没有用。 When people go for the hassle that is securing the api, usually it requires an admin access level to access the api.当人们 go 为保护 api 的麻烦而烦恼时,通常需要管理员访问级别才能访问 api。

An alternative would be to use a scraper like puppeteer to re-create a api from scratch and host it on a separate server.另一种方法是使用像 puppeteer 这样的刮板从头开始重新创建 api 并将其托管在单独的服务器上。 (sounds complicated but it's pretty easy) (听起来很复杂,其实很简单)

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

相关问题 如何通过REST-API获取WordPress中插件的自定义帖子? - How to get custom posts of plugin in WordPress by REST-API? 我可以显示从一个wordpress网站到另一个wordpress网站的所有帖子,并用单独的域作为所有帖子的前缀吗? - Can i show all posts from one wordpress site to other wordpress site with separate domain as prefix to all posts? 如何从wordpress上拉帖子到不同主机上的其他非wordpress网站? - how to pull posts from wordpress to other non-wordpress site on different hosting? 网站如何通过Facebook API在其不拥有的其他页面上发布公共消息(时间线/墙贴)? - How can a site post public messages (timeline/wall posts) on other Pages it does not own via the Facebook API? 如何通过 wordpress rest api 从自定义表中获取所有记录? - How do I get all records from custom table via wordpress rest api? 我如何使用WordPress REST API从我的角度应用程序中提取更多WordPress帖子 - How can I pull in more WordPress posts from my angular app, using the WordPress REST api 从另一个WordPress网站提取帖子 - Pulling posts from another WordPress site 显示来自另一个WordPress网站的帖子 - Show Posts from Another WordPress Site 从Wordpress博客中提取帖子到另一个站点 - Extract posts from wordpress blog to another site 如何通过 API 从 LinkedIn 获取公司帖子? - How to get company posts from LinkedIn via API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM