[英]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.