找到了这个方便的小脚本,它使用 Curl 和 PHP 来使用 Wordpress XML-RPC 函数直接发布到我的 Wordpress 博客。我想我已经知道在哪里输入大多数信息,但是有两个值我就是想不通(到目前为止,谷歌搜索也没有)。
下面我放了整个脚本,其他人可能会使用 - 由http://blog.artooro.com/2012/09/03/wordpress-api-xml-rpc-new-easy-to-use-php-class/提供
我无法弄清楚的两个值是“ch”和“execute”。不确定这是 Curl 值还是 PHP 值。
class WordPress {
private $username;
private $password;
private $endpoint;
private $blogid;
private $ch;
public function __construct($username, $password, $endpoint, $blogid = 1) {
$this->myusername = $username;
$this->mypassword = $password;
$this->my-site.com/xmlrpc.php = $endpoint;
$this->1 = $blogid;
$this->ch = curl_init($this->my-site.com/xmlrpc.php);
curl_setopt($this->ch, CURLOPT_HEADER, false);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
}
private function execute($request) {
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($this->ch);
$result = xmlrpc_decode($response);
if (is_array($result) && xmlrpc_is_fault($result)) {
throw new Exception($result['faultString'], $result['faultCode']);
}
else {
return $result;
}
}
public function publish_post($title, $content, array $tags, array $categories, $status = 'publish', $date = Null) {
// Set datetime for post
if ($date == Null) {
$post_date = date("Ymd\TH:i:s", time());
}
else {
$post_date = $date;
}
xmlrpc_set_type($post_date, 'datetime');
$params = array(
$this->id,
$this->myusername,
$this->mypassword,
array(
'post_type' => 'post',
'post_status' => $status,
'post_title' => $title,
'post_content' => $content,
'post_date' => $post_date,
'terms_names' => array('category' => $categories, 'post_tag' => $tags)
)
);
$request = xmlrpc_encode_request('wp.newPost', $params);
$response = $this->execute($request);
return $response;
}
}