首页 > 解决方案 > 使用 Curl 将参数传递给请求正文中的 API

问题描述

我将表单中的数据发布到 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);

标签: phpapicurl

解决方案


我找到了答案,这是有效的代码。

<?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;
?>

推荐阅读