2011-05-07 49 views
0

我試圖從extrnal php代碼發佈到wordpress博客,我所有的文件都在同一個目錄public_html。發佈到wordpress與CURL

這是我的代碼:

function wpPostXMLRPC1($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; 
} 

,但其錯誤,錯誤的是

Fatal error: Call to undefined function xmlrpc_encode_request() 

我可以張貼到我與Microsoft Word(發佈 - >博客帖子)的WordPress博客,幫幫我吧

+0

和無法理解,請求的'xmlrpc_encode_request'函數未在服務器上安裝或激活。 – 2011-05-07 17:53:44

回答

2

PHP的xmlrpc擴展似乎沒有在您的服務器上啓用。

+0

但我可以用microsoftword(publish-> blogpost)發送blogpost,它使用xmlrpc發送帖子 – Javad 2011-05-07 18:26:23

+0

我現在在我的服務器上安裝了xmlrpc,並再次嘗試但顯示同樣的錯誤致命錯誤:調用未定義函數xmlrpc_encode_request() – Javad 2011-05-07 18:47:07

+0

Did你重新加載服務器配置?你可以在phpinfo()的輸出中看到_xmlrpc_擴展嗎? – abaumg 2011-05-07 18:49:35

0

我用同樣掙扎。我在網絡上的其他地方發現了一個類似的問題,並調整爲適合Wordpress。注意你的WordPress安裝(wordpress.org),而不是wordpress.com的博客託管服務。這應該是工作的前提是你有捲曲的XmlWriter啓用:

<?php 
class atompub 
    { 

    //public $parae = ''; 

    function __construct($one, $two, $three, $four) 
     { 
     $this->author=$one; 
     $this->title=$two; 
     $this->categories=$three; 
     $this->body=$four; 
     } 

    function create_post() 
     { 
     $xmlwriter = new XMLWriter(); 
     $xmlwriter->openMemory(); 
     $xmlwriter->startDocument("1.0", "UTF-8"); 
      $xmlwriter->startElement('entry'); 
       $xmlwriter->writeAttribute('xmlns', 'http://www.w3.org/2005/Atom'); 
       $xmlwriter->startElement('author'); 
        $xmlwriter->writeElement('name', $this->author); 
       $xmlwriter->endElement(); 
       $xmlwriter->writeElement('title', $this->title); 
       $xmlwriter->startElement('content'); 
        $xmlwriter->writeAttribute('type', 'html'); 
        $xmlwriter->text($this->body); 
       $xmlwriter->endElement(); 
       $xmlwriter->startElement('category'); 
        $xmlwriter->writeAttribute('term', $this->categories); 
       $xmlwriter->endElement(); 
      $xmlwriter->endElement(); 
     $xmlwriter->endDocument(); 

     return $xmlwriter->outputMemory(); 
     } 

    function __destruct() 
     { 
     } 
    } 


$target = "<URL til your WordPress installation>/wp-app.php/posts"; 
// Note that the directory "posts" are used for posting (POST method) 
// "service" is used to pull info via the GET method (not shown here) 

$user = "XXX"; // Substitue XXX with your WordPress username 
$passwd = "YYY"; // Substitue XXX with your WordPress password 

$author='Your Name'; 
$title='The title of your choice for your new entry'; 
$array_of_categories='Category'; 
$body='This is the main body. All the text goes in here'; 

$xml_post = new atompub($author,$title,$array_of_categories,$body); 
$post = $xml_post->create_post(); 

$headers = array("Content-Type: application/atom+xml "); 
$handle = curl_init($target); 
$curlopt_array = array(
CURLOPT_HTTPHEADER => $headers, 
CURLOPT_POST => true, 
CURLOPT_POSTFIELDS => $post, 
CURLOPT_SSL_VERIFYPEER => false, 
CURLOPT_USERPWD => $user.':'.$passwd, 
CURLOPT_FOLLOWLOCATION => true, 
CURLINFO_HEADER_OUT => true); 
curl_setopt_array($handle, $curlopt_array); 

$result = curl_exec($handle); 
//var_dump($result); 
$header_sent=curl_getinfo($handle); 
//var_dump($header_sent); 

if ($result === false) { 
print "Got " . curl_errno($handle) . " : " . curl_error($handle) . "\n"; 
curl_close ($handle); 
return; 
} 

$response_http_code = curl_getinfo ($handle, CURLINFO_HTTP_CODE); 
if ($response_http_code != 201) { 
print("HTTP status code: $response_http_code \n"); 
curl_close($handle); 
return; 
} 

curl_close($handle); 

?> 

這應該直接工作,但您需要更改指定的字符串(博客網址,用戶名,密碼,作者等)。注意登錄不安全。這僅用於演示功能。您可能還想要更改響應代碼處理(這不是我的,它與基於此的原始示例一起)。

成功Wordpress將XML返回給您,並提供發佈事件的詳細信息。

+0

謝謝,但我怎麼能附加文件(前圖像)呢? – Javad 2011-09-03 03:00:57

+0

這個例子使用Atompub協議,它似乎只用於文本(不是二進制)。我可能是錯的,但我很確定。這與Curl本身無關,但接收端(Wordpress)期望使用特定標籤的XML輸入。 – itsproject 2011-10-18 07:01:45

0

致命錯誤:調用未定義功能xmlrpc_encode_request()

有時這種錯誤出現,因爲xmlrpc擴展被禁用。

執行phpinfo()查看xmlrpc模塊是否顯示。

如果沒有,則需要通過刪除分號的php.ini啓用它,就像

;延長= php_xmlrpc.dll分機= php_xmlrpc.dll

,然後重新啓動Apache