P.C*_*.C. 5 php wordpress xml-rpc attachment metaweblog
任何人都知道如何使用XMLRPC创建附加在WordPress中的照片的新帖子?
我可以分别创建新帖子并上传新图片,但看起来无法将上传的照片附加到创建的帖子上?
以下是我目前使用的代码.
<?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()."\n";
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 )
}
?>
Run Code Online (Sandbox Code Playgroud)
小智 9
我参与过WordPress网站(我现在的雇主使用其中的3个)并且每天都发布内容并且大量使用我强制使用我最擅长的东西 - 脚本!
它们基于PHP,易于使用和部署.和安全?只需使用.htaccess即可保护它.
根据研究,XMLRPC在文件方面是wordpress非常糟糕的一件事.上传文件后,您无法将该附件与特定帖子相关联!我知道,这很烦人.
所以我决定自己解决这个问题.我花了一个星期的时间来解决它.您将需要100%控制符合XMLRPC的发布客户端,否则这对您没有任何意义!
您需要从WordPress安装:
如果您像我一样制作自己的发布工具,则需要class-IXR.php.它们具有正确工作的base64编码器.不要相信PHP附带的那个.
您还需要在编程方面有一定的经验才能与此相关.我会尽量更清楚.
修改class-wp-xmlrpc-server.php
mw_newMediaObject功能.这是我们的目标.这里有一点点说明; WordPress借用博客和移动类型的功能.尽管WordPress还为xmlrpc提供了一个独特的类集,但他们选择保持功能的通用性,以便无论使用何种平台,它们都能正常工作.mw_newMediaObject($args).通常,这应该在第2948行.注意文本编辑器的状态栏以查找您所在的行号.如果您仍然找不到它,请使用文本编辑器的搜索/查找功能查找它.向下滚动一点,你应该看到这样的东西:
$name = sanitize_file_name( $data['name'] );
$type = $data['type'];
$bits = $data['bits'];
Run Code Online (Sandbox Code Playgroud)在$ name变量之后,我们将添加一些内容.见下文.
$name = sanitize_file_name( $data['name'] );
$post = $data['post']; //the post ID to attach to.
$type = $data['type'];
$bits = $data['bits'];
Run Code Online (Sandbox Code Playgroud)
注意新的$ post变量.这意味着无论何时您发出新的文件上传请求,现在都可以使用'post'参数进行附加.
如何查找您的帖子编号取决于您如何使用符合xmlrpc的客户端添加帖子.通常,您应该通过发布获得此结果.这是一个数值.
编辑完上述内容后,就可以继续使用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' ]
);
Run Code Online (Sandbox Code Playgroud)所以这就是为什么没有图片与任何帖子相关联的原因!对于post_parent参数,它总是默认为0!那不会再是这种情况了.
// 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' ]
);
Run Code Online (Sandbox Code Playgroud)
$ post_id现在占用$ post的值,该值来自xmlrpc请求.一旦将其提交给附件,它将与您想要的任何帖子相关联!
这可以改进.可以分配默认值,这样如果没有输入值,事情就不会被破坏.虽然在我这边,我把默认值放在我的客户端,没有其他人访问XMLRPC接口,但我.
完成更改后,保存文件并将其重新上传到找到它的路径中.再次确保进行备份.
警惕影响此模块的WordPress更新.如果发生这种情况,您需要再次重新应用此编辑!
在PHP类型编辑器中包含class-IXR.php.如果你正在使用其他东西,那么,我无法帮助你.:(
希望这可以帮助一些人.
下面是一些示例代码,用于附加 WordPress 不支持的路径中的图像 (wp-content)
<?php
function attach_wordpress_images($productpicture,$newid)
{
include('../../../../wp-load.php');
$upload_dir = wp_upload_dir();
$dirr = $upload_dir['path'].'/';
$filename = $dirr . $productpicture;
# print "the path is : $filename \n";
# print "Filnamn: $filename \n";
$uploads = wp_upload_dir(); // Array of key => value pairs
# echo $uploads['basedir'] . '<br />';
$productpicture = str_replace('/uploads','',$productpicture);
$localfile = $uploads['basedir'] .'/' .$productpicture;
# echo "Local path = $localfile \n";
if (!file_exists($filename))
{
echo "hittade inte $filename !";
die ("no image for flaska $id $newid !");
}
if (!copy($filename, $localfile))
{
wp_delete_post($newid);
echo "Failed to copy the file $filename to $localfile ";
die("Failed to copy the file $filename to $localfile ");
}
$wp_filetype = wp_check_filetype(basename($localfile), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($localfile)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $localfile, $newid );
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $localfile );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12674 次 |
| 最近记录: |