Skip to content

Instantly share code, notes, and snippets.

@darrenboyd
Created November 19, 2012 18:23
Show Gist options
  • Save darrenboyd/4112506 to your computer and use it in GitHub Desktop.
Save darrenboyd/4112506 to your computer and use it in GitHub Desktop.
RealGravity API Call Using PHP
<?php
// Example RealGravity API call using PHP.
// Note that the API key is bogus and will not work. Please replace with your key and keep it secure.
// This example will make a search against the market content and take the first result and get
// the video embed code. Both the video title and embed code are output so you should be able to
// see the results of this page in a browser.
/*** REPLACE WITH YOUR API KEY!!! ***/
$apiKey = 'fakeK3y_replaceM3';
$searchApiEndpoint = "http://api.realgravity.com/v1/market_content/search.json";
$widgetApiEndPoint = "http://api.realgravity.com/v1/widgets/single.json";
// This first API call does a market_content search against the $searchTerm below to find a list of videos
$searchTerm = 'halloween';
$searchApi = $searchApiEndpoint . "?search_term=" . urlencode($searchTerm) . "&api_key=" . urlencode($apiKey);
// Use cURL to make the API call and then decode the JSON results to validate we got a result
$searchCurl = curl_init($searchApi);
curl_setopt($searchCurl, CURLOPT_RETURNTRANSFER, 1);
$searchJSONResults = curl_exec($searchCurl);
$result = json_decode($searchJSONResults);
curl_close($searchCurl);
if (!empty($result->contents)) {
$video = $result->contents[0];
print "<h1>" . $video->title . "</h1>";
// Next we use the widget API call to get an embed code and write that out to the results
$demoPlayerId = '2526';
$videoId = $video->id;
$widgetEmbedCodeApi = $widgetApiEndPoint . "?player_id=" . $demoPlayerId .
"&video_id=" . $videoId . "&api_key=" . urlencode($apiKey);
// Use cURL to make the API call and then decode the JSON results to validate we got a result
$widgetCurl = curl_init($widgetEmbedCodeApi);
curl_setopt($widgetCurl, CURLOPT_RETURNTRANSFER, 1);
$widgetJSONResults = curl_exec($widgetCurl);
$widgetResult = json_decode($widgetJSONResults);
if (isset($widgetResult->widgets)) {
$embedCode = $widgetResult->widgets->flash;
print_r($embedCode);
}
else {
print "<h2>No embed code returned. Please verify your API key and the video id.</h2>";
print "<p><b>Video Id:</b>" . $video->id . " </p>";
}
curl_close($widgetCurl);
}
else {
print "<h2>No results. Please verify your API key and search term.</h2>";
print "<p><b>Search Term:</b> " . $searchTerm . " </p>";
}
@darrenboyd
Copy link
Author

Hi Devs! If you see any issues here, please let me know. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment