如何使用自定义函数将数据插入wordpress表

时间:2014-04-07 10:08:58

标签: php sql wordpress azure

我在Azure上托管了一个wordpress应用程序,我需要使用自定义函数将数据插入到wp_posts表中,因为我将此脚本作为后台作业运行而且我不能使用wordpress函数,因为脚本是不在wordpress文件夹上。

我试过了:

mysql_query("INSERT INTO wp_posts(post_title, post_content, post_status, post_type, comment_status, page_template), VALUES($title, $sum, 'publish', 'post', 'closed', 'content.php')");

但是我收到以下错误: PHP警告:mysql_query():尝试以其访问权限禁止的方式访问套接字。

日志文件如下:

[04/07/2014 09:51:47 > adf9f5: SYS INFO] Status changed to Initializing
[04/07/2014 09:51:47 > adf9f5: SYS INFO] Run script 'data.php' with script host - 'PhpScriptHost'
[04/07/2014 09:51:47 > adf9f5: SYS INFO] Status changed to Running
[04/07/2014 09:51:48 > adf9f5: ERR ] PHP Warning:  mysql_query(): An attempt was made to access a socket in a way forbidden by its access permissions.
[04/07/2014 09:51:48 > adf9f5: ERR ]  in C:\DWASFiles\Sites\abacuskenya\Temp\jobs\triggered\y\rdksuocv.xbx\data.php on line 50
[04/07/2014 09:51:48 > adf9f5: ERR ] PHP Warning:  mysql_query(): A link to the server could not be established in   C:\DWASFiles\Sites\abacuskenya\Temp\jobs\triggered\y\rdksuocv.xbx\data.php on line 50
[04/07/2014 09:51:48 > adf9f5: ERR ] PHP Warning:  mysql_insert_id(): An attempt was made to access a socket in a way forbidden by its access permissions.

如何使用除wordpress以外的自定义函数将脚本插入到该表中?wp_insert_post

我的data.php如下:

<?php
define('DB_NAME', '**********');
define('DB_USER', '********');
define('DB_PASSWORD', '**************');
define('DB_HOST', '*******');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link){
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if(!$db_selected){
    die('Cannot use '. DB_NAME . ': ' .mysql_error());
}
$pages = array("page0", "page1", "page2", "page3", "page4");
    foreach($pages as $page){
        $json_feed = "http://digitalrand.net/api/url_data/?key=*****&pass=*****%&".$page;
        $json = file_get_contents($json_feed);
        $obj = json_decode($json, true);
            foreach($obj as $article_array){

            $url = $article_array['url'];
            $domain = $article_array['domain'];
            $favicon = $article_array['favicon'];
            $title = $article_array['title'];
            $category = $article_array['category'];
            $large_summary = $article_array['summary'];
            $sum = implode(',',$large_summary);
            $images = $article_array['images'];

            $image_array = reset($images);
            $image = $image_array["image"];

            $sql = "INSERT INTO wp_posts(post_title, post_content, post_status, post_type, comment_status, page_template), VALUES($title, $sum, 'publish', 'post', 'closed', 'content.php')";
            echo $sql;

            mysql_query($sql);

            $post_id = mysql_insert_id();
            echo "The Post ID is " . $post_id . "<br>";

            $data = array($favicon, $domain, $image);

            $value = implode(',',$data);


            //$a = add_post_meta($post_id, 'post_favicon_domain_image', $value, true);
            $sql2 = "INSERT INTO wp_postmeta(post_id, meta_key, meta_value), VALUES($post_id, 'post_favicon_domain_image', $value)";
            mysql_query($sql2);
            //echo $a;
            }
    }

2 个答案:

答案 0 :(得分:5)

您应该可以使用$ wpdb。

本文介绍如何从WordPress构建之外访问wordpress数据库。

  

http://www.stormyfrog.com/using-wpdb-outside-wordpress/

然后您可以使用$ wpdb在自定义函数中执行SQL。

<?php 
  global $wpdb;

  $table = 'wp_posts';

  $data  = array(post_title=>$post_title, post_content=>$post_content, post_status=>$post_status, post_type=>$post_type, comment_status=>$comment_status, page_template=>$page_template);

  $wpdb->insert( $table, $data);
?> 

WPDB参考:

  

https://codex.wordpress.org/Class_Reference/wpdb

答案 1 :(得分:1)

我建议您使用WP API在wordpress上创建任何内容。它可以在任何你想要的地方。如果手动将数据插入到wp db,则系统可能无法正常工作。例如,当您创建帖子时,它会发送一封电子邮件。如果手动插入db,则不起作用。而不是那样,你可以使用如下的WP API;

class WPClient {

    var $xmlRpcUrl = "";
    var $username  = "";
    var $password = "";

    public function __construct($xmlrpcurl, $username, $password) {
        $this->xmlRpcUrl = $xmlrpcurl;
        $this->username  = $username;
        $this->password = $password;
    }

    function sendRequest($requestname, $params) {
        $request = xmlrpc_encode_request($requestname, $params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
        curl_setopt($ch, CURLOPT_URL, $this->xmlRpcUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        $results = curl_exec($ch);
        curl_close($ch);
        return $results;
    }

    function createPost($title, $body, $category, $keywords = '', $encoding='UTF-8') {

        $title = htmlentities($title, ENT_NOQUOTES, $encoding);
        $keywords = htmlentities($keywords, ENT_NOQUOTES, $encoding);

        $postData = array(
            'title' => $title,
            'description' => $body,
            'mt_allow_comments' => 0,  // If 1 => allow comments
            'mt_allow_pings' => 0,  // If 1 => allow trackbacks
            'post_type' => 'post',
            'mt_keywords' => $keywords,
            'categories' => array($category)
        );
        $params = array(0, $this->username, $this->password, $postData, true);

        return $this->sendRequest('metaWeblog.newPost', $params);
    }
}

和用法;

$wpClient = new WPClient("http://yourdomain.com/xmlrpc.php", "your_username", "your_password");
$wpClient->createPost("Your title", "Your Content", "Post category", "your tags");