Передача параметров в API в теле запроса с помощью Curl - PullRequest
1 голос
/ 06 мая 2020

Я отправляю данные из формы в 2 различных APIS. Первый работает хорошо, а этот нет.

Это то, что я отправляю, есть соединение с API, но параметры не поступают, служба поддержки говорит, что я должен отправить данные в тело запроса, но я не знаю, как это сделать.

$chThree= curl_init();
// values API 3
$dataApiThree = array(
    'cid'                       => '199',
    'uid'                       => $_POST['uid'],
    'f_3_firstname'             => $_POST['firstname'],
    'f_4_lastname'              => $_POST['lastname'],
    'f_1_email'                 => $_POST['email'],
    'f_11_postcode'             => $_POST['meta_Zip'],
    'f_12_phone1'               => $_POST['mobile_phone'],
    'f_135_nombre_empresa'      => $_POST['meta_empresa'],
    'f_134_cantidad_vehiculos'  => $_POST['meta_cantidadVehiculos'],
    'f_133_tipo_servicio    '   => $_POST['meta_Gestion']
  );

  $optsThree = array(
      CURLOPT_URL            => 'https://leadtowin.databowl.com/api/v1/lead',
      CURLOPT_HTTPHEADER     => 'Content-Type: application/x-www-form-urlencoded',
      CURLOPT_POST           => true,
      CURLOPT_POSTFIELDS     => $dataApiThree,
      CURLOPT_RETURNTRANSFER => false,
      CURLOPT_REFERER        => $_SERVER['HTTP_REFERER']
  );
  //end API 3

//curl API3
curl_setopt_array($chThree, $optsThree);
$responseThree = curl_exec($chThree);
$responseThree = json_decode($responseThree);
curl_close($chThree);

1 Ответ

0 голосов
/ 13 мая 2020

Я нашел ответ, это был код, который работает.

<?php
$post_array = array(
    "cid" => "199",
    "sid" => "2",
    "uid" => $_POST['uid'],
    "optin_email" => $_POST['optin_email'],
    "optin_email_timestamp" => $_POST['optin_email_timestamp'],
    "optin_phone" => $_POST['optin_phone'],
    "optin_phone_timestamp" => $_POST['optin_phone_timestamp'],
    "optin_sms" => $_POST['optin_sms'],
    "optin_sms_timestamp" => $_POST['optin_sms_timestamp'],
    "optin_postal" => $_POST['optin_postal'],
    "optin_postal_timestamp" => $_POST['optin_postal_timestamp'],
    "f_1_email" => $_POST['f_1_email'],
    "f_3_firstname" => $_POST['f_3_firstname'],
    "f_4_lastname" => $_POST['f_4_lastname'],
    "f_11_postcode" => $_POST['f_11_postcode'],
    "f_12_phone1" => $_POST['f_12_phone1'],
    "f_135_nombre_empresa" => $_POST['f_135_nombre_empresa'],
    "f_134_cantidad_vehiculos" => $_POST['f_134_cantidad_vehiculos'],
    "f_133_tipo_servicio" => $_POST['f_133_tipo_servicio']
);
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://leadtowin.databowl.com/api/v1/lead",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($post_array),
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/x-www-form-urlencoded",
    "Accept-Encoding: UTF-8",
    "Content-Type: application/x-www-form-urlencoded",
    "Cookie: PHPSESSID=d053646c7953d9116849a8aa4717ab81"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
?>
...