How to use EasyRDF to save RDF Turtle file from SPARQL query

Sam Source

I am using EasyRDF lib and using this code.

I want similar code, but when user inputs SPARQL endpoint, I want to save some statistical information (i.e., number of triples, unique predicates/subjects) to a file with a pattern like --

dcterms:triples "12345"
dcterms:predicats "1234"


 <form role="form" id="myform" method="post" >
 <div id="collapseOne-4" class="panel-collapse collapse in">
<div class="panel-body">
 <div class="form-group">
 <input type="text" class="form-control" placeholder="Title" id="title" name="title"  size="20" required>
 <label> Description</label>
 <textarea class="form-control" rows="3" placeholder=" Description " id="des" name="des"  size="200" required></textarea>
     <input type="url" class="form-control" placeholder="Endpoint "  id="endpoint" name="endpoint" size="50"  required pattern="https?://.+"> 
<label>Total Number of Triples</label>
 <input type="text" id="triples" class="form-control" 
placeholder="Disabled input" disabled >

and code for input data is

 $myFile = "sample.ttl";
  if (isset($_POST['uri'])) {

    $graph = new \EasyRdf_Graph();
    $graph->addLiteral($_POST['uri'], 'dcterms:title', 
    $graph->addLiteral($_POST['uri'], 'dcterms:Description', 
    $graph->addLiteral($_POST['uri'], 'void:endpoint', 
$graph->addLiteral($_POST['uri'], 'void:triples', 
    $data = $graph->serialise($format);

  $fp = fopen ($myFile, "a"); 
    if ($fp) {
     fwrite ($fp, $data);
     fclose ($fp);
echo ("File written");
    else {
echo ("File was not written");


What I am not achieving yet is, what if from user-provided SPARQL endpoint at the same time (before submission) I collect some stats via SPARQL query and afterwards add in the same file (after submission)? My issue is how to bind the query below with the (user input) SPARQL endpoint in order to query and get and save the results.

$result = $sparql->query(
    'SELECT (COUNT(*) AS ?triples) where { ?s ?p ?o  


Currently output looks like this

  @prefix dcterms: <> .
    @prefix foaf: <> .
    @prefix void: <> .

   dcterms:title "MyDataset" ;
   dcterms:Description "This is dataset description" ;
   void:endpoint "" ;
   void:triples ""

But what I want is

   dcterms:title "MyDataset" ;
   dcterms:Description "This is dataset description" ;
   void:endpoint "" ;
   void::triples "12345"

Any help will be highly appreciated and thanks in advance.



comments powered by Disqus