【问题标题】:Post to wordpress via php通过 php 发布到 wordpress
【发布时间】:2013-11-14 04:00:15
【问题描述】:

我正在尝试使用 php 在我的 wordpress 网站上发帖。

我使用 php 从网站获取数据并将所有数据存储在变量中。

我找到了一些自动 wordpress php 海报的代码,但它们有点复杂,我不确定如何使用/更改它们。

通过 php 最简单的方法是什么?

我的数据是这样的:

$topic_name = "名称";

$mainimage = "url/图片":

$input = "你好........" ;

$category = "汽车";

$tags = ("tag1","tag2","tag3"...);

注意:我只需要基本代码即可登录到我的 wordpress 并通过 php 发布一些随机文本 - 我很确定我以后可以弄清楚如何输入类别、标签等。

我正在尝试使用它,因为它看起来很简单,但我认为它不适用于最新版本的 wordpress (3.7.1) -

  • 我现在使用 xampp 在本地托管网站

如果有人可以修改它或可以分享一个可以工作的,那就太好了。

function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8') {
    $title = htmlentities($title,ENT_NOQUOTES,$encoding);
    $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

    $content = array(
        'title'=>$title,
        'description'=>$body,
        'mt_allow_comments'=>0,  // 1 to allow comments
        'mt_allow_pings'=>0,  // 1 to allow trackbacks
        'post_type'=>'post',
        'mt_keywords'=>$keywords,
        'categories'=>array($category)
    );
    $params = array(0,$username,$password,$content,true);
    $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_URL, $rpcurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    $results = curl_exec($ch);
    curl_close($ch);
    return $results;
}

【问题讨论】:

    标签: php wordpress post


    【解决方案1】:

    你真的需要使用 XML-RPC 吗?这通常是您发布到远程 WordPress 安装时想要执行的操作。例如,来自完全不同的网站、来自移动应用等。

    听起来您正在编写一个将在您的 WordPress 安装中运行的插件。在这种情况下,您可以直接致电wp_insert_post()

    来自WordPress's wiki 的一个非常简单的示例,其中还包含您可以使用的完整参数列表:

    // Create post object
    $my_post = array(
      'post_title'    => 'My post',
      'post_content'  => 'This is my post.',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_category' => array(8,39)
    );
    
    // Insert the post into the database
    wp_insert_post( $my_post );
    

    【讨论】:

    • 我实际上需要从不同的位置发布 _ wordpress 站点和脚本都是我的,所以如果可以使用更简单的代码在同一服务器上将它们合并在一起 - 我可能会这样做.例如:从我在不同位置的 php 文件 - 我可以访问 wordpress 位置中的 php 文件并传递数据 - 不太清楚如何做到这一点@Michael Hampton
    • 您只需要从其顶级目录include 安装的wp-load.php 文件,一切都应该正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2012-07-04
    • 2018-04-02
    相关资源
    最近更新 更多