0

找到了这个方便的小脚本,它使用 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;
}

}

4

1 回答 1

1

$this->ch= Curl Handle,它将保存 curl 请求句柄的属性。它是私有的,因为它不会在课堂外使用。

$this->execute()= 是执行 curl 请求并返回结果的类方法。它是私有的,因为它不会在课堂外使用。

两者都是类的一部分,而不是 PHP 内部的一部分。

还:

我看到提供的代码存在一些问题:

  • $this->my-site.com/xmlrpc.php = $endpoint;应该 $this->endpoint = $endpoint;
  • $this->1 = $blogid;应该$this->blogid = $blogid;

加上在方法中更改对它们属性的引用publish_post()

固定代码:

<?php 
/*Usage:*/
$wordpress = new WordPress($username, $password, 'my-site.com/xmlrpc.php', 1);
$wordpress->publish_post(...);


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->endpoint   = $endpoint;
        $this->blogid     = $blogid;

        $this->ch = curl_init($this->endpoint);
        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->blogid,
            $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;
    }

}
?>

希望能帮助到你

于 2013-10-29T14:46:14.590 回答