"./arc/", "config_path"=>"./arc_config.php" ); $api = new ARC_api($api_args); $api->db_connect(); //read the JSON template file require('json.php'); $json = new Services_JSON(); $data = $json->decode(file_get_contents('template.json')); //get all the items - we will create a query for each item we find $template_items = $data->items; //empty the items array from our original JSON file - we will fill it with results $data->items = array(); //iterate over the template items, creating a query foreach($template_items as $item) { //the subject of the graph - can be preset by the user, or left blank //so we either turn it into or ?id $subject = (isset($item->id) && !empty($item->id))? '<'.$item->id.'>' : '?id'; // get the human readable type label $type_name = $item->type; // get the type's IRI $type = '<'.$data->types->$type_name->uri.'>'; //get the label's property name $label_prop_name = $item->label; //get the label's property's IRI $label_prop = '<'.$data->properties->$label_prop_name->uri.'>'; //for storing WHERE conditions $triples = array(); //vars in the select statement $vars = array(); //jsonc is a parameter that the ARC store accepts to strip out // unwanted meta properties from the JSON results object $jsonc = array('id'); //iterate over the template item's properties foreach($item as $k => $v) { //determine node type of object if(empty($v)) { $object = '?'.$k; } elseif( (substr($v,0,1)=='<') && (substr($v,-1,1)=='>') ) { $object = $k; } else { $object ='"'.$v. '"'; } $vars[]='?'.$k; $jsonc[]= $k; if(!in_array($k, array('type','id','label')) ) { $property = $data->properties->$k->uri; $triples[] = <<<_SPQL_ $subject <{$property}> $object . _SPQL_; } } $sparql = "SELECT ?id ".implode(" ", $vars)." \r\n WHERE { \r\n\t GRAPH ?id { $subject $type . \r\n $subject ?type . \r\n $subject $label_prop ?label . \r\n ?id $label_prop ?label . \r\n ".implode("\r\n", $triples)." \r\n\t } \r\n } LIMIT 100"; $args=array( "result_type"=>'json', // (rows|json|xml|single|rows_n_count|row_count|sql) "query"=>$sparql, "result_type_args"=>array("jsonc"=> implode(",",$jsonc),) ); $qr=$api->query($args); $result = $json->decode($qr['result']); foreach($result->results->bindings as $row) { //make the rdf:type human readable $row->type = $type_name; $data->items[] = $row; } } //output header("Content-type: text"); echo stripslashes($json->encode($data)); ?>