Skip to content

Instantly share code, notes, and snippets.

@NickDeckerDevs
Last active December 9, 2019 09:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickDeckerDevs/d1f060df8e376e8f8996656b7f05ef61 to your computer and use it in GitHub Desktop.
Save NickDeckerDevs/d1f060df8e376e8f8996656b7f05ef61 to your computer and use it in GitHub Desktop.
Two attempts at endpoints using oauth2 token
function upload_table($table_id, $csv_file_name) {
$accessToken = $_SESSION['accessToken'];
// /hubdb/api/v1/tables/:tableId/import
$url = "https://api.hubapi.com/hubdb/api/v1/tables/$table_id/import";
$postBody = [
"file" => "@$csv_file_name;type=text/csv",
"config" => [
"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]
]
]
];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: Bearer $accessToken", "Content-type: application/json"]);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postBody));
$response = curl_exec($curl);
/* $response:
* HTTP/1.1 100 Continue
*
* HTTP/1.1 415 Unsupported Media Type
* Access-Control-Allow-Credentials: true
* Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Accept-Charset, Accept-Encoding
* Access-Control-Allow-Methods: GET
* X-Trace: 1B6AA19E52117B82AE62DF3A5E27A26432384EF3196B0238DCDF32CE54
* Content-Length: 0
* Date: Tue, 02 May 2017 07:25:33 GMT
* Connection: close
*/
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
$error = "Error: call to URL $url failed with status $status, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl);
}
echo "HTTP status $status updating account<br/><br/>";
curl_close($curl);
}
/* working update */
function update_table_name($table_id, $name) {
set_time_limit(0);
$accessToken = $_SESSION['accessToken'];
$url = "https://api.hubapi.com/hubdb/api/v1/tables/$table_id";
$postBody = json_encode(["name" => "$name"]);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: Bearer $accessToken", "Content-type: application/json"]);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postBody);
$response = curl_exec($curl);
d($response);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
d($status);
if ( $status != 200 ) {
$error = "Error: call to URL $url failed with status $status, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl);
// $logs = logErrorMessage($error);
d($error);
}
echo "HTTP status $status updating account<br/><br/>";
curl_close($curl);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment