WordPress XMLRPC: wp.getTags

WordPress XMLPRC provides the function wp.getTags to get all the tags corresponding to a blog.

Parameters to this function:

  1. int blog_id
  2. string username
  3. string password

Return Values

  • array
    • struct
      1. int tag_id
      2. string name
      3. int count
      4. string slug
      5. string html_url
      6. string rss_url

It returns an array of tags. Each array element contains the details of the tag like the HTML URL, RSS URL, tag count.

The following program shows the usage of wp.getTags. Note that you must fill the following values

  1. $USERNAME
  2. $PASSWORD
  3. $BLOGID
  4. $BLOGURL

Note that BLOGID is usually one or you can use wp.getUsersBlogs to get the value of BLOGID.

<?php

$USERNAME = "admin";
$PASSWORD = "check";
$BLOGID = 1;
$BLOGURL = "http://localhost/wordpress";

$XMLRPC_URL = $BLOGURL."/xmlrpc.php";

function get_response($URL, $context) {
 if(!function_exists('curl_init')) {
 die ("Curl PHP package not installedn");
 }

 /*Initializing CURL*/
 $curlHandle = curl_init();

 /*The URL to be downloaded is set*/
 curl_setopt($curlHandle, CURLOPT_URL, $URL);
 curl_setopt($curlHandle, CURLOPT_HEADER, false);
 curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
 curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);

 /*Now execute the CURL, download the URL specified*/
 $response = curl_exec($curlHandle);
 return $response;
}

  /*Creating the wp.getTags request which takes on two parameters
   username and password*/
  $request = xmlrpc_encode_request("wp.getTags",
    array($BLOGID, $USERNAME, $PASSWORD));

  /*Making the request to wordpress XMLRPC of your blog*/
  $xmlresponse = get_response($XMLRPC_URL, $request);
  $response = xmlrpc_decode($xmlresponse);

  if ($response && xmlrpc_is_fault($response)) {
    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  } else {
    /*Printing the response on to the console*/
    print_r($response);
  }
echo "n";
?>
}

Let’s execute the program

$ php wp.getTags.php

The output will be something like

[xml]
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<array><data>
<value><struct>
<member>
<name>tag_id</name>
<value>
<string>7</string>
</value>
</member>
<member>
<name>name</name>
<value>
<string>install libboost-python-dev ubuntu</string>
</value>
</member>
<member>
<name>count</name>
<value>
<string>1</string>
</value>
</member>
<member>
<name>slug</name>
<value>
<string>install-libboost-python-dev-ubuntu</string>
</value>
</member>
<member>
<name>html_url</name>
<value>
<string>http://localhost/wordpress/?tag=install-libboost-python-dev-ubuntu</string&gt;
</value>
</member>
<member>
<name>rss_url</name>
<value>
<string>http://localhost/wordpress/?feed=rss2&amp;amp;tag=install-libboost-python-dev-ubuntu</string&gt;
</value>
</member>
</struct>
</value>
<value><struct>
<member>
<name>tag_id</name>
<value>
<string>9</string>
</value>
</member>
<member>
<name>name</name>
<value>
<string>install lobasis3.3-javafilter ubuntu</string>
</value>
</member>
<member>
<name>count</name>
<value>
<string>2</string>
</value>
</member>
<member>
<name>slug</name>
<value>
<string>install-lobasis3-3-javafilter-ubuntu</string>
</value>
</member>
<member>
<name>html_url</name>
<value>
<string>http://localhost/wordpress/?tag=install-lobasis3-3-javafilter-ubuntu</string&gt;
</value>
</member>
<member>
<name>rss_url</name>
<value>
<string>http://localhost/wordpress/?feed=rss2&amp;amp;tag=install-lobasis3-3-javafilter-ubuntu</string&gt;
</value>
</member>
</struct>
</value>
<value><struct>
<member>
<name>tag_id</name>
<value>
<string>10</string>
</value>
</member>
<member>
<name>name</name>
<value>
<string>java</string>
</value>
</member>
<member>
<name>count</name>
<value>
<string>2</string>
</value>
</member>
<member>
<name>slug</name>
<value>
<string>java</string>
</value>
</member>
<member>
<name>html_url</name>
<value>
<string>http://localhost/wordpress/?tag=java</string&gt;
</value>
</member>
<member>
<name>rss_url</name>
<value>
<string>http://localhost/wordpress/?feed=rss2&amp;amp;tag=java</string&gt;
</value>
</member>
</struct>
</value>
<value><struct>
<member>
<name>tag_id</name>
<value>
<string>6</string>
</value>
</member>
<member>
<name>name</name>
<value>
<string>libboost-python-dev</string>
</value>
</member>
<member>
<name>count</name>
<value>
<string>1</string>
</value>
</member>
<member>
<name>slug</name>
<value>
<string>libboost-python-dev</string>
</value>
</member>
<member>
<name>html_url</name>
<value>
<string>http://localhost/wordpress/?tag=libboost-python-dev</string&gt;
</value>
</member>
<member>
<name>rss_url</name>
<value>
<string>http://localhost/wordpress/?feed=rss2&amp;amp;tag=libboost-python-dev</string&gt;
</value>
</member>
</struct>
</value>
<value><struct>
<member>
<name>tag_id</name>
<value>
<string>8</string>
</value>
</member>
<member>
<name>name</name>
<value>
<string>lobasis3.3-javafilter</string>
</value>
</member>
<member>
<name>count</name>
<value>
<string>2</string>
</value>
</member>
<member>
<name>slug</name>
<value>
<string>lobasis3-3-javafilter</string>
</value>
</member>
<member>
<name>html_url</name>
<value>
<string>http://localhost/wordpress/?tag=lobasis3-3-javafilter</string&gt;
</value>
</member>
<member>
<name>rss_url</name>
<value>
<string>http://localhost/wordpress/?feed=rss2&amp;amp;tag=lobasis3-3-javafilter</string&gt;
</value>
</member>
</struct>
</value>
</data>
</array>
</value>
</param>
</params>
</methodResponse>

[/xml]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s