From c0cdbe907a97e6c70a23d71ee165c2a849f068f3 Mon Sep 17 00:00:00 2001
From: Alvaro Graves <alvaro@graves.cl>
Date: Wed, 3 Aug 2011 20:41:44 -0700
Subject: [PATCH] switched from file_get_contents to curl in query function

---
 classes/Endpoint.php | 53 ++++++++++++++++++++++++++------------------
 1 file changed, 32 insertions(+), 21 deletions(-)

diff --git a/classes/Endpoint.php b/classes/Endpoint.php
index baf5906b..a7db5bfd 100644
--- a/classes/Endpoint.php
+++ b/classes/Endpoint.php
@@ -9,27 +9,38 @@ class Endpoint{
   	$this->params = $params;
   }
   
-  public function query($q, $output = 'json'){
-  	global $conf;
-  	$auxoutput = $this->params['output'];
-  	if($output != null){
-  	  $this->params['output'] = $output;
-    }
-  	$context = stream_context_create(array(
-  	  'http' => array('header'=>'Connection: close')));
-  	$params = $this->params;
-  	$params['query'] = $q;
-  	$url = $this->sparqlUrl.'?'.http_build_query($params, '', '&');
-  	$aux = file_get_contents($url, false,$context);
-  	
-  	$this->params['output'] = $auxoutput;
-  	
-  	if(preg_match("/select/i", $q)){
-  	  return json_decode($aux, true);
-  	}
-  	if(preg_match("/describe/i", $q)){
-  	  return $aux;
-  	}  	
+   public function query($q, $output = 'json'){
+        global $conf;
+        $auxoutput = $this->params['output'];
+        $accept = 'application/sparql-results+json';
+        if($output != null){
+          $this->params['output'] = $output;
+        }
+
+        if($output == 'xml'){
+          $accept = 'application/sparql-results+xml';
+        }
+        $c = curl_init();
+        $context = array();
+        $context[0] = 'Connection: close';
+        $context[1] = 'Accept: '.$accept;
+        $params = $this->params;
+        $params['query'] = $q;
+        $url = $this->sparqlUrl.'?'.http_build_query($params, '', '&');
+
+        curl_setopt($c, CURLOPT_URL, $url);
+        curl_setopt($c, CURLOPT_HTTPHEADER, $context);
+        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
+        $aux = curl_exec($c); // execute the curl command 
+        curl_close($c);
+        $this->params['output'] = $auxoutput;
+
+        if(preg_match("/select/i", $q)){
+          return json_decode($aux, true);
+        }
+        if(preg_match("/describe/i", $q)){
+          return $aux;
+        }
   }
   
   public function queryPost($q){
-- 
GitLab