My upload json file is like this :
{“email_verified” : false,“email”: “lol@gmail.com”,“username” : “lol@gmail.com”,“user_metadata”: {“given_name”: “john”,“family_name”:“doe”,“address”: “123 asda”,“add_address”: “qwe”,“postal_code”: “123123”,“office”: “accenture”,“role”: “editor”},“blocked”: true}]
and it works fine in user import export extension in auth0.com
but php management api does not work
here is the code :
<h4 style="color:red;">Only csv and json file types are accepted</h4>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="file" id="inputFile" accept=".csv, .json">
<input type="submit" name="import_user" value="Start importing user">
<input type="submit" name="clear" value="Clear">
</form>
if (isset($_POST'import_user']))
{
if ($_POST'MAX_FILE_SIZE'] < $_FILES'file']'size'])
$error = "File size must be less than 500kb";
if ($_FILES'file']'size'] == 0)
$error = "File is empty";
if (!isset($error) && $_FILES'file']'error'] == 0)
{
$fileName = $_FILES'file']'name'];
$extension = $_FILES'file']'type'];
if ($extension == 'test/csv')
{
$array = array_map("str_getcsv", explode("\n", $data));
$data = json_encode($array);
}
if (is_uploaded_file($_FILES'file']'tmp_name'])):
$data = file_get_contents($_FILES'file']'tmp_name']);
echo $data;
$fields =
"users" => $data,
"connection_id" => "con_ZM1P7U6bpYh4h0zS",
"upsert" => true,
"external_id" => "",
"send_completion_email" => true
];
$file = http_build_query($fields);
dump(user_import_job($file));
endif;
}
}
function user_import_job($file) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://efficient-ip.eu.auth0.com/api/v2/jobs/users-imports",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $file,
CURLOPT_HTTPHEADER => array(
"authorization: Bearer " . get_token(),
"content-type: multipart/form-data; boundary=------"
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) return "cURL Error #:" . $err;
else return json_decode($response);
}
I got an error 400 invalid multipart payload format