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

SWS001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gwkgCommented:
I haven't used this but I do notice that it says

"For categories, pass an array of strings of names of categories that the post belongs to"

So why are you are transforming your array of categories into a string with implode?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SWS001Author Commented:
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
0
SWS001Author Commented:
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
)
)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.