LoginSignup
0
0

More than 1 year has passed since last update.

HubSpot Client Library でインポートしたい

Last updated at Posted at 2022-04-14

はじめに

MAサービスであるHubSpotは、APIにアクセスするためのクライアントライブラリを公開しているが、ドキュメントがいまいちわかりにくく、さらにインポートに関してはリクエストのサンプルコードが掲載されていない。ライブラリを使ったインポート機能を開発する際に非常に苦労した。
そのため、備忘録を残しておく。

サンプルコード

<?php
// アップロードするファイル
$file = new \SplFileObject("./import.csv");

// データ
$data = [
  "name" => "インポート名",
  "dateFormat" => "YEAR_MONTH_DAY", // (*)ポイント
  "files" => [
    [
      "fileName" => $file->getBasename(), // (*)ポイント
      "fileFormat" => "CSV",
      "fileImportPage" => [
        "hasHeader": true,
        "columnMappings" => [
          [
            "columnObjectTypeId" => "0-1",
            "columnName" => "UserEmail", // CSVのヘッダ
            "propertyName" => "email", // HubSpotのプロパティ名
            "idColumnType" => "HUBSPOT_ALTERNATE_ID",
          ],
          // 他のマッピング情報は省略
        ],
      ],
    ],
  ]
];

$client = \HubSpot\Factory::createWithApiKey("APIキー");
$client->crm()->imports()->coreApi()->create($file, json_encode($data)); // (*)ポイント

ポイント

  1. "dateFormat" はドキュメントに書かれている位置は誤りのようで、サンプルコードの位置に書くと正しく動作する。
  2. "fileName" はパスを含めないファイル名を指定する。
  3. create()メソッドの第1引数は、SplFileObjectではなく、ファイルのパス(string型)でもよい。つまり$file->getRealPath()としても問題なく動く。ファイルが複数ある場合はSplFileObjectの配列またはファイルパスの配列を渡す。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0