GoodData导出仪表板API

时间:2016-06-27 21:02:35

标签: php gooddata

我正在研究如何从某个特定项目中导出仪表板,如link所示。

我制作的代码生成正确下载的链接,就像预期的那样。但是导出的PDF文件不会像我瞄准那样过滤掉过滤的值。

我的问题发生在这个评论下面:

// Execute the dashboard with context

...我设置“影响仪表板的所有过滤器”,就像上面的链接所说的那样。但它没有指定如何设置过滤器的值。

我在PHP中创建了所有代码(替换所有“{some-thing}”以使此代码有效):

<?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);
?>

1 个答案:

答案 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的文档中。