2013-07-18 4 views
6

में पोस्ट करने के लिए छवि संलग्न करें मैं वर्डप्रेस में पोस्ट करने के लिए XMLRPC का उपयोग कर रहा हूं। वर्डप्रेस कोड डीबग करने के बाद, मुझे थंबनेल पोस्ट करने में समस्याएं आ रही हैं, मुझे लगता है कि मेरी समस्या इस तथ्य के कारण है कि छवि पोस्ट से जुड़ी नहीं है। मुझे वर्डप्रेस पैच किए बिना या PHP का उपयोग किए बिना यह करना होगा, केवल iwth XMLRPC।वर्डप्रेस XMLRPC

मैं एक छवि अपलोड कर सकता हूं और छवि की आईडी प्राप्त कर सकता हूं। अन्य बिंदु जो मुझे भ्रमित करता है वह है कि आप एक पोस्ट को एक ऐसी छवि से कैसे संलग्न करते हैं जिसे आपने अभी तक पोस्ट नहीं किया है क्योंकि आप छवि अपलोड करने की प्रतीक्षा करते हैं? मुझे छवि अपलोड करने के बाद पोस्ट करना है, फिर छवि आईडी का उपयोग करना और पोस्ट आईडी छवि मेटाडेटा पर एक अद्यतन करते हैं?

संपादित करें: वर्डप्रेस में कोड है कि समस्याग्रस्त है इस चेक

if ($thumbnail_html = wp_get_attachment_image($thumbnail_id, 'thumbnail')) 

है और मेरी धारणा यह है कि यह विफल रहता है, क्योंकि छवि अनअनुलग्नित है, अगर मैं उस कोड को ठीक सब ठीक है, लेकिन मैं नहीं कर सकता है मेरे एप्लिकेशन उपयोगकर्ताओं के डब्ल्यूपी को पैच करें (इसलिए यह कोई समाधान नहीं है)

+0

http://wp.tutsplus.com/tutorials/creative-coding/uploading-pictures-via-xml-rpc-and-php-to-wordpress/ देखें। – user1929959

+0

किसी भी तरह से PHP कोड में हेरफेर किए बिना यह संभव नहीं है। –

उत्तर

7

हां यह संभव है यदि वर्डप्रेस संस्करण 3.5 या अधिक हो, तो फ़ाइल/छवि अपलोड करने के लिए कोड का उपयोग करते समय आप post_id सेट कर सकते हैं।

  1. newPost समारोह का उपयोग करें और विशेष रुप से प्रदर्शित छवि के बिना सामग्री पोस्ट और यह भी सेट गलत पर प्रकाशित करते हैं, post_id इस

    द्वारा लौटाए गए रिकॉर्ड: प्रवाह मैं फ़ीचर की गई छवियों के साथ नए पदों के लिए इस्तेमाल इस तरह है
  2. छवि अपलोड करें और पोस्ट सिर्फ तैनात की आईडी पर post_id निर्धारित करते हैं, image_id

  3. रिकॉर्ड जब संपादित पोस्ट किया है और wp_post_thumbnailके बराबर सेटimage_id तुम सिर्फ अपलोड की गई और यह भी स्थापित करने के लिए प्रकाशित सच (यदि आवश्यक हो)

महत्वपूर्ण: माइम प्रकार महत्वपूर्ण है, यह "छवि/जेपीजी" या "छवि/png" कृपया दस्तावेज़ देखें, अगर होना चाहिए माइम प्रकार "jpg" अटैचिंग की तरह खराब हो जाएगा विफल हो जाएगा।

युक्ति: डीबगिंग के लिए, यदि आपको वर्डप्रेस से सामान्य त्रुटि मिलती है और आप यह नहीं समझ सकते कि आप वर्डप्रेस कोड की जांच क्यों कर सकते हैं और इसे संपादित भी कर सकते हैं, डीबगिंग/ट्रेसिंग कॉल जोड़ना और उम्मीद है कि आप कारण बता सकते हैं ।

यह श्रेणी, छवि और टैग के साथ एक पोस्ट का एक उदाहरण है। यह वर्ग IXR.php
https://github.com/WordPress/WordPress/blob/master/wp-includes/class-IXR.php
और mime_content_type समारोह
https://github.com/caiofior/storebaby/blob/master/magmi/plugins/extra/general/socialnotify/wp/mimetype.php

 $client = new IXR_Client($url); 
     $content = array(
      'post_status' => 'draft', 
      'post_type' => 'post', 
      'post_title' => 'Title', 
      'post_content' => 'Message', 
      // categories ids 
      'terms' => array('category' => array(3)) 
     ); 
     $params = array(0, $username, $password, $content); 
     $client->query('wp.newPost', $params); 
     $post_id = $client->getResponse(); 

     $content = array(
      'name' => basename('/var/www/sb/img.jpeg'), 
      'type' => mime_content_type('/var/www/sb/img.jpeg'), 
      'bits' => new IXR_Base64(file_get_contents('/var/www/sb/img.jpeg')), 
      true 
     ); 
     $client->query('metaWeblog.newMediaObject', 1, $username, $password, $content); 
     $media = $client->getResponse(); 
     $content = array(
      'post_status' => 'publish', 
      // Tags 
      'mt_keywords' => 'tag1, tag2, tag3', 
      'wp_post_thumbnail' => $media['id'] 
     ); 
     $client->query('metaWeblog.editPost', $post_id, $username, $password, $content, true); 
+0

संपादन के साथ दो चरणों में ऐसा क्यों करना आवश्यक है? मुझे लगता है कि आप पहले छवि अपलोड कर सकते हैं, आईडी को कैप्चर कर सकते हैं, फिर आईडी को newPost() में संलग्न कर सकते हैं? –

+0

@RickStrahl उस समय मुझ पर भरोसा करते हैं कि उन सभी चरणों में महत्वपूर्ण थे, शायद अब अलग है, लेकिन उस छवि को अपलोड करने के लिए आपको पोस्ट में छवि संलग्न करने के लिए post_id होना था, और विशेष रुप से प्रदर्शित छवि को सेट करने के लिए आपको आईडी की आवश्यकता थी अपलोड की गई छवि, मेरा प्रश्न और उत्तर सीधे XMLRPC का उपयोग करने के लिए था और कोई WP लाइब्रेरी नहीं है जो शायद इसे कम चरणों में कर सकती है – simion314

0

यह, मेरे संस्करण है wp.newPost और wp.editPost उपयोग करने की आवश्यकता, वर्डप्रेस 3.4 पर कहा, कि कस्टम पद के उपयोग की अनुमति प्रकार

require_once("IXR_Library.php.inc"); 
$title = 'My title'; 
$body = 'My body'; 
$category="category1, category2"; // Comma seperated pre existing categories. Ensure that these categories exists in your blog. 
$keywords="keyword1, keyword2, keyword3"; 
$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format 

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

$content = array(
    'post_title'=>$title, 
    'post_content'=>$body, 
    'post_type'=>'some_custom_post_type', 
    'post_status' => 'draft', // http://codex.wordpress.org/Post_Status 
    'mt_allow_comments'=>0, // 1 to allow comments 
    'mt_allow_pings'=>0, // 1 to allow trackbacks 
    'mt_keywords'=>$keywords, 
    'categories'=>array($category), 
    'custom_fields' => array($customfields) 
); 

// Create the client object 
$client = new IXR_Client('http://example.com/xmlrpc.php'); 
$username = "wp_username"; 
$password = "wp_username_password"; 

$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immediately, to save as draft set it as 'false' 

if (!$client->query('wp.newPost', $params)) { 
    die('<br/><strong>Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >'); 

    } 
else 
{ 
    $post_id = $client->getResponse(); 
    echo 'Inserted with id'.$post_id; 
    $picture = '/full/path/to/pic.jpg'; 
    $content = array(
     'name' => basename($picture), 
     'type' => mime_content_type($picture), 
     'bits' => new IXR_Base64(file_get_contents($picture)), 
     true 
    ); 
    if (!$client->query('metaWeblog.newMediaObject', 1, $username, $password, $content)) { 
     die('<br/><strong>Something went wrong - newMediaObject'.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >'); 
    } 
    else 
    { 
     $media = $client->getResponse(); 
     $content = array(
      'post_status' => 'publish', 
      'post_thumbnail' => $media['id'] 
     ); 
     if (!$client->query('wp.editPost', 0, $username, $password, $post_id, $content)) { 
      die('<br/><strong>Something went wrong editPost - '.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >'); 
     } 
    } 
} 
1

मेरा संस्करण यदि आप केवल wp.newPost और wp का उपयोग करना चाहते हैं।editPost

include_once(ABSPATH . WPINC . '/class-IXR.php'); 
include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php'); 

    $usr = 'username_on_the_server_side'; 
    $pwd = 'password_on_the_server_side'; 
    $xmlrpc = 'server side xmlrpc.php url'; 
    $client = new IXR_Client($xmlrpc); 

    //////////// IMAGE UPLOAD AND ATTACHMENT POST CREATION /////////// 
     $img_attach = 'link to the image'; 
     $img_attach_content = array(
       'name' => basename($img_attach), 
       'type' => mime_content_type($img_attach), 
       'bits' => new IXR_Base64(file_get_contents($img_attach)), 
         ); 
     $status = $client->query('wp.uploadFile','1', $usr, $pwd, $img_attach_content); 
     $image_returnInfo = $client ->getResponse(); 

    //////////// POST CREATION /////////// 

     $custom_fields = array( 
          array('key' => 'blabla1', 'value' => 'blabla1_value'), 
          array('key' => 'blabla12', 'value' => 'blabla1_value2') 
         ); 
     $post_content = array(
      'post_type' => 'post', 
      'post_status' => 'draft', //for now 
      'post_title' => 'XMLRPC Test', 
      'post_author' => 3, 
      'post_name' => 'XMLRPC Test', 
      'post_content' => 'XMLRPC Test Content', 
      'custom_fields' => $custom_fields 
     ); 

    $res = $client -> query('wp.newPost',1, $usr, $pwd, $post_content); 
    $postID = $client->getResponse(); 
    if(!$res) 
     echo 'Something went wrong....'; 
    else { 
      echo 'The Project Created Successfully('.$res.')<br>Post ID is '.$postID.'<br>'; 
    } 

    //////////// Image Post Attachment Edit /////////// 
     $img_attach_content2 = array(
       'post_type' => 'attachment', 
       'post_status' => 'inherit', 
       'post_title' => $postID, 
       'post_name' => $postID, 
       'post_parent' => $postID, 
       'guid' => $image_returnInfo['url'], 
       'post_content' => '', 
       'post_mime_type' => 'image/jpg' 
       ); 

    $res2 = $client -> query('wp.editPost', 0, $usr, $pwd,  $image_returnInfo['id'], $img_attach_content2); 

    $postIDimg = $client->getResponse();  

    //////////// POST EDIT /////////// 

     $post_content2 = array(
       'post_status' => 'publish', //publish 
       'wp_post_thumbnail' => $image_returnInfo['id'], 
       'custom_fields' => array('key' => '_thumbnail_id', 'value' => $image_returnInfo['id']) 
      ); 
      $media2= $client->query('wp.editPost',0, $usr, $pwd, $postID, $post_content2);