$f) { if ($to_object) { $_res->{$f} = $fields[$key]; } else { $_res[$f] = $fields[$key]; } } $res[] = $_res; } return $res; } public static function parse_json($data_string) { $json = json_decode($data_string, true); $fields = $json["head"]["vars"]; $results = $json["results"]["bindings"]; $res = array(); $fields = is_array($fields)? $fields : array($fields); $results = is_array($results)? $results : array($results); foreach ($results as $result) { $row = array(); foreach ($fields as $field) { $row[$field] = $result[$field]["value"]; } $res[] = $row; } return $res; } public static function build_restful_url($url, $params){ $url .="?"; foreach ($params as $key=>$value){ $url .= "$key=".SparqlUtil::encode($value)."&"; } return $url; } public static function encode($url){ $url = urlencode($url); return $url; } /* * create prefixes for querying local */ public static function createPrefixes() { global $smwgNamespace; $pref = "BASE <".$smwgNamespace.">\n"; $pref .= "PREFIX article: <".$smwgNamespace.">\n"; $pref .= "PREFIX a: <".$smwgNamespace.">\n"; $pref .= "PREFIX property: <".$smwgNamespace."Property:>\n"; $pref .= "PREFIX prop: <".$smwgNamespace."Property:>\n"; $pref .= "PREFIX category: <".$smwgNamespace."Category:>\n"; $pref .= "PREFIX cat: <".$smwgNamespace."Category:>\n"; $pref .= "PREFIX rdfs: \n"; $pref .= "PREFIX rdf: \n"; $pref .= "PREFIX fn: \n"; $pref .= "PREFIX afn: \n"; return $pref; } /* * reads a file (using a proxy if specified) */ public static function readQueryOutput($url, $proxy_ip, $proxy_port, $type) { if (isset($proxy_ip)) { $fp = fsockopen($proxy_ip, $proxy_port, &$errno, &$errstr); $data = ""; if ($fp) { $out = "GET $url HTTP/1.1\r\n"; if ($type == "json") { $out .= "Accept: application/sparql-results+json \r\n\r\n"; } else { $out .= "\r\n"; } fputs($fp, $out); $output = ""; $reading_headers = true; while (!feof ($fp)) { $curline = fgets($fp, 4096); if ($curline=="\r\n") { $reading_headers = false; } if (!$reading_headers) { $output .= $curline; } } fclose($fp); return $output; } } else { $header = 'Accept: */*'; if ($type == "json") { $header = 'Accept: application/sparql-results+json'; } $opts = array('http' => array( 'method' => 'GET', 'header' => $header, ) ); $context = stream_context_create($opts); $output = file_get_contents($url, false, $context); return $output; } return false; } }