我正在尝试通过批量插入演练在salesforce上批量插入CSV

时间:2019-03-04 12:51:33

标签: php salesforce

我正在尝试将联系人的CSV文件上传到SALESFORCE,但是CSV中的数据有问题。

    $file_name = $_FILES["fileToUpload"]["tmp_name"];

    $csv = explode("\n", file_get_contents($file_name));

    $res = array_values($csv);

    $upload_url = $instance_url . "/services/data/v44.0/jobs/ingest/$decode_res[id]/batches/";

    $curl = curl_init($upload_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $auth_header);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($res));
    $curl_result = curl_exec($curl);

    print_r(json_decode($curl_result, true));
    $curl_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

1 个答案:

答案 0 :(得分:0)

Found the solution
The data is need to be passed in csv format
example ==>
FirstName,LastName,Title,ReportsTo.Email,Birthdate,Description
Tom,Jones,Senior Director,buyer@salesforcesample.com,1940-06-07Z,"Self-described"

    $file_name = $_FILES["fileToUpload"]["tmp_name"];
    $csv = explode("\n", file_get_contents($file_name));

    $res = array_values(array_filter($csv));

    $string = "";

    foreach (array_chunk($res, 1) as $value) {
        $string .= $value[0] . "\n";
    }

    $upload_url = 
    "$instance_url/services/data/v44.0/jobs/ingest/$decode_res[id]/batches/";
    $curl = curl_init($upload_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $auth_header);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($curl, CURLOPT_POSTFIELDS, $string);
    $curl_result = curl_exec($curl);