使用 php(多部分表单数据 post)将 .pbix (power BI) 文件导入工作区

Import .pbix (power BI) file into the workspace using php (multipart form-data post)

我想将本地 pbix 文件导入到在 azure power bi 帐户中创建的工作区。我已经使用 REST API 创建了 workspaceId。但是,当我尝试导入给出 200 ok 状态而不是 202 accepted response with Id 的 pbix 文件时。

这是我遵循的参考代码enter link description here

POST https://api.powerbi.com/v1.0/collections/mypbiapp/workspaces/32960a09-6366-4208-a8bb-9e0678cdbb9d/imports?datasetDisplayName=mydataset01 Authorization: AppKey MpaUgrTv5e... Content-Type: multipart/form-data; boundary="A300testx"

--A300testx Content-Disposition: form-data

{the content (binary) of .pbix file} --A300testx--

我使用 php curl 请求来调用 Rest API 下面显示了我尝试过的代码,

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.powerbi.com/v1.0/collections/XXXXXX/workspaces/XXX-XXX-XXX-XXXXXXXX/imports?datasetDisplayName=mydataset01');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postData = array(
        'datafile' => '@C:\Users\Desktop\report1.pbix',
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
        "Authorization: AppKey R97v4Fe5=="
) );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
echo $response = curl_exec($ch);

curl_close ( $ch );

作为响应,我收到状态代码 200 ok json

{"id":"0331a80d-6f23-4626-9624-1f6b98ce373a"}

但是这个新数据集不是在 workspaceID 中创建的。请帮我找到这里的问题。

这是使用 php curl 的多部分表单数据 post 的答案,这段代码对我有用

$name = 'report1';
$file = 'report1.pbix';
$boundary = "----------BOUNDARY";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.powerbi.com/v1.0/collections/XXXXXX/workspaces/XXX-XXX-XXX-XXXXXXXX/imports?datasetDisplayName=mydataset01');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$postdata .= "--" . $boundary . "\r\n";
$postdata .= "Content-Disposition: form-data; name=\"" . $name . "\"; filename=\"" . $file . "\"\r\n";
$postdata .= "Content-Type: application/octet-stream\r\n\r\n";
$postdata .= file_get_contents($file);
$postdata .= "\r\n";
$postdata .= "--" . $boundary . "--\r\n";

curl_setopt($ch, CURLOPT_POSTFIELDS, $$postdata);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
        "Authorization: AppKey R97v4Fe5==",
        'Content-Type: multipart/form-data; boundary='.$boundary,
        'Content-Length: ' . strlen($postdata)
) );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
echo $response = curl_exec($ch);

curl_close ( $ch );

这是给定解决方案的现成可用版本

public function import($access_key, $workspace_collection_name, $workspace_id, $file_path, $file_name, $display_name, $nameConflict = 'Overwrite')
{

    $boundary = "----------BOUNDARY";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.powerbi.com/v1.0/collections/$workspace_collection_name/workspaces/$workspace_id/imports?datasetDisplayName=$display_name&nameConflict=$nameConflict");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $postdata = '';
    $postdata .= "--" . $boundary . "\r\n";
    $postdata .= "Content-Disposition: form-data; name=\"" . $file_name . "\"; filename=\"" . $file_path . "\"\r\n";
    $postdata .= "Content-Type: application/octet-stream\r\n\r\n";
    $postdata .= file_get_contents($file_path);
    $postdata .= "\r\n";
    $postdata .= "--" . $boundary . "--\r\n";

    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Authorization: AppKey " . $access_key,
        'Content-Type: multipart/form-data; boundary=' . $boundary,
        'Content-Length: ' . strlen($postdata)
    ));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
    if (curl_error($ch)) {
        return 'curl error';
    }
    curl_close($ch);

    return $response;

}