2011-05-07 77 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