Created
May 3, 2017 14:01
-
-
Save NickDeckerDevs/95711b65488a4ec0d9f309e2abbf70f9 to your computer and use it in GitHub Desktop.
This is what I'm using to upload a csv to replace a HubDB table/db via CURL and OAUTH2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* table_id - This is the ID of the table/database we are updating | |
* $csv_file_name - This is the full path of the CSV file ($csv_file_name = __DIR__.'/import/2017-05-01-FL.csv') | |
*/ | |
function upload_table($table_id, $csv_file_name) { | |
$accessToken = $_SESSION['accessToken']; | |
$url = "https://api.hubapi.com/hubdb/api/v1/tables/$table_id/import"; | |
$csv_file = new CURLFile($csv_file_name,'text/csv'); | |
$config_json = '{"resetTable":true,"skipRows":1,"format":"csv","columnMappings":[{"source":1,"target":1},{"source":2,"target":2},{"source":3,"target":3},{"source":4,"target":4},{"source":5,"target":5},{"source":6,"target":6},{"source":7,"target":7},{"source":8,"target":8},{"source":9,"target":9},{"source":10,"target":10},{"source":11,"target":11},{"source":12,"target":12},{"source":13,"target":13}]};type=application/json'; | |
$post_data = array( | |
"file" => $csv_file, | |
"config" => $config_json | |
); | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_VERBOSE, true); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: Bearer $accessToken", "Content-type:multipart/form-data"]); | |
$response = curl_exec($curl); | |
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
curl_close($curl); | |
if ( $status != 200 ) { | |
$message = "Error: call to URL $url failed with status $status, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl); | |
} else { | |
$message = is_json($response) ? decode_upload_response($response) : 'There Was an Error Understanding Response: '.$response; | |
} | |
return $message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment