PHP SDK

In order to ease the development of your PHP projects, Viadeo provides you with a dedicated SDK in this language.

You can read a short code introduction below, access the SDK reference or directly download the SDK.

Please you’ll need to install curl for PHP : sudo apt-get install curl libcurl3 libcurl3-dev php5-curl

The code is often the best usage example, so let’s consider the following PHP code :

include('viadeoapi.inc');

// Initialize ViadeoAPI instance ----------------------------------------------
$VD = new ViadeoAPI();
$VD->init(array(
        'store'            =>    true,
        'client_id'        =>    '[your client id]',
        'client_secret'    =>    '[your client secret]'
     ));

// Manage with disconnect -----------------------------------------------------
if (isset($_REQUEST['disconnect']) && ($_REQUEST['disconnect'] == 'true')) {
        $VD->disconnect();
        echo "You've been disconnected, ";
        echo "<a href='".ViadeoHelper::getCurrentURL()."'>reconnect</a>";
        exit(0);
}

// Use the Viadeo API automated OAuth workflow management ---------------------
try { $VD->OAuth_auto(); } catch (ViadeoException $e)  {
    echo "An error occured during Viadeo API authentication: $e";
}

// ============================================================================

try {
    // Retrieve my user id ----------------------------------------------------
    $me          = $VD->get("/me")->execute();

    // Retrieve my contacts, by pages of 100 contacts, with partial details ---
    $req_friends = $me->connection('contacts')->user_detail('partial')->limit(100);
    $friends     = $req_friends();

    // Start HTML output, and prints the disconnect link ----------------------
    html_start($friends->count) ;
    if ($VD->isAuthenticated()) {
        echo "<a href='".ViadeoHelper::getCurrentURL()."?disconnect=true'>disconnect</a><br/>";
    }

    // Generate an HTML page with all my contacts, retrieving all result pages
    $nb = 1; $maxPages = 5;
    do {
       foreach ($friends->data as $friend) {
           $class = ($nb % 2 == 0) ? "class='odd'" : "";
           print_friend($friend, $class, $nb++);
       }
    } while ((--$maxPages > 0)
         && (strlen($friends->paging->next) > 0)
         && ($friends = $req_friends->setURL($friends->paging->next)->x()));

    html_end();

    // Indicates on my news that I'm consulting my contacts -------------------
    $VD->post("/status")->message("I'm consulting the list of my friends..")->execute();

} catch (ViadeoAPIException $e) {
    echo "An error occured during API manipulations";
}

For code simplification, the three methods html_start(), html_end() and print_friend() are not shown.

You can find below a sample implementation :

function html_start($count) {
    echo "<html><head><meta charset='UTF-8'/>";
    echo <<<EOF
        <style>
        body {font-family: Tahoma,Helvetica,Sans Serif;}
        table {border-collapse:collapse;}
        tr.odd{background:#f5f5f5;}
        th {font-weight: bold;}
        td, th {border:1px solid #ddd; padding:5px 8px}
        </style>
EOF;
    echo "</head><body>You have <b>" . $count . "</b> friends on Viadeo ! <br/><br/>";
    echo "<table border='1'>";
    echo "<tr><th>Num</th><th><th>Name</th><th>Contacts</th><th>Headline</th></tr>";
}

function html_end() {
    echo "</table></body></html>";
}

function print_friend($friend, $class, $nb) {
    echo "<tr $class>";
    echo "  <td>".$nb."</td>";
    echo "  <td><a href='".$friend->link."'><img src='".$friend->picture_small."'/></a></td>";
    echo "  <td>".$friend->name."</td>";
    echo "  <td>".$friend->contact_count."</td>";
    echo "  <td>".$friend->headline."</td>";
    echo "</tr>";
}

You can access to the running sample clicking here.

You can go directly to the SDK reference for more details about its usage.

You can also download the latest version of the SDK.