質問編集履歴

3

cURLでの動作

2021/07/21 23:38

投稿

RyoS_
RyoS_

スコア4

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ユーザーをHubSpotに追加する`POST /crm/v3/objects/contacts`を実装したいです。
4
4
 
5
- [サイトURL](https://developers.hubspot.jp/docs/api/crm/contacts)
5
+ [https://developers.hubspot.jp/docs/api/crm/contacts](https://developers.hubspot.jp/docs/api/crm/contacts)
6
6
 
7
7
 
8
8
 
@@ -214,6 +214,164 @@
214
214
 
215
215
 
216
216
 
217
+ ## cURLでの実装
218
+
219
+ 公式サイトのPHPでは、cURLコマンドを使用しているのでこちらのコードで検証してみました。
220
+
221
+ cURLでは、うまくいきました。
222
+
223
+ [https://developers.hubspot.jp/docs/api/crm/contacts](https://developers.hubspot.jp/docs/api/crm/contacts)
224
+
225
+
226
+
227
+ ```php:HubspotController.php
228
+
229
+ <?php
230
+
231
+
232
+
233
+ namespace App\Http\Controllers;
234
+
235
+
236
+
237
+ use Illuminate\Http\Request;
238
+
239
+ use GuzzleHttp\Client;
240
+
241
+
242
+
243
+ // Log
244
+
245
+ use Illuminate\Support\Facades\Log;
246
+
247
+
248
+
249
+ class HubSpotController extends Controller
250
+
251
+ {
252
+
253
+ public function store(Request $request)
254
+
255
+ {
256
+
257
+ try {
258
+
259
+ // env
260
+
261
+ $HubSpotApiKey = config("env.hubspot_api_key");
262
+
263
+
264
+
265
+ // HubSpot Value
266
+
267
+ $profileName = "aaa";
268
+
269
+ $profileEmail = "aaa@gmail.com";
270
+
271
+
272
+
273
+ Log::info($profile["name"]);
274
+
275
+ Log::info($profile["email"]);
276
+
277
+
278
+
279
+
280
+
281
+ // Register HubSpot
282
+
283
+ $data = array(
284
+
285
+ "properties" => [
286
+
287
+ "email" => $profileEmail,
288
+
289
+ "firstname" => $profileName
290
+
291
+ ]
292
+
293
+ );
294
+
295
+
296
+
297
+ $formParams = json_encode($data, JSON_UNESCAPED_UNICODE);
298
+
299
+
300
+
301
+ Log::info($formParams);
302
+
303
+
304
+
305
+ $curl = curl_init();
306
+
307
+
308
+
309
+ curl_setopt_array($curl, array(
310
+
311
+ CURLOPT_URL => "https://api.hubapi.com/crm/v3/objects/contacts?hapikey=" . $HubSpotApiKey,
312
+
313
+ CURLOPT_RETURNTRANSFER => true,
314
+
315
+ CURLOPT_ENCODING => "",
316
+
317
+ CURLOPT_MAXREDIRS => 10,
318
+
319
+ CURLOPT_TIMEOUT => 30,
320
+
321
+ CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
322
+
323
+ CURLOPT_CUSTOMREQUEST => "POST",
324
+
325
+ CURLOPT_POSTFIELDS => $formParams,
326
+
327
+ CURLOPT_HTTPHEADER => array(
328
+
329
+ "accept: application/json",
330
+
331
+ "content-type: application/json"
332
+
333
+ ),
334
+
335
+ ));
336
+
337
+
338
+
339
+ $response = curl_exec($curl);
340
+
341
+ $err = curl_error($curl);
342
+
343
+ curl_close($curl);
344
+
345
+
346
+
347
+ if ($err) {
348
+
349
+ Log::info($err);
350
+
351
+ } else {
352
+
353
+ Log::info($response);
354
+
355
+ }
356
+
357
+ } catch (\GuzzleHttp\Exception\BadResponseException $e) {
358
+
359
+ Log::info($e);
360
+
361
+ return $e->getResponse()->getBody()->getContents();
362
+
363
+ }
364
+
365
+ }
366
+
367
+ }
368
+
369
+ ```
370
+
371
+
372
+
373
+
374
+
217
375
  ## 質問事項
218
376
 
219
377
  なぜこのようなエラーが発生するのでしょうか?

2

修正

2021/07/21 23:37

投稿

RyoS_
RyoS_

スコア4

test CHANGED
File without changes
test CHANGED
@@ -222,4 +222,6 @@
222
222
 
223
223
 
224
224
 
225
+
226
+
225
227
  よろしくお願いいたします。

1

タイトルを変更

2021/07/21 10:53

投稿

RyoS_
RyoS_

スコア4

test CHANGED
@@ -1 +1 @@
1
- HubSpotのAPIを叩いたら「Invalid input JSON」のエラーが発生する
1
+ 【Laravel/PHP】HubSpotのAPIを叩いたら「Invalid input JSON」のエラーが発生する
test CHANGED
File without changes