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 " + x.hybridGraph.title);
Console.WriteLine("Description " + x.hybridGraph.description);
Console.WriteLine("Image " + 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 ' . $siteInformation['hybridGraph']['title'] . '\n';
print 'Description ' . $siteInformation['hybridGraph']['description'] . '\n';
print 'Logo URL ' . $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 requestUrl = 'https://opengraph.io/api/1.1/site/' + urlEncoded + '?app_id=' + apiKey;
$('#loadOpenGraphData').click(function(){
$.getJSON(requestUrl, function(json) {
console.log('hey result', json);
// 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);
});
});
import requests
# Replace with your actual app_id
app_id = 'xxxxxx'
target_url = 'https://github.com'
# Make the API request
api_url = f'https://opengraph.io/api/1.1/site/{requests.utils.quote(target_url, safe="")}'
response = requests.get(api_url, params={'app_id': app_id})
if response.status_code == 200:
data = response.json()
hybrid = data.get('hybridGraph', {})
print(f"Title: {hybrid.get('title')}")
print(f"Description: {hybrid.get('description')}")
print(f"Image: {hybrid.get('image')}")
else:
print(f"Error: {response.status_code}")
First, make sure you have Python 3 installed. Then install the requests library
pip install requests
Save the code above to a file (e.g., opengraph_example.py) and run it
python opengraph_example.py
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
func main() {
appID := "xxxxxx" // Replace with your app_id
targetURL := "https://github.com"
apiURL := fmt.Sprintf(
"https://opengraph.io/api/1.1/site/%s?app_id=%s",
url.QueryEscape(targetURL),
appID,
)
resp, err := http.Get(apiURL)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
if hybrid, ok := result["hybridGraph"].(map[string]interface{}); ok {
fmt.Printf("Title: %v\n", hybrid["title"])
fmt.Printf("Description: %v\n", hybrid["description"])
fmt.Printf("Image: %v\n", hybrid["image"])
}
}
Make sure you have Go installed (go.dev). Save the code to main.go and run it
go run main.go