MailChimp group options select in php - php

I tried to find the answer for the last 5 hours but I finally caved in and am reaching out for help.
Basically, this code worked great until I needed to select a group option. I'm not sure if I'm creating my interests array properly, I may have to use 'merge_vars' but really would like some guidance before I spend another 5 hours blindly walking into walls.
Note for my group name I've been using the entire string "group[3117]"
Action.php
<?php
session_start();
if(isset($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$interest = $_POST['group[3117]'];
if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL) === false){
// MailChimp API credentials
$apiKey = '+ insert api key here +';
$listID = 'insert list id';
$interest = 'insert group name'; // YOUR INTEREST/GROUP ID
// MailChimp API URL
$memberID = md5(strtolower($email));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listID . '/members/' . $memberID;
// member information
$json = json_encode([
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => [
'FNAME' => $fname,
'LNAME' => $lname
],
'interests' => array(
$interest => true
),
]);
// send a HTTP POST request with curl
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// store the status message based on response code
if ($httpCode == 200) {
$_SESSION['msg'] = '<p style="color: #34A853">You have successfully subscribed to CodexWorld.</p>';
} else {
switch ($httpCode) {
case 214:
$msg = 'You are already subscribed.';
break;
default:
$msg = 'Some problem occurred, please try again.';
break;
}
$_SESSION['msg'] = '<p style="color: #EA4335">'.$msg.'</p>';
}
}else{
$_SESSION['msg'] = '<p style="color: #EA4335">Please enter valid email address.</p>';
}
}
// redirect to homepage
header('location:index.php');
Index.php
<?php session_start(); // place it on the top of the script ?>
<?php
$statusMsg = !empty($_SESSION['msg'])?$_SESSION['msg']:'';
unset($_SESSION['msg']);
echo $statusMsg;
?>
<form method="post" action="action.php">
<p><label>First Name: </label><input type="text" name="fname" /></p>
<p><label>Last Name: </label><input type="text" name="lname" /></p>
<p><label>Email: </label><input type="text" name="email" /></p>
<p><select name="group[3117]" class="REQ_CSS" id="mce-group[3117]">
<option value=""></option>
<option value="1">Los Angeles</option>
<option value="2">Seattle</option>
<option value="4">Portland</option> </p>
<p><input type="submit" name="submit" value="SUBSCRIBE"/></p>
</form>

Here's the solution, there's a lot of manual data entry but it works, if someone has an easier method please let me know.
Enter the MailChimp API playground. Select your list / subresources / interest-categories / interest name / subresources / interests / response . scroll down and there will be a unique "id" for interest. (ignore category id)
Here is the code, credit goes Mukesh Chapegan and CodexWorld
Name this page Action.php
<?php
$email = $_POST['email'];
$memberHash = md5($email);
$first_name = $_POST['fname'];
$last_name = $_POST['lname'];
$interest = $_POST['location'];
$api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // YOUR API KEY
$server = 'us14.'; //last part of ur api key
$list_id = 'XXXXXXX'; // YOUR LIST ID
$auth = base64_encode( 'user:'.$api_key );
$data = array(
'apikey' => $api_key,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $first_name,
'LNAME' => $last_name
),
'interests' => array(
$interest => true
),
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.$list_id.'/members/'.$memberHash);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
$result_obj = json_decode($result);
echo $result_obj->status;
echo '<br>';
echo '<pre>'; print_r($result_obj); echo '</pre>';
?>
Here is the HTML form. name this index.php
Note I named the select element to location and the option values to the interest id I obtained through the API playground.
<?php session_start(); // place it on the top of the script ?>
<?php
$statusMsg = !empty($_SESSION['msg'])?$_SESSION['msg']:'';
unset($_SESSION['msg']);
echo $statusMsg;
?>
<form method="post" action="action.php">
<p><label>First Name: </label><input type="text" name="fname" /></p>
<p><label>Last Name: </label><input type="text" name="lname" /></p>
<p><label>Email: </label><input type="text" name="email" /></p>
<p> <select name="location" class="REQ_CSS" id="mce-group[3117]">
<option value=""></option>
<option value="f3eb68268d">Los Angeles</option>
<option value="3204c60199">Seattle</option>
<option value="3c5b8d638a">Portland</option> </p>
</select>
<p><input type="submit" name="submit" value="SUBSCRIBE"/></p>
</form>
If this answer helps please upvote so it's easier to find. I looked for this solution for a very very long time and found a lot of questions but few answers.

Related

I want to convert a post method form to curl command

I'm making an auto SMS system. But SMS provider gave me post method form but I want it to modify with curl command by my own.
they provided me
<head>
<title>SMS</title>
</head>
<body>
<form name="sms" method="post" >
<input type="text" placeholder="send to number" name="to" /><br />
<input type="textarea" placeholder="sms" name="msg" /><br />
<input type="submit" value=" OK " />
</form>
<?php
$apikey='$2y$10$t..Yr.YDG0kXYiuLwQ78OecDgz6/qh.1xSWx77mXjczkk3AEKvTZe';
if (isset($_POST["msg"]) ) {
$sendto = $_POST["to"];
$fullNumber = '880' . substr(preg_replace('/\D/', '', $sendto), -10);
$msg = urlencode($_POST["msg"]);
// $masking='CITY PORTER';
// $masking=urlencode($masking);
// masking $url='http://example.com/smsapi/masking?api_key='.$apikey.'&smsType=text&maskingID='.$masking.'&mobileNo='.$fullNumber.'&smsContent='.$msg.'';
$url='http://example.com/smsapi/non-masking?api_key='.$apikey.'&smsType=text&mobileNo='.$fullNumber.'&smsContent='.$msg.'';
if ( !empty($_POST["to"]) && !empty($_POST["msg"])) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL =>$url,
CURLOPT_USERAGENT =>'My Browser'
));
$resp = curl_exec($curl);
echo $resp;
curl_close($curl);
}
else
{ echo "Field is empty";}
}
?>
</body>
I converted it to curl command but this is not still working. I just want to input multiple value without is set() 1 value.
$api_key = "$2y$10$mtW.yfKj18i2mTPe/0iCEuKdCfCGh9zOYYEU9AmnMrJyBb.h7fVcG";
$number = $row[mailing_no];
$message = "Dear Guardian, ". $row[name] . " has swiped his card right now";
$type= "text";
$params = array('api_key'=>$api_key, 'smsType'=>$type, 'mobileNo'=>$number, 'smsContent'=>$message);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/smsapi/non-masking?".http_build_query($params, "", "&"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close ($ch);
Try this i hope your problem will be solve
<div id="send_reply"></div>
<form name="sms" method="post" >
<input type="text" placeholder="send to number" name="to" id="phone" /><br />
<input type="textarea" placeholder="sms" name="msg" id="mess" /><br />
<input type="submit" value=" OK " id="send_btn" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#send_btn").click(function(){
var txt1=$("#phone").val();
var txt2=$("#mess").val();
$.ajax({
url : 'check.php',
method : 'GET',
data : {txt1:txt1,txt2:txt2},
success:function(dda){
$("#send_reply").html(dda);
}
});
});
});
</script>
I think we were missing double quotes for $row keys, try this:
$api_key = "$2y$10$mtW.yfKj18i2mTPe/0iCEuKdCfCGh9zOYYEU9AmnMrJyBb.h7fVcG";
$number = $row["mailing_no"];
$message = "Dear Guardian, ". $row["name"] . " has swiped his card right now";
$type= "text";
$params = array('api_key'=>$api_key, 'smsType'=>$type, 'mobileNo'=>$number, 'smsContent'=>$message);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/smsapi/non-masking?".http_build_query($params, "", "&"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close ($ch);

How to send firebase push notification using trigger

I am trying to send notification whenever the database is inserted with new value but i keep getting "error in sending request" repeatedly while trying this code,not able to find what's the problem, I have used server api key as suggested. I have used one form that takes two textboxs which will be directed to the other file above.
<?php
$host='localhost';
$username='';
$pwd="";
$db="";
$con=mysqli_connect($host,$username,$pwd,$db);
if($_SERVER['REQUEST_METHOD']=='POST'){
$full_name = $_POST['full_name'];
$contact_number = $_POST['contact_number'];
//require_once('dbConnect.php');
$sql = "INSERT INTO notification (full_name,contact_number) VALUES (
'$full_name'
'$contact_number')";
$check = "SELECT * from notification where full_name='$full_name' AND contact_number='$contact_number'";
$checkData = mysqli_query($con,$check);
if (mysqli_num_rows($checkData) > 0) {
echo "Request already posted";
}else{
if(mysqli_query($con,$sql)){
$notiTitle = "notification request";
$notiMessage ="by".$full_name;
sendNotification($notiTitle, $notiMessage);
echo "sucessfully added";
}else{
echo "error in sending request";
}
}
}else{
echo 'error';
}
function sendNotification($title, $msg) {
$titlee = $title;
$message = $msg;
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
$server_key = "AIz#################################";
$sql = "SELECT app_id FROM user_app_id";
$result = mysqli_query($con,$sql);
// fetch all key of devices
$finalKey=array();
while($row= mysqli_fetch_array($result)){
$finalKey[]=$row['app_id'];
}
$headers = array(
'Authorization:key=' .$server_key,
'Content-Type : application/json');
$fields = array('registration_ids'=>$finalKey, 'notification'=>array('title'=>$title, 'body'=>$message));
$payload = json_encode($fields);
$curl_session = curl_init();
curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session, CURLOPT_POST, true);
curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($curl_session);
curl_close($curl_session);
echo $result;
mysqli_close($con);
}
<html>
<body>
<form action="init.php" method="post">
Full Name: <input type="text" name="full_name"><br>
Contact No: <input type="text" name="contact_number"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
?>
Can anyone help me on this?
Not sure if it was a typo in your code, but you need to adjust the PHP code - you've wrapped the HTML within the <?php ?> segment.
Try exiting the PHP segment before the HTML starts like below;
?>
<html>
<body>
<form action="init.php" method="post">
Full Name: <input type="text" name="full_name"><br>
Contact No: <input type="text" name="contact_number"><br>
<input type="submit" value="submit">
</form>
</body>
</html>

I am trying to make a custom User creation with ZenDesk(CRM)

This site helped me out a lot last time I had an issue. I was wondering why this php code was not working. Any help would be appreciated. I am trying to make a custom user creation request with ZenDesk.
I got the request to work, but when I edited the code to fit it into the actual Instance of my CRM it does not even try to make a PHP request. I verified this should be possible if the code is correct. I Just can not figure out why it is not correct.
<?php
include("config.php");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://url/api/v2/users.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"user\": {\"name\": \"$name\",
\"email\": \"$email\", \"phone\": \"$phone"}}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "email" . ":" . "password");
$phone = phone;
$email = email;
$name = name;
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
if ( !isset ($_POST['Name']) ||
!isset ($_POST['Name'] ||
!isset ($_POST['phone']
)
{
}
curl_close ($ch);
?>
<!DOCTYPE html>
<html>
<body>
<form
action="://url/agent/users/{{user.id}}/requested_tickets">
First name:<br>
<input type="text" name="name" value="name">
<br>
Last name:<br>
<input type="text" name="email" value="email">
<br><br>
<input type="text" name="phone" value="+15555555555">
<br><br>
<input type="submit" value="Submit">
</form>
<p>If you click the "Submit" button, the form-data will be sent to a
page
called "/action_page.php".</p>
</body>
</html>

Linking Mailchimp to Sign-Up Boxes

I am linking an e-mail sign-up and a post code (UK English for zip code) input box (see html code below) to an existing Mailchimp e-mail list. I have taken out the $apiKey, $listId and $submit_url identifiers from the code below. I have been able to successfully link the Mailchimp list to the e-mail input (so that e-mail address are populated when they are input in the Mailchimp list but I cannot figure out how to get post codes to automatically update in mailchimp list when they are input).
I am relatively new to php and would be so grateful for any help.
<div class="subscribe">
<form method="post" action="bokacsubscribe.php" role="form">
<input type="email" placeholder="Enter your e-mail address..." class="subscribe-input" name="email" style="margin-bottom: 10px;">
<form method="post" action="bokacsubscribe.php" role="form">
<input type="email" placeholder="Enter your postcode" class="subscribe-input" name="postcode" style="margin-bottom: 10px;">
<button type="submit" class="btn btn-subscribe success" style="border-radius:120px;background-color:rgb(241,174,200);border:1px solid rgb(241,174,200)"><b>Send</b></button>
<input type="hidden" name="referr" id="referr"/>
</form>
</div>
</div>
<?php
// Credits: https://gist.github.com/mfkp/1488819
session_cache_limiter('nocache');
$apiKey = ''; - // How get your Mailchimp API KEY - http://kb.mailchimp.com/article/where-can-i-find-my-api-key
$listId = ''; - // How to get your Mailchimp LIST ID - http://kb.mailchimp.com/article/how-can-i-find-my-list-id
$submit_url = ""; - // Replace us2 with your actual datacenter
$double_optin = false;
$send_welcome = false;
$email_type = 'html';
$email = $_POST['email'];
$merge_vars = array( 'YNAME' => $_POST['yname'] );
$data = array(
'email_address' => $email,
'apikey' => $apiKey,
'id' => $listId,
'double_optin' => $double_optin,
'send_welcome' => $send_welcome,
'merge_vars' => $merge_vars,
'email_type' => $email_type
);
$payload = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $submit_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($payload));
$result = curl_exec($ch);
curl_close ($ch);
$data = json_decode($result);
if ($data->error) {
// do whatever you do on failure here
} else {
// do whatever you do on success here
$arrResult = array ('response'=>'success');
}

How to put POST form data in action

I am trying to setup a user creation API for VestaCP. I have these two pages.
The form:
<form action="process.php" method="post">
Username:<br>
<input type="text" name="username">
<br>
Password:<br>
<input type="password" name="password">
<br>
Email Address:<br>
<<input type="email" name="email">
<br><br>
<input type="submit" value="Submit">
</form>
Then the action:
<?php
// Server credentials
$vst_hostname = 'MYSERVERADDRESS';
$vst_username = 'MYSERVERUSERNAME';
$vst_password = 'MYSERVERPASSWORD';
$vst_command = 'v-add-user';
// New Account
$username = 'username';
$password = 'password';
$email = 'email';
$package = 'Free';
$fist_name = 'Null';
$last_name = 'Null';
// Prepare POST query
$postvars = array(
'user' => $vst_username,
'password' => $vst_password,
'returncode' => $vst_returncode,
'cmd' => $vst_command,
'arg1' => $username,
'arg2' => $password,
'arg3' => $email,
'arg4' => $package,
'arg5' => $fist_name,
'arg6' => $last_name
);
$postdata = http_build_query($postvars);
// Send POST query via cURL
$postdata = http_build_query($postvars);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://' . $vst_hostname . ':8083/api/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$answer = curl_exec($curl);
// Check result
if($answer == 0) {
echo "User account has been successfuly created\n";
} else {
echo "Query returned error code: " .$answer. "\n";
}
?>
I think the problem is inputting the form data into the php script but I'm not very experienced so I don't know.
The API is connecting to my server correctly as I tested the same
//Server Credentials
part with a different API and it worked.
When I run this script, it outputs "User account has been successfully created" but when I check my control panel, there is no new user.
Thank you for any help.
EDIT: I only wrote the form, this script was made by VestaCP as shown here: https://vestacp.com/docs/api/. The errors in this script are direct from Vesta.
Try this:
<form action="process.php" method="post">
Username:<br>
<input type="text" name="username">
<br>
Firstname:<br>
<input type="text" name="firstname">
<br>
Name:<br>
<input type="text" name="name">
<br>
Password:<br>
<input type="password" name="password">
<br>
Email Address:<br>
<input type="email" name="email">
<br><br>
<input type="submit" value="Submit">
</form>
process.php
<?php
// Server credentials
$vst_hostname = 'MYSERVERADDRESS';
$vst_username = 'admin';
$vst_password = 'ADMINPASSWORD';
$vst_returncode = 'yes';
$vst_command = 'v-add-user';
// New Account
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$package = 'default'; // $package = 'Free'; //Free package exist on server?
$fist_name = $_POST['firstname'];
$last_name = $_POST['name'];
// Prepare POST query
$postvars = array(
'user' => $vst_username,
'password' => $vst_password,
'returncode' => $vst_returncode,
'cmd' => $vst_command,
'arg1' => $username,
'arg2' => $password,
'arg3' => $email,
'arg4' => $package,
'arg5' => $fist_name,
'arg6' => $last_name
);
$postdata = http_build_query($postvars);
// Send POST query via cURL
$postdata = http_build_query($postvars);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://' . $vst_hostname . ':8083/api/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$answer = curl_exec($curl);
// Check result
if($answer == 0) {
echo "User account has been successfuly created\n";
} else {
echo "Query returned error code: " .$answer. "\n";
}
?>

Categories