Link to home
Start Free TrialLog in
Avatar of SWS001
SWS001

asked on

How to post categories to Wordpress using metaWeblog.newPost

Hi all,

I've started using metaWeblog.newPost to post to my wordpress blog using XMLRPC as it handled posting tags as well.

According to the metaweblog API spec for XMLRPC it says

metaWeblog.newPost (blogid, username, password, struct, publish) returns string

(within struct)

For categories, pass an array of strings of names of categories that the post belongs to, named categories. On the server side, it's not an error if the category doesn't exist, only record categories for ones that do exist.

http://www.xmlrpc.com/metaWeblogApi

I have attached my code, and a couple outputs from running it. I have tried passing either the ID of category (528) or the name (Accommodation) and neither worked. It posts correctly, just never manages the category. I can use a mt.setCategory or similar but I'm looking to get this working.





CODE


function wpPostXMLRPC($title,$body,$categories,$tags=""){
	$rpcurl = "http://debian/blog/xmlrpc.php";
	$username = "admin";
	$password = "password";
	$categories = implode(",", $categories);

    $content = array(
        'title'=>$title,
        'description'=>$body,
        'mt_allow_comments'=>1,  // 1 to allow comments
        'mt_allow_pings'=>1,  // 1 to allow trackbacks
        'post_type'=>'post',
        'mt_keywords'=>$tags,
        'categories'=>$categories
    );
	
	print_r($content);
	
    $params = array(0,$username,$password,$content,true);

	$request = xmlrpc_encode_request('metaWeblog.newPost',$params); //new
	
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
	curl_setopt($ch, CURLOPT_URL, $rpcurl);
	curl_setopt($ch, CURLOPT_VERBOSE, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	
	$output = curl_exec($ch);
	
	if ($output === false) {
		echo "Curl failure";
	} else {
		echo "Curl success, result: \n\n";
		echo $output;
	}
	curl_close($ch);


OUTPUT


Array
(
    [title] => My title
    [description] => <p>My description<p>
    [mt_allow_comments] => 1
    [mt_allow_pings] => 1
    [post_type] => post
    [mt_keywords] => Keyword one, keyword two
    [categories] => 528
)
* About to connect() to debian port 83 (#0)
*   Trying 127.0.0.1... * connected
* Connected to debian (127.0.0.1) port 83 (#0)
> POST /blog/xmlrpc.php HTTP/1.1
Host: debian:83
Accept: */*
Content-Length: 4743
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue

< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Date: Wed, 07 Apr 2010 10:14:19 GMT
< Server: Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny4 with Suhosin-Patch
< X-Powered-By: PHP/5.2.6-1+lenny4
< Connection: close
< Content-Length: 173
< Vary: Accept-Encoding
< Content-Type: text/xml
<
* Closing connection #0
Curl success, result:

<?xml version="1.0"?>
<methodResponse>
  <params>
    <param>
      <value>
        <string>3612</string>
      </value>
    </param>
  </params>
</methodResponse>
derf@debian:/tnp/RussLive/cron$ php featuredCompanyBlog.php
Array
(
     [title] => My title
    [description] => <p>My description<p>
    [mt_allow_comments] => 1
    [mt_allow_pings] => 1
    [post_type] => post
    [mt_keywords] => Keyword one, keyword two
    [categories] => Accommodation
)
* About to connect() to debian port 83 (#0)
*   Trying 127.0.0.1... * connected
* Connected to debian (127.0.0.1) port 83 (#0)
> POST /blog/xmlrpc.php HTTP/1.1
Host: debian:83
Accept: */*
Content-Length: 4757
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue

< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Date: Wed, 07 Apr 2010 10:18:11 GMT
< Server: Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny4 with Suhosin-Patch
< X-Powered-By: PHP/5.2.6-1+lenny4
< Connection: close
< Content-Length: 173
< Vary: Accept-Encoding
< Content-Type: text/xml
<
* Closing connection #0
Curl success, result:

<?xml version="1.0"?>
<methodResponse>
  <params>
    <param>
      <value>
        <string>3613</string>
      </value>
    </param>
  </params>
</methodResponse>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gwkg
gwkg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SWS001
SWS001

ASKER

Excellent spot thank you! You can have the points.

I was putting it in an array, then forgot I was csv'ing it.

Another caveat is the category needs to be LOWER CASE, so here is what I ended up passing it

Array
(
    [title] => My title
    [description] => <p>My description<p>
    [mt_allow_comments] => 1
    [mt_allow_pings] => 1
    [post_type] => post
    [mt_keywords] => Keyword one, keyword two
    [categories] => my category name
)

Thanks again
Avatar of SWS001

ASKER

Sorry just realised my output looks wrong, should be more like


Array
(
    [title] => My title
    [description]  => <p>My description<p>
    [mt_allow_comments] => 1
     [mt_allow_pings] => 1
    [post_type] => post
     [mt_keywords] => Keyword one, keyword two
    [categories] =>  Array
(
[0] => category 1
[0] => category 2
)
)