Ning Logo

Access Your Ning App with Your Favorite Programming Language

Next: Ning REST API Reference >>

The Ning REST API allows both the reading and writing of Ning App data from anywhere on the internet, using any programming language that can talk HTTP. The full database power of the Ning Content Store is available to your code, wherever it is.

David wanted to pull the latest items out of his Band Names app automatically for inclusion on his website. Since his website's written in PHP, he wrote some PHP code to do it, below. (You can also take a look at equivalent code in Perl and Python)

(Note: Feed requests made by external code should add the GET parameter xn_auth=no to circumvent Ning's cookie-based authentication scheme.)

<?php
 
$url = "http://bandnames.ning.com/xn/atom/1.0/";
$url .= "content(type='NingBlogPost')";
$url .= "?xn_auth=no&order=published@D&from=0&to=10";
$sxml = @simplexml_load_file($url);
echo '<ul>';
foreach ($sxml->entry as $entry) {
  echo "<li><a href='http://bandnames.ning.com/"
      . "?action=post&amp;id=". $entry->id . "'>" 
      . htmlentities($entry->title).'</a></li>';
}
echo '</ul>';
 
?>

He also knocked together a quick example of how to insert a new object into an App. The REST API also allows objects to be updated, deleted and tagged; anything you can do with an object inside a Ning App, you can do with the REST API. Note that for this example, the login credentials provided have to be those of the App owner. (Once again, we have the equivalent Perl code)

 
<?php
 
/* Configuration information */
$app = 'your-app-here.ning.com';
$user = 'your-NingID-here';
$password = 'your-password-here';
 
/**
 * Content objects are atom entries
 *
 * The xn namespace is for Ning-specific system attributes.
 *   You must specify an <xn:type/> element to indicate the
 *   type of the content object.
 * The my namespace is for any other attributes you want.
 *
 * More info on the entry format is at:
 * --> http://documentation.ning.com/sections/atom.php
 */
$entry =<<<_XML_
<entry xmlns="http://www.w3.org/2005/Atom"
       xmlns:xn="http://www.ning.com/atom/1.0"
       xmlns:my="http://$app">
  <xn:type>Food</xn:type>
  <title type="text">Hamburger</title>
  <summary type="text">A delicious meaty sandwich.</summary>
  <xn:private>false</xn:private>
  <my:rating type="number">5</my:rating>
  <my:ingredients type="string">
    <xn:value>Bun</xn:value>
    <xn:value>Ground Beef</xn:value>
    <xn:value>Onions</xn:value>
    <xn:value>Lettuce</xn:value>
    <xn:value>Pickles</xn:value>
    <xn:value>Ketchup</xn:value>
  </my:ingredients>
</entry>
_XML_;
 
/*
 * NOTE: the URL begins with https -- this request should be 
 * sent over SSL so that your NingID and password aren't 
 * exposed on the network.
 */
$curl = curl_init("https://$app/xn/atom/1.0/content");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
/* Ignore SSL Cert verification unless your curl 
 * configuration is set up to handle it. */
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $entry);
curl_setopt($curl, CURLOPT_USERPWD, $user.':'.$password);
 
print curl_exec($curl);
 
?>

Next: Ning REST API Reference >>