2014-04-07 40 views
2

我有一个WordPress应用程序托管在Azure上,我需要使用自定义函数将数据插入到wp_posts表中,因为我将此脚本作为后台作业运行我不能使用WordPress的功能,因为脚本不在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; 
      } 
    } 
+0

您可以通过使用WP API远程创建内容。手动插入数据到数据库并不健康。如果手动插入,Action,Hooks,Filters和wp系统上的所有组件都不起作用。如果您使用API​​,则所有系统都会像在正常的内容创建中一样工作。查看我的回答,例如 –

回答

1

我建议你使用WP API在wordpress上创建任何内容。它可以在任何你想要的地方。如果您手动将数据插入wp数据库,您的系统可能无法正常工作。例如,当您创建帖子时,它会发送一封电子邮件。如果您手动插入数据库,这不起作用。相反,你可以像下面一样使用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, $content, 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"); 
+0

data.php文件,它是完全不在wordpress中的php作业脚本。那么我将如何能够在其中要求类WPClient。请注意,我在Azure付款版本上不允许我访问wordpress文件夹本身,因此要求wordpress文件夹中的文件不起作用 – Mutuma

+0

您可以将上述类放在文件“WpClient.php”中,并将其包含在您的'data.php'。并在我的答案中进行实例化。你不需要在wp文件夹中使用 –

+0

我想要做的是使用后台作业data.php从JSON FEED获取数据,然后将该数据插入到wordpress数据库中,然后我可以在wordpress应用程序中显示这些数据。请注意,在我的Azure付款版本中,我没有被授予访问权限来控制服务器,因为我不在虚拟框中,因此不允许创建wpclient.php。我需要能够在我的data.php上做所有事情。 – Mutuma

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

+0

第一篇文章要求我有一个路径到我的应用程序的wordpress文件夹,但我托管在Azure付款版本,不允许我访问服务器,所以我不能得到wordpress文件夹。我如何能够做到这一点,而无需访问wordpress文件夹? – Mutuma

+0

如果您无法访问任何文件以添加此功能,最好的办法是使用Hüseyin的方法。 –

+0

谢谢!它帮助我:) –

相关问题