Есть кастом модуль с формой в drupal 8. Нужно сделать, чтобы при отправке формы создавался новый контакт на сайте hubspot.com. Читала документацию [url]https://developers.hubspot.com/docs/methods/contacts/create_or_update[/url]
Как я понимаю, нужно создать пост запрос на [url]https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/testingapis@hubspot.com/?hapikey=demo[/url], где подставить свой hapikey и почту заменить выражением, достающим емэйл введенный в форму.
Выдает ошибку:
GuzzleHttp\Exception\ClientException: Client error: POST https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/123@gmail.com/?hapikey=62c6e162-1f3e-40eb-aa07-d0a31a5aa131
resulted in a 400 Bad Request
response: {"status":"error","message":"Invalid JSON input: No content to map due to end-of-input at line 1, column 0","correlation (truncated...) in GuzzleHttp\Exception\RequestException::create() (line 113 of vendor\guzzlehttp\guzzle\src\Exception\RequestException.php).
Что не так с JSON данными? Может нужно передать заголовок с указанием content-type? Помогите, пожалуйтса...
my_form.info.yml
name: 'My Form'
description: 'Example create form in Drupal 8'
core: 8.x
type: module
my_form.routing.yml
my_form.example_form:
path: '/my-form'
defaults:
_form: '\Drupal\my_form\Form\MyForm'
_title: 'My form'
requirements:
_permission: 'access content'
MyForm.php
<?php
namespace Drupal\my_form\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class MyForm extends FormBase {
public $properties = [];
public function getFormId() {
return 'my_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['first_name'] = array(
'#type' => 'textfield',
'#title' => 'First name',
'#required' => TRUE
);
$form['last_name'] = array(
'#type' => 'textfield',
'#title' => 'Last name',
'#required' => TRUE
);
$form['subject'] = array(
'#type' => 'textfield',
'#title' => 'Subject',
'#required' => TRUE
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => 'Message',
'#required' => TRUE
);
$form['email'] = array(
'#type' => 'email',
'#title' => $this->t('Your e-mail address'),
'#required' => TRUE
);
$form['button'] = array(
'#type' => 'submit',
'#value' => 'Submit'
);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (strpos($form_state->getValue('email'), '.com') === FALSE) {
$form_state->setErrorByName('email', 'E-mail is incorrect!');
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$message = $form_state->getValue('message');
$message = wordwrap($message, 70, "\r\n");
$subject = $form_state->getValue('subject');
$res = mail('admin@mail.ru', $subject, $message);
if($res) {
\Drupal::logger('my_form')->notice('Mail is sent. E-mail: '.$form_state->getValue('email'));
drupal_set_message('E-mail is sent!');
}
$email = $form_state->getValue('email');
$url = "https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/".$email."/?hapikey=62c6e162-1f3e-40eb-aa07-d0a31a5aa131";
$json = '{
"properties": [
{
"property": "firstname",
"value": "HubSpot"
},
{
"property": "lastname",
"value": "Test"
},
{
"property": "website",
"value": "http://hubspot.com"
},
{
"property": "company",
"value": "HubSpot"
},
{
"property": "phone",
"value": "555-122-2323"
},
{
"property": "address",
"value": "25 First Street"
},
{
"property": "city",
"value": "Cambridge"
},
{
"property": "state",
"value": "MA"
},
{
"property": "zip",
"value": "02139"
}
]
}';
$request = \Drupal::httpClient()->post($url, NULL, $json);
try {
$response = $request->send();
}
catch (\Guzzle\Http\Exception\RequestException $exception) {
// ...
}
}
}