2016-06-27 70 views
0

我正在努力如何从某个特定项目导出仪表板,如此link显示出来。GoodData导出仪表板API

我所做的代码生成了正确下载的链接,就像预期的那样。但是,导出的PDF文件不会像我的目标那样过滤值。

我的问题发生此评论如下:

// Execute the dashboard with context 

...这里我supose设置,像上面的链接显示的是“影响仪表盘的所有过滤器”。但它没有指定如何设置过滤器的值。

我做了所有在PHP(替换所有的“{一些,事情}”,以使此代码工作)代码:

<?php 

    $login = '{email}'; 
    $pass = '{password}'; 

    $headers = array('accept' => 'Accept: application/json','content-type' => 'Content-Type: application/json; charset=utf-8'); 
    $data = json_encode(array('postUserLogin'=>array('login'=> $login,'password'=> $pass,'remember'=>0))); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    curl_setopt($ch, CURLOPT_URL, "https://secure.gooddata.com/gdc/account/login"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    $output = curl_exec($ch); 
    $AuthSST = substr($output, strrpos($output, "GDCAuthSST=")+strlen("GDCAuthSST="), 17); 

    $headers = array('accept' => 'Accept: application/json', 
        'content-type' => 'Content-Type: application/json; charset=utf-8', 
        'cookie:' => 'Cookie: $Version=0; GDCAuthSST='.$AuthSST.' $Path=/gdc/account'); 

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_HTTPGET, 1); 
    curl_setopt($ch, CURLOPT_URL, "https://secure.gooddata.com/gdc/account/token"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    $output = curl_exec($ch);   

    $start = strrpos($output, "GDCAuthTT=")+strlen("GDCAuthTT="); 
    $finish = strrpos($output, "Path=/gdc"); 

    $GDCAuthTT = substr($output, $start, ($finish-$start)); 

    $headers = array('accept' => 'Accept: application/json', 
        'content-type' => 'Content-Type: application/json; charset=utf-8', 
        'cookie:' => 'Cookie: $Version=0; GDCAuthTT='.$GDCAuthTT.' $Path=/gdc/account'); 

    // Authentication finished, now start the dashboard export API 

    // Execute the dashboard with context 

    $project_id = '{project_id}'; 
    $filter_id = '{filter_id}'; 
    $filter_obj = '{0000}'; 
    $dash_obj = '{0000}'; 
    $url = 'https://secure.gooddata.com/gdc/internal/projects/'.$project_id.'/executionContexts'; 

    // Inside filters: constraint, promptUri, uri, id ||||||||| Inside executionContext: dashboard, links, name, type, user 

    $bode = json_encode(array('executionContext' => array('filters' => array(array(
          'uri' => '/gdc/md/'.$project_id.'/obj/'.$filter_obj, // /elements?id=0000 
          'id' => $filter_id)), 
          'dashboard' => '/gdc/md/'.$project_id.'/obj/'.$dash_obj, 
          'type' => 'export' 
         ))); 

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $bode); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    $output = curl_exec($ch); 

    // Export the Dashboard 

    $dash_tab = 'ae2b1ab3a87c'; 

    $start = strrpos($output, "\"uri\":\"")+7; 
    $finish = strrpos($output, "\"}"); 

    $url = "https://secure.gooddata.com/gdc/projects/".$project_id."/clientexport"; 

    $request_url = "https://secure.gooddata.com/dashboard.html#"; 
    $project = "project=/gdc/projects/".$project_id; 
    $dashboard = "&dashboard=/gdc/md/".$project_id."/obj/".$dash_obj; 
    $tab = "&tab=".$dash_tab."&export=1"; 
    $ctx = "&ctx=".substr($output, $start, ($finish-$start)); 

    $bode2 = json_encode(array('clientExport'=>array('url'=>$request_url.$project.$dashboard.$tab.$ctx, 'name'=>'{Dash_Name}'))); 

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $bode2); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    $output = curl_exec($ch); 

    // Poll the URL from response Result: 

    $start = strrpos($output, "\"poll\":\"")+8; 
    $finish = strrpos($output, "\"}"); 
    $poll = "https://secure.gooddata.com".substr($output, $start, ($finish-$start)); 

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_HTTPGET, 1); 
    curl_setopt($ch, CURLOPT_URL, $poll); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 

    $output = curl_exec($ch); 

    echo "Link: ".$poll; 
    echo "<br>Output: ".$output; 

    // Wait 90 seconds and then make the download, otherwise it returns 202 http code 

    sleep(90); 

    $opts = array('http' => 
     array(
     'method' => 'GET', 
     'header' => "Accept: application/json\r\n". 
      "Content-Type: application/json; charset=utf-8\r\n". 
      'Cookie: $Version=0; GDCAuthTT='.$GDCAuthTT.' $Path=/gdc/account'."\r\n", 
     'timeout' => 6000 
    ) 
    ); 

    $context = stream_context_create($opts); 
    $file = file_get_contents($poll."?download=true", false, $context); 
    file_put_contents("Dashboard.pdf", $file); 

    echo "<br><br>Link used: ".$poll."?download=true"; 

    $errors= error_get_last(); 
    echo "<br><br>".$errors['type']; 
    echo "<br>".$errors['message']; 

    curl_close($ch); 
?> 
+0

请不要使用黑客如 '$ AuthSST = SUBSTR($输出,strrpos($输出, “GDCAuthSST =”)+ strlen的( “GDCAuthSST =”),17);' 我们可以改变任何时候GDCAuthSST cookie的长度(实际上我们很快就会完成)。正确处理cookie或使用基于标头的身份验证(即使用验证级别2) - 有关更多详细信息,请参阅[http://developer.gooddata.com/api#/reference/authentication](API doc)。 –

+0

哇,谢谢!我打算这么做^^ –

回答

1

您可以指定使用的过滤器定义constraint对象中的过滤器值。确切的约束格式取决于过滤器的类型。如果过滤器是list类型的约束格式为:

"constraint": { 
    "type": "list", 
    "elements": [ 
     "/gdc/md/{project_id}/obj/{attribute_id}?id={element_id}", 
     ... 
    ] 
} 

如果它是一个日期滤波器的约束是这样的:

"constraint": { 
    "from": "2006-07-01", 
    "to": "2008-03-31", 
    "type": "interval" 
} 

"constraint": { 
    "from": -5, 
    "to": 0, 
    "type": "floating" 
} 

整个executionContext的实施例对象POST与一个list筛选器选择了2个值:

{ 
    "executionContext": { 
    "filters": [ 
     { 
     "uri": "/gdc/md/{project_id}/obj/{attribute_id}", 
     "constraint": { 
      "type": "list", 
      "elements": [ 
      "/gdc/md/{project_id}/obj/{displayForm_id}/elements?id={element_id_1}", 
      "/gdc/md/{project_id}/obj/{displayForm_id}/elements?id={element_id_2}" 
      ] 
     }, 
     "id": "{filter_id}" 
     } 
    ], 
    "dashboard": "/gdc/md/{project_id}/obj/{dashboard_id}", 
    "type": "export" 
    } 
} 

您可以看到过滤器约束的有效示例,例如在handling drillscreating of saved view的文档中。

+0

谢谢!它工作正常! –

+0

@Álvarook - 请将答案标记为正确,以便其他人知道 – akloboucnik