3

我正在使用 Wordpress XML-RPC 自动发布到我的博客,并且我使用 PHP 获得了这两个函数:wp.newPost 和 wp.uploadFile。

但是,当我在一个 php 脚本中运行它们时,如下所示:(仅包括重要部分)当我尝试发布时,Wordpress 没有从 wp.uploadFile 检测到 attachment_id,即使 attachment_id 存在。

//wp.newPost content
$content = array(
'post_title' => $title,
'post_content' => $body,
'post_status' => 'publish', 
'post_type' => 'products',
'post_thumbnail' = // wp.uploadFile is called here that returns attachement id
);

$this->Curl->post($url,$content);

当我尝试运行上面的代码时,我得到:“faultCode 404 faultString Invalid attachment ID。”

我确认 wp.uploadFile 图像已成功上传并显示在 Wordpress 库中。事实上,如果我再次运行该脚本并将“post_thumbnail”值替换为 wp.uploadFile 返回的完全相同的 attachement_id,它就可以工作!

因此,如果我如上图所示同时运行这两个函数,Wordpress 显然不会检测到图像已上传。有什么解决办法吗?我真的很讨厌将 Wordpress 附件 ID 存储在我自己的数据库中。

4

1 回答 1

2

通过 xmlrpc 发布我正在使用 IXR

require_once("IXR_Library.php.inc");

以下是我正在使用的;它肯定需要一些编辑,但可能会给你一些线索

上传新文件:

$file_upload_path = get_post_meta ( $image->ID,'_wp_attached_file', false);
$file_path = $_SERVER['DOCUMENT_ROOT']."/dev/0.2/wp-content/uploads/".$file_upload_path[0];
$path_parts = pathinfo($file_path);
$file_name = $path_parts['filename'].'.'.$path_parts['extension'];
$data = array(
  'name'  => $file_name ,
  'type'  => $image->post_mime_type ,
  'bits'  => new IXR_Base64( file_get_contents( $file_path ) ) ,
  'overwrite' => true
);
$status = $rpc->query(
    'wp.uploadFile', 
    '1',
    $username,
    $password,
    $data
);
$image_returnInfo = $rpc->getResponse();

创建一个新帖子:

$data = array(
    'post_title' => $post->post_title,
    'post_content' => $post->post_content . $attachement_string_hack,
    'post_type' => 'product',
    'post_status' => 'publish',
    'dateCreated' => (new IXR_Date(time())),
    'post_thumbnail' => $image_returnInfo['id']
);
$rpc = new IXR_Client( $rpc_url );
$res = $rpc->query('wp.newPost', '', $username, $password, $data, 0);

希望这可以帮助!(我现在坚持将图片附加到帖子中)

于 2012-12-10T09:16:26.437 回答