x
 
Create New Page
 View Previous Changes    ( Last updated by siffring )

How to add an entry using PHP and Metaweblog API

Here’s a quick way to post entries to your EE weblog using PHP and the Metaweblog API.

First, install and configure the Metaweblog API using the documentation.

Next, download and install the PHP XMLRPC library on your server.

Then you can use this PHP code to post entries to your weblog. You will need to set your EE API path and add a valid EE username and password. (I recommend creating a new user for your script and limit its permissions as much as possible.)

<?php
include("xmlrpc.inc"); // Make sure the XMLRPC library is in your path or give the full path

// Get this value from EE under Modules -> Metaweblog API -> URL
$ee_api_path = "http://yourdomain.com/index.php?ACT=XX&id=Y;
$ee_username = "
YOUR-EE-USERNAME";
$ee_password = "
YOUR-EE-PASSWORD";

$c = new xmlrpc_client($ee_api_path);
//$c->debug = true; // Uncomment this line for debugging info
    
$content['title']="
TITLE GOES HERE";
$content['categories'] = array("
CATEGORY #1","CATEGORY #2");

// You can map these three fields to any field (or custom field)
// in your weblog. Just make sure that the field is a textarea and
// follow the instructions in the EE documentation mentioned above.
$content['description']="DESCRIPTION GOES HERE";
$content['mt_text_more']="MORE GOES HERE";
$content['mt_keywords]="KEYWORDS GO HERE";


$x = new xmlrpcmsg("metaWeblog.newPost",
        array(php_xmlrpc_encode("1"),
        php_xmlrpc_encode($ee_username),
        php_xmlrpc_encode($ee_password),
        php_xmlrpc_encode($content),
        php_xmlrpc_encode("1")));

$c->return_type = '
phpvals';
$r =$c->send($x);
if ($r->errno=="0")
    echo "<br>Successfully Posted ";
else {
    echo "<br>There was an error";
    print_r($r);
}
?>

If you get stuck, it might be worth looking at the code for EE’s Metaweblog API plugin. You can find that under system/modules/metaweblog_api.

Also, there is a lot more you can do with this API that is not covered here like listing existing entries and categories, etc.

Thanks to this page for some of the sample code.
Category:Metaweblog API

Categories: