Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

graphcommons/graphcommons-php7-archive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

GraphCommons (for PHP7)

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 means var_dump

Notice: See Graph Commons's official documents here before using this library.

Installation

# 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

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;

Usage

use GraphCommons\{
    GraphCommons,
    Graph\Graph,
    Graph\Signal,
    Graph\SignalCollection
};

// init gc object
$gc = new GraphCommons('<YOUR API KEY>', $config=[]): GraphCommons
Check API Status

GET /api/v1/status

dump $gc->api->status(): array
Get a Graph

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";
}
Add a New Graph

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
Add a New Graph Signal

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 Graph Types

GET /api/v1/graphs/:id/types

$graphTypes = $gc->api->getGraphTypes('<NODE ID>'): array
Check a Graph Edge

GET /api/v1/graphs/:id/edges

 $data = $gc->api->isGraphEdgeExists('<NODE ID>',
   '<FROM ID>', '<TO ID>', $directed=true): bool
Get a Node

GET /api/v1/nodes/:id

$graphNode = $gc->api->getNode('<NODE ID>'): GraphNode
Get (search) Nodes

GET /api/v1/nodes/search

$graphNodes = $gc->api->getNodes([
    'query' => 'kerem',
    'limit' => 1,
]): GraphNodes

Error Handling

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";
}

About

PHP7 Wrapper for Graph Commons API/v1

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages