Accessing the Ning API with PHP

PHP is a popular scripting language used for making web applications. Here are some examples of how to use the Ning API with your own PHP scripts. The examples use the Ning API PHP library.

To use the Ning API PHP library, replace the following values in NingApi.php:

  • $subdomain
  • $email
  • $password
  • $consumerKey
  • $consumerSecret

Retrieving tokens

The Ning API PHP library automatically requests a token when it is needed. The developer adds their email and password to the NingApi.php configuration file.

Retrieving photos

<?php
require_once('ning-api-php/NingApi.php');

// Get the most recent photo
$result = NingApi::instance()->photo->fetchNRecent();

print_r($result);

Outputs:

Array
(
    [success] => 1
    [anchor] => JSEwMVyGby2GnKyw-yJEDs03z9v-8loI
    [firstPage] => 1
    [lastPage] => 
    [entry] => Array
        (
            [0] => Array
                (
                    [id] => 3843070:Photo:525
                    [author] => 2cpor74jnszaj
                    [url] => http://apiexample.ning.com/xn/detail/3843070:Photo:525
                    [createdDate] => 2011-03-29T20:54:48.130Z
                    [updatedDate] => 2011-03-29T20:54:48.156Z
                    [title] => Photo Title
                    [description] => Photo Description
                    [visibility] => all
                    [tags] => Array
                        (
                        )

                    [image] => 3843070:Photo:525
                )

        )

    [resources] => Array
        (
            [3843070:Photo:525] => Array
                (
                    [height] => 600
                    [width] => 600
                    [url] => http://dummy.dummy/nonexistent.ico
                )

            [2cpor74jnszaj] => Array
                (
                    [fullName] => John Quest
                    [iconUrl] => http://dummy.dummy/nonexistent.ico
                    [url] => http://apiexample.ning.com/profile/JohnQuest
                )

        )

)

Add a new photo

<?php
require_once('ning-api-php/NingApi.php');

$parts = array(
    "title" => "Photo Title",
    "description" => "Photo Description",
    "file" => "@/Users/devin/Pictures/nasa/NASA-01.jpg"
);

// Create a new photo
$result = NingApi::instance()->photo->create($parts);

print_r($result);

Outputs:

Array
(
    [success] => 1
    [id] => 3843070:Photo:525
)

Update a photo

<?php
require_once('ning-api-php/NingApi.php');

$parts = array(
    "title" => "Photo Title",
    "description" => "Photo Description",
    "file" => "@/Users/devin/Pictures/nasa/NASA-01.jpg"
);

// Create a new photo
$newPhoto = NingApi::instance()->photo->create($parts);

$parts = array(
    "title" => "Updated Photo Title",
    "description" => "Updated Photo Description",
    "id" => $newPhoto['id']
);

// Update the photo we created
$result = NingApi::instance()->photo->updateById($parts);

print_r($result);

Outputs:

Array
(
    [success] => 1
)

Delete a photo

<?php
require_once('ning-api-php/NingApi.php');

$parts = array(
    "title" => "Photo Title",
    "description" => "Photo Description",
    "file" => "@/Users/devin/Pictures/nasa/NASA-01.jpg"
);

// Create a new photo
$newPhoto = NingApi::instance()->photo->create($parts);

// Delete the photo we created
$result = NingApi::instance()->photo->delete($newPhoto);

print_r($result);

Outputs:

Array
(
    [success] => 1
)