2014-04-07 70 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

謝謝!它幫助我:) –

相關問題