Friday, November 6, 2020

Send html Form or Post data to Sendgrid contact list


How to Send html Form or Post data to Sendgrid contact list using PHP through API using JSON

################################## API ##############################
$curl = curl_init();

$list_ids="<<Your Contact List ID>>";

$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$email=$_POST['email'];

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.sendgrid.com/v3/marketing/contacts",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "PUT",
  CURLOPT_POSTFIELDS => "{\"list_ids\":[\"$list_ids\"],\"contacts\":[{\"address_line_1\":\"string (optional)\",\"address_line_2\":\"string (optional)\",\"city\":\"string (optional)\",\"country\":\"string (optional)\",\"email\":\"$email\",\"first_name\":\"$first_name\",\"last_name\":\"$last_name\",\"postal_code\":\"string (optional)\",\"state_province_region\":\"string (optional)\",\"custom_fields\":{}}]}",

  CURLOPT_HTTPHEADER => array(
    "authorization: Bearer SG.XXXXXXXXXXXXXX",
    "content-type: application/json"
  ),
));
$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  //echo $response;
    $_SESSION['message'] = "Thank You, we will get back to you in the next few days";
    
    if(isset($_POST['contactus'])){
        header('Location: contact.php');
    } else{
        eader('Location: sign-up.php');
    }
}

################################## API ##############################

No comments:

Post a Comment

How to inatall docker on Ubuntu using apt repository

 # Add Docker's official GPG key: sudo apt-get update sudo apt-get install ca-certificates curl sudo install -m 0755 -d /etc/apt/keyring...