Skip to content
summercyy edited this page May 28, 2017 · 1 revision

Example

Now you have the basic idea on how to use our APIs to connect gStore. Yet you might be still a little confused. Here we provide a simple demo to show you what to do explicitly.

Let’s say, you need to use gStore in a web project. PHP is a popular generalpurpose scripting language that is especially suited to web development. So, using our PHP API can meet your requirements. Here is what we implement.

First, get your web server ready so it can run PHP files. We won’t give detailed instructions on this step here. You can easily google it according to your web server(for example, Apache or Nginx, etc.)

Next, go to your web document root(usually in /var/www/html or apache/htdocs, you can check it in config fle), and create a folder named ”Gstore”.

Then copy the GstoreConnector.php file into it. Create a ”PHPAPI.php” file. Edit it like below:

<?php

include( 'GstoreConnector.php');
$host = '127.0.0.1';
$port = 3305;
$dbname = $_POST["databasename"];
$sparql = $_POST["sparql"];
$format = $_POST["format"];
$load = new Connector($host, $port);
$load->load($dbname);
$query = new Connector($host, $port);
$result = $query->query($sparql);
switch ($format) {
    case 1:
        $array = explode("<", $result);
        $html = '<html><table class="sparql" border="1"><tr><th>' . $array[0] . "</th></tr>";
        for ($i = 1; $i < count($array); $i++) {
            $href = str_replace(">", "", $array[$i]);
            $html.= '<tr><td><a href="' . $href . '">' . $href . '</a></td></tr>';
        }
        $html.= '</table></html>';
        echo $html;
        exit;

    case 2:
        $filename = 'result.txt';
        header("Content-Type: application/octet-stream");
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        echo $result;
        exit;

    case 3:
    $filename = 'result.csv';
    header("Content-Type: application/octet-stream");
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    $array = explode("<", $result);
    echo $array[0];
    for ($i = 1; $i < count($array); $i++) {
        $href = str_replace(">", "", $array[$i]);
        echo $href;
    }
    exit;
}
?>

This PHP file get three parametres from a website, including databasename, sparql and output format. Then it use our PHP API to connect gStore and run the query. Finally, the ”switch” part gives the output.

After that, we need a website to collect those imformation(databasename, sparql and output format). We create a html file and use a form to do it, just like below:

<form id="form_1145884" class="appnitro" method="post" action="PHPAPI.php">
    <div class="form_description">
        <h2>Gstore SPARQL Query Editor</h2>
        <p></p>
    </div>
    <ul>
        <li id="li_1" >
            <label class="description" for="element_1">
                Database Name
            </label>
            <div>
                <input id="element_1" name="databasename" class="element text medium"
                       type="text" maxlength="255" value="dbpedia_2014_reduce">
                </input>
            </div>
        </li>
        <li id="li_3">
            <label class="description" for="element_3">Query Text </label>
            <div>
                <textarea id="element_3" name="sparql" class="element textarea large">
                    SELECT DISTINCT ?uri
                    WHERE {
                        ?uri <type> <Astronaut> .
                        { ?uri <nationality> <Russia> . }
                        UNION
                        { ?uri <nationality> <Soviet_Union> . }
                    }
                </textarea>
            </div>
        </li>
        <li id="li_5" >
            <label class="description" for="element_5">
                Results Format
            </label>
            <div>
                <select class="element select medium" id="element_5" name="format">
                    <option value="1" selected="ture">HTML</option>
                    <option value="2" >Text</option>
                    <option value="3" >CSV</option>
                </select>
            </div>
        </li>
        <li class="buttons">
            <input type="hidden" name="form_id" value="1145884" />
            <input id="saveForm" class="button_text" type="submit" name="submit" value="Run Query" />
        </li>
    </ul>
</form>

As you can see in the code, we use an <input> element to get the database name, and <texarea> for sparql, <select> for output format. <form> lable has an attribute ”action” which specifies which file to execute. So, when you click the ”submit” button, it will call PHPAPI.php file and post the values from the form.

Finally, don’t forget to start gserver on your server.

Previous

API Explanation

Next

FAQ