Graph Commons is a collaborative 'network mapping' platform and a knowledge base of relationships. You can map relationships at scale and unfold the mystery about complex issues that impact you and your community.
See more about here.
Before beginning;
- Set your autoloader properly or use composer
- Use PHP >= 7.0.0 (see for other PHP < 7 support here)
- Handle each action in
try/catch
blocks - On README,
dump
meansvar_dump
Notice: See Graph Commons's official documents here before using this library.
# composer
~$ composer require graphcommons/graphcommons-php7
// composer.json
{"require": {"graphcommons/graphcommons-php7": "~1.0"}}
// manual, after cloning to /path/to/GraphCommons
$autoload = require('/path/to/GraphCommons/Autoload.php');
$autoload->register();
Configuration is optional but you can provide all these options;
// dump whole stream while requests
$config['debug'] = false;
// stream blocking
$config['blocking'] = true;
// timeout for read
$config['timeout_read'] = 5;
// timeout for connection
$config['timeout_connection'] = 5;
use GraphCommons\{
GraphCommons,
Graph\Graph,
Graph\Signal,
Graph\SignalCollection
};
// init gc object
$gc = new GraphCommons('<YOUR API KEY>', $config=[]): GraphCommons
GET /api/v1/status
dump $gc->api->status(): array
Notice: You can see each graph data as JSON requesting https://graphcommons.com/graphs/<GRAPH ID>.json
.
GET /api/v1/graphs/:id
$graph = $gc->api->getGraph('<GRAPH ID>'): Graph
dump $graph->id: string
dump $graph->image->path: string
dump $graph->license->type: string
dump $graph->layout->springLength: int
// iteration over users, nodes, edges, nodeTypes, edgeTypes
foreach ($graph->users as $user) {
print $user->id ."\n";
}
POST /api/v1/graphs
$graph = $gc->api->addGraph((function() {
$graph = new Graph();
$graph->setName('Person Graph');
$graph->setDescription('The Person Graph!');
$graph->setStatus(Graph::STATUS_DRAFT);
$graph->setSignals(SignalCollection::fromArray([
[
'action' => Signal::NODE_CREATE,
'parameters' => [
'name' => 'Ahmet',
'type' => 'Person']],
[
'action' => Signal::EDGE_CREATE,
'parameters' => [
'from_name' => 'Ahmet',
'from_type' => 'Person',
'to_name' => 'Burak',
'to_type' => 'Person',
'name' => 'COLLABORATED',
'weight' => 2]],
]));
return $graph;
})()): Graph
PUT /api/v1/graphs/:id/add
$graph = $gc->api->addGraphSignal(
'<GRAPH ID>', SignalCollection::fromArray([
[
'action' => Signal::EDGE_CREATE,
'parameters' => [
'from_name' => 'Ahmet',
'from_type' => 'Person',
'to_name' => 'Fatih',
'to_type' => 'Person',
'name' => 'COLLABORATED',
'weight' => 2],
],
])): Graph
GET /api/v1/graphs/:id/types
$graphTypes = $gc->api->getGraphTypes('<NODE ID>'): array
GET /api/v1/graphs/:id/edges
$data = $gc->api->isGraphEdgeExists('<NODE ID>',
'<FROM ID>', '<TO ID>', $directed=true): bool
GET /api/v1/nodes/:id
$graphNode = $gc->api->getNode('<NODE ID>'): GraphNode
GET /api/v1/nodes/search
$graphNodes = $gc->api->getNodes([
'query' => 'kerem',
'limit' => 1,
]): GraphNodes
try {
$gc->api->getGraph('nö!');
} catch (\Throwable $e) {
// 404
print $e->getCode() ."\n";
// API error: code(404) message(Graph not found)
print $e->getMessage() ."\n";
// print original response status
print $gc->client->response->getStatus() ."\n";
// print original response body
print $gc->client->response->getBody() ."\n";
}