开发者

post to wordpress with CURL

I tried to post to wordpress blog from extrnal p开发者_Python百科hp code , all of my files are in the same directory, public_html.

this is my code:

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;
}

but its wrong, the error is

Fatal error: Call to undefined function xmlrpc_encode_request()

i can post to my wordpress blog with microsoft word(publish->blogPost), so Help me


PHP's xmlrpc extension seems to be not enabled on your server.


Wordpress using http://scripts.incutio.com/xmlrpc/ as xmlrpc server, you don't need xmlrpc extension to post to your wp. Just follow the instruction http://scripts.incutio.com/xmlrpc/advanced-client-construction.php.

But if you want to post on another way, such as posting by email you can follow this tutorial codex.wordpress.org/Post_to_your_blog_using_email


I struggled with the same. I found a similar problem somewhere else on the net and tweaked to to fit Wordpress. Mind you Wordpress install (wordpress.org), not the blog hosting service at wordpress.com. This should be working provided you have curl and xmlwriter enabled:

<?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);

?>

This should work directly, but you need to change the strings indicated (Blog URL, username, password, author, etc...). Beware that the login in insecure. This is only for demonstrating the functionality. You may also want to change the response code handling (which isn't mine, it came along with the original example which this is based upon).

On success Wordpress returns XML to you with details of the post event.


Fatal error: Call to undefined function xmlrpc_encode_request()

some times this error appear because xmlrpc extension is disabled.

execute phpinfo() to see if xmlrpc module displays or not.

If not, you need to enable it from php.ini by removing the semicolon, like

;extension=php_xmlrpc.dll to extension=php_xmlrpc.dll

and then restart Apache

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜