如何使用 XMLRPC 在 WordPress 中创建附有照片的新帖子?
有人知道如何使用 XMLRPC 在 WordPress 中创建附有照片的新帖子吗?
我可以单独创建新帖子和上传新图片,但似乎无法将上传的照片附加到创建的帖子中?
I am able to create new post and upload new picture separately, but looks like there is no way to attach the uploaded photo to the created post?
以下是我目前使用的代码.
Below is the codes I'm currently using.
<?php
DEFINE('WP_XMLRPC_URL', 'http://www.blog.com/xmlrpc.php');
DEFINE('WP_USERNAME', 'username');
DEFINE('WP_PASSWORD', 'password');
require_once("./IXR_Library.php");
$rpc = new IXR_Client(WP_XMLRPC_URL);
$status = $rpc->query("system.listMethods"); // method name
if(!$status){
print "Error (".$rpc->getErrorCode().") : ";
print $rpc->getErrorMessage()."
";
exit;
}
$content['post_type'] = 'post'; // post title
$content['title'] = 'Post Title '.date("F j, Y, g:i a"); // post title
$content['categories'] = array($response[1]['categoryName']); // psot categories
$content['description'] = '<p>Hello World!</p>'; // post body
$content['mt_keywords'] = 'tag keyword 1, tag keyword 2, tag keyword 3'; // post tags
$content['mt_allow_comments'] = 1; // allow comments
$content['mt_allow_pings'] = 1; // allow pings
$content['custom_fields'] = array(array('key'=>'Key Name', 'value'=>'Value One')); // custom fields
$publishBool = true;
if(!$rpc->query('metaWeblog.newPost', '', WP_USERNAME, WP_PASSWORD, $content, $publishBool)){
die('An error occurred - '.$rpc->getErrorCode().":".$rpc->getErrorMessage());
}
$postID = $rpc->getResponse();
echo 'POST ID: '.$postID.'<br/>';
if($postID){ // if post has successfully created
$fs = filesize(dirname(__FILE__).'/image.jpg');
$file = fopen(dirname(__FILE__).'/image.jpg', 'rb');
$filedata = fread($file, $fs);
fclose($file);
$data = array(
'name' => 'image.jpg',
'type' => 'image/jpg',
'bits' => new IXR_Base64($filedata),
false // overwrite
);
$status = $rpc->query(
'metaWeblog.newMediaObject',
$postID,
WP_USERNAME,
WP_PASSWORD,
$data
);
echo print_r($rpc->getResponse()); // Array ( [file] => image.jpg [url] => http://www.blog.com/wp-content/uploads/2011/09/image.jpg [type] => image/jpg )
}
?>
我参与了 WordPress 网站(我现在的雇主使用了其中的 3 个)并且每天发布大量内容迫使我使用我所做的最好的——脚本!
I've been involved in WordPress sites (my current employer uses 3 of these) and posting stuff daily and by the bulk has forced me to use what I do best-- scripts!
它们是基于 PHP 的,使用和部署起来既快捷又方便.和安全?只需使用 .htaccess 来保护它.
They're PHP-based and are quick and easy to use and deploy. And security? Just use .htaccess to secure it.
根据研究,XMLRPC 在文件方面是 wordpress 真正糟糕的一件事.上传文件后,您无法将该附件与特定帖子相关联!我知道,这很烦人.
As per research, XMLRPC when it comes to files is one thing wordpress really sucks at. Once you upload a file, you can't associate that attachment to a particular post! I know, it's annoying.
所以我决定自己解决这个问题.我花了一个星期来整理它.您需要 100% 控制符合 XMLRPC 的发布客户端,否则这对您没有任何意义!
So I decided to figure it out for myself. It took me a week to sort it out. You will need 100% control over your publishing client that is XMLRPC compliant or this won't mean anything to you!
您将需要,从您的 WordPress 安装:
You will need, from your WordPress installation:
- class-IXR.php,位于/wp-admin/includes
- class-wp-xmlrpc-server.php,位于/wp-includes
如果您像我一样制作自己的发布工具,则需要 class-IXR.php.他们有正常工作的 base64 编码器.不要相信 PHP 自带的那个.
class-IXR.php will be needed if you craft your own posting tool, like me. They have the correctly-working base64 encoder. Don't trust the one that comes with PHP.
您还需要有一定的编程经验才能与此相关.我会尽量说得更清楚.
You also need to be somewhat experienced in programming to be able to relate to this. I will try to be clearer.
修改 class-wp-xmlrpc-server.php
Modify class-wp-xmlrpc-server.php
- 通过 ftp 将其下载到您的计算机上.备份一份,以防万一.
- 在文本编辑器中打开文件.如果它没有格式化,(通常应该,否则,他们使用的是 unix 类型的换行符)在其他地方打开它或使用类似 Ultraedit 的东西.
- 注意
mw_newMediaObject
函数.这是我们的目标.这里有一点注意;WordPress 借用了博主和移动字体的功能.尽管 WordPress 也有一个独特的 xmlrpc 类集,但它们选择保持功能通用,以便无论使用什么平台都能正常工作. - 查找函数
mw_newMediaObject($args)
.通常,这应该在第 2948 行.注意您的文本编辑器的状态栏以查找您所在的行号.如果您仍然找不到它,请使用文本编辑器的搜索/查找功能查找它. 向下滚动一点,您应该会看到如下所示的内容:
- Download this to your computer, through ftp. Backup a copy, just in case.
- Open the file in a text editor. If it doesn't come formatted, (typically it should, else, it's unix-type carriage breaks they are using) open it elsewhere or use something like ultraedit.
- Pay attention to the
mw_newMediaObject
function. This is our target. A little note here; WordPress borrows functionality from blogger and movabletype. Although WordPress also has a unique class sets for xmlrpc, they choose to keep functionality common so that they work no matter what platform is in use. - Look for the function
mw_newMediaObject($args)
. Typically, this should be in line 2948. Pay attention to your text editor's status bar to find what line number you are in. If you can't find it still, look for it using the search/find function of your text editor. Scroll down a little and you should have something that looks like this:
$name = sanitize_file_name( $data['name'] );
$type = $data['type'];
$bits = $data['bits'];
在 $name 变量之后,我们将添加一些东西.见下文.
After the $name variable, we will add something. See below.
$name = sanitize_file_name( $data['name'] );
$post = $data['post']; //the post ID to attach to.
$type = $data['type'];
$bits = $data['bits'];
注意新的 $post 变量.这意味着每当您提出新的文件上传请求时,您现在都可以附加一个post"参数.
Note the new $post variable. This means whenever you will make a new file upload request, a 'post' argument will now be available for you to attach.
如何找到您的帖子编号取决于您如何使用符合 xmlrpc 的客户端添加帖子.通常,您应该通过发布获得此信息.它是一个数值.
How to find your post number depends on how you add posts with an xmlrpc-compliant client. Typically, you should obtain this as a result from posting. It is a numeric value.
在您编辑完以上内容后,是时候转到第 3000 行了.
Once you've edited the above, it's time to move on to line 3000.
// Construct the attachment array
// attach to post_id 0
$post_id = 0;
$attachment = array(
'post_title' => $name,
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $post_id,
'post_mime_type' => $type,
'guid' => $upload[ 'url' ]
);
这就是为什么没有图片与任何帖子相关联的原因!post_parent 参数始终默认为 0!以后不会了.
So here's why no image is associated to any post! It is always defaulted to 0 for the post_parent argument! That's not gonna be the case anymore.
// Construct the attachment array
// attach to post_id 0
$post_id = $post;
$attachment = array(
'post_title' => $name,
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $post_id,
'post_mime_type' => $type,
'guid' => $upload[ 'url' ]
);
$post_id 现在占用了 $post 的值,它来自 xmlrpc 请求.一旦将其提交给附件,它将与您想要的任何帖子相关联!
$post_id now takes up the value of $post, which comes from the xmlrpc request. Once this is committed to the attachment, it will be associated to whatever post you desire!
这可以改进.可以分配一个默认值,这样如果没有输入值,事情就不会被破坏.虽然在我这边,我将默认值放在我的客户端上,除了我之外没有其他人访问 XMLRPC 接口.
This can be improved. A default value can be assigned so things don't get broken if no value is entered. Although in my side, I put the default value on my client, and no one else is accessing the XMLRPC interface but me.
更改完成后,保存您的文件并将其重新上传到您找到它的同一路径中.再次提醒,一定要做好备份.
With the changes done, save your file and re-upload it in the same path where you found it. Again, make sure to make backups.
警惕影响此模块的 WordPress 更新.如果发生这种情况,您需要再次重新应用此修改!
Be wary of WordPress updates that affects this module. If that happens, you need to reapply this edit again!
在您的 PHP 类型编辑器中包含 class-IXR.php.如果您正在使用其他东西,那么我无法帮助您.:(
Include class-IXR.php in your PHP-type editor. If you're using something else, well, I can't help you there. :(
希望这对一些人有所帮助.
Hope this helps some people.