But who doesn’t love a little copy and paste action? Below are links to example code for various languages.
If there is a language you think we are missing please let us know!
We generally recommend that users utilize whichever library they would normally use to make HTTP requests. Since all authentication and request information is passed to OpenGraph via HTTP GET parameters, any HTTP client will work. All responses from OpenGraph.io are returned in JSON and use the same formatting.
Ruby
C#
Php
NodeJS
JQuery
require 'opengraph-io'
opengraph = OpenGraphIO.new({app_id: '!!!!YOUR_APP_ID!!!'})
ogData = opengraph.get_site_info('https://github.com')
siteDescription = ogData["hybridGraph"]["description"]
print siteDescription
To get the code above working, create a new directory and change into that directory from the command line (Linux or OSX)
mkdir example & cd example
Next, install the Ruby opengraph-io package
gem install -user-install opengraph-io
Finally, run the script which outputs the hybrid description of the site as gathered by OpenGraph.io
ruby test.rb
using System;
using System.Net;
using System.IO;
using System.Json;
namespace CSharpDemo
{
class MainClass
{
public static void Main (string[] args)
{
var url ="http://cnet.com";
var urlEncoded = Uri.EscapeDataString(url);
var requestUrl = "https://opengraph.io/api/1.1/site/" + urlEncoded;
⁄⁄ Make sure to get your API key! No need for a CC
requestUrl += "?app_id=XXXXXXXXXXXXXXXXXXXXXXXX";
var request = WebRequest.Create(requestUrl);
request.ContentType = "application/json;";
string text;
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
dynamic x = JsonConvert.DeserializeObject(text);
Console.WriteLine("Title\t\t" + x.hybridGraph.title);
Console.WriteLine("Description\t" + x.hybridGraph.description);
Console.WriteLine("Image\t\t" + x.hybridGraph.image);
}
}
}
}
$siteUrl = 'http://cnet.com';
$requestUrl = 'https://opengraph.io/api/1.1/site/' . urlencode($siteUrl);
⁄⁄ Make sure you include your free app_id here! No CC required
$requestUrl = $requestUrl . '?app_id=XXXXXXXXXXXXXXXXXXXXXXXX';
$siteInformationJSON = file_get_contents ($requestUrl);
$siteInformation = json_decode($siteInformationJSON, true);
print ';Title\t\t'; . $siteInformation['hybridGraph']['title'] . '\n';
print 'Description\t' . $siteInformation['hybridGraph']['description'] . '\n';
print 'Logo URL\t' . $siteInformation['hybridGraph']['image'] . '\n';
To get the code above working, copy the code above into a file like test.php. Then run the following command
php test.php
var opengraph = require('opengraph-io')({appId: 'xxxxxx'}) ⁄⁄ <-- Enter your app_id!
var express = require('express');
var app = express();
app.get('/site/info', function (req, res) {
var siteUrl = req.query['url'];
opengraph.getSiteInfo(siteUrl, function(err, siteInfo){
console.log('hey err, err);
console.log('hey result', siteInfo);
res.json(siteInfo);
});
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!');
console.log('Test this proxy with the following url:', 'http://localhost:3000/site/info?url=https%3A%2F%2Fnytimes.com');
});
To get the code above working, create a new directory and change into that directory from the command line (Linux or OSX)
mkdir example & cd example
Next, initialize the node package. Enter the information you are prompted for after entering the following
npm init
Next, install the ExpressJS and https packages
npm install express opengraph-io
Finally, create a server.js file with the contents above and start the app up
node server.js
var url = 'http://cnet.com';
var urlEncoded = encodeURIComponent(url);
var apiKey 'xxxxxxxxxx';
⁄⁄ The entire request is just a simple get request with optional query parameters
var url = 'https://opengraph.io/api/1.1/site' + urlEncoded + '?app_id=' + apiKey;
$('#loadOpenGraphData').click(function(){
$.getJSON(requestUrl, function(json) {
console.log('hey result', siteInfo);
⁄⁄ Throw the object in the console to see what it looks like!
console.log('json', json
⁄⁄ Update the HTML elements!
$('#title').text(json.hybridGraph.title);
$('#description').text(json.hybridGraph.description);
$('#icon').attr('src', json.hybridGraph.image);
});
});