首页 > 解决方案 > wp_remote_post() 函数的 php 致命错误

问题描述

我尝试在 wordpress 中远程添加一个帖子条目(来自我的 wordpress 网站之外的网站)。这是我使用的代码:

<?php
$url = 'https://mywpsite.com/wp-json/wp/v2/posts';

$user = 'wpuser';
$pass = 'xxxx yyyy xxxx yyyy'; //Password for use with Application Passwords Plugin

$headers = [
    'Authorization' => 'Basic ' . base64_encode( $user . ':' . $pass ),
    'cache-control' => 'no-cache',
];

$body = [
    'status'  => 'draft',
    'type'    => 'post',
    'title'   => 'Title of my Post',
    'content' => 'My Content',
];

$args = [
    'headers'     => $headers,
    'body'        => $body,
    'method'      => 'POST',
    'timeout'     => 45,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking'    => true,
    'cookies'     => [],
];

$response = wp_remote_post( $url, $args );

if ( is_wp_error( $response ) ) {
    print_r( $response->get_error_message() );
}
?>

提供商的 Web 服务器上的错误日志显示:“PHP 致命错误:未捕获的错误:调用未定义的函数 wp_remote_post() in ...” 我检查了 php.ini -> cURL 支持已启用,allow_url_fopen 已打开。我还在 .htaccess 中添加了重写规则。关于检查或做什么的任何想法?

标签: phpwordpresswordpress-rest-api

解决方案


推荐阅读