Created
July 24, 2012 07:50
-
-
Save pstoellberger/3168655 to your computer and use it in GitHub Desktop.
Using Saiku from PHP Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/// example by diamonddog ;) | |
class curl_request { | |
private static $opt = array( | |
// CURLOPT_CONNECTTIMEOUT => 20, | |
CURLOPT_RETURNTRANSFER => true, | |
// CURLOPT_FRESH_CONNECT => 1, | |
// CURLOPT_TIMEOUT => 320, | |
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1', | |
); | |
public function exec( $url, $method = 'get', $params = array() ){ | |
$ch = curl_init(); | |
$opts = self::$opt; | |
switch( $method ) { | |
case 'get': | |
$url.= '?'.http_build_query( $params ); | |
break; | |
case 'post': | |
if( count( $params ) > 0 ) { | |
$opts[CURLOPT_POST] = count( $params ); | |
$opts[CURLOPT_POSTFIELDS] = http_build_query( $params ); | |
} | |
break; | |
} | |
$cookie_file = "/tmp/saiku.txt"; | |
$opts[CURLOPT_COOKIEJAR] = $cookie_file; | |
$opts[CURLOPT_COOKIEFILE] = $cookie_file; | |
$opts[CURLOPT_URL] = $url; | |
curl_setopt_array( $ch, $opts ); | |
$result = curl_exec($ch); | |
$info = curl_getinfo($ch); | |
return array( 'response' => $result, 'info' => $info ); | |
} | |
} | |
$base = 'http://192.168.0.17:8080/saiku/rest/saiku/'; | |
$x = new curl_request; | |
// step 1 login | |
$r = $x->exec( $base.'session/' ); | |
$session = json_decode( $r[ 'response' ], true ); | |
if( count( $session ) === 0 ) { | |
// user is not logged in, let's login | |
$r = $x->exec( $base.'session/', 'post', array( 'username' => 'admin', 'password' => 'admin' ) ); | |
if( $r[ 'info' ][ 'http_code' ] == '200' ) { | |
$r = $x->exec( $base.'session/' ); | |
$session = json_decode( $r[ 'response' ], true ); | |
} | |
} else { | |
//var_dump( $session); | |
//echo $session[ 'session_id' ]; | |
} | |
// step 2 fetch stored query | |
$stored_query = 'income_ctr-product'; | |
$qstring = $base.$session['username'].'/repository/'.$stored_query; | |
$r = $x->exec( $qstring ); | |
$mdx = json_decode( $r[ 'response' ], true ); | |
// step 3 post query to server | |
$new_query = rand(0,1000000000); | |
$qstring = $base.$session['username'].'/query/'.$new_query; | |
$r = $x->exec( $qstring, 'post', array( 'xml'=> $mdx['xml'] ) ); | |
$metadata = json_decode( $r[ 'response' ], true ); | |
// step 4 fetch the results | |
$qstring = $base.$session['username'].'/query/'.$new_query.'/result/flattened'; | |
$r = $x->exec( $qstring ); | |
$results = json_decode( $r[ 'response' ], true ); | |
var_dump( $results ); | |
die(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment