Mailchimp API Integration in PHP, FNAME and LNAME not passing - php

I've been trying to integrate the Mailchimp API in PHP on our website, and I cannot seem to get mailchimp to take the FNAME and the LNAME.
The email always gets passed through to mailchimp and they are added to the list but the names simply don't and are always blank.
Things I have tried:
Using static names such as Dave in place of $_POST[FirstNAME] etc. but still no luck
Using MERGE1 and MERGE2 which are the alternate names for FNAME and LNAME.
Sending the email along with the FNAME and LNAME in the array for merge_vars
Putting the array in directly or as it is below within a variable ($Merge).
require('../MailCHIMP_API_PHP/Mailchimp.php');
$Mailchimp = new Mailchimp( $api_key );
$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
$Merge = array('FNAME' => $_POST[FirstNAME], 'LNAME' => $_POST[LastNAME]);
$subscriber = $Mailchimp_Lists->subscribe( $list_id, array(
'email' => htmlentities($_POST[Email]),
'merge_vars' => $Merge
));
if ( ! empty( $subscriber['leid'] ) ) {
//echo "success";
} else {
//echo "fail";
}
As always there is probably something simple I am missing but I have been staring at this code for so long I obviously can't see it!

This issue was solved so thought I might as well post an answer. Here is the code that works.
require('../MailCHIMP_API_PHP/Mailchimp.php');
$Mailchimp = new Mailchimp( $api_key );
$subscriber = $Mailchimp->call('lists/subscribe',
array(
'id' => $list_id,
'email' => array('email' => htmlentities($_POST[Email])),
'merge_vars' => array('FNAME' => htmlentities($_POST[FirstNAME]), 'LNAME' => htmlentities($_POST[LastNAME])),
'double_optin' => false
));
if ( ! empty( $subscriber['leid'] ) ) {
//echo "success";
} else {
//echo "fail";
}

Related

Mailchimp Error: Bad Request - Your Campaign is not ready to send

I am using following tutorial to create campaign and send email in MailChimp using Php.
https://isabelcastillo.com/create-send-mailchimp-campaign-api-3
My Code piece are
require_once('../wp-load.php');
function isa_mailchimp_api_request( $endpoint, $type = 'POST', $body = '' )
{
// Configure --------------------------------------
$api_key = 'API KEY HERE'; // Changed API Key here
// STOP Configuring -------------------------------
$core_api_endpoint = 'https://<dc>.api.mailchimp.com/3.0/';
list(, $datacenter) = explode( '-', $api_key );
$core_api_endpoint = str_replace( '<dc>', $datacenter, $core_api_endpoint );
$url = $core_api_endpoint . $endpoint;
//print_r($url );
$request_args = array(
'method' => $type,
'timeout' => 20,
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'apikey ' . $api_key
)
);
if ( $body ) {
$request_args['body'] = json_encode( $body );
}
$request = wp_remote_post( $url, $request_args );
$response = is_wp_error( $request ) ? false : json_decode( wp_remote_retrieve_body( $request ) );
echo '<pre>';
print_r($response);
return $response;
}
function isa_create_mailchimp_campaign( $list_id, $subject ) {
$reply_to = 'info#newslume.com';
$from_name = 'NewsLume';
$subject= 'Another new test message 14 17';
$campaign_id = '';
$body = array(
'recipients' => array('list_id' => $list_id),
'type' => 'regular',
'settings' => array('subject_line' => $subject,
'title' => 'a test title NewsLUme',
'reply_to' => $reply_to,
'from_name' => $from_name,
'use_conversation'=> false,
'to_name'=> 'sajid',
'auto_footer'=> false,
'inline_css'=> false,
'auto_tweet'=> false,
'drag_and_drop'=> false
)
);
$create_campaign = isa_mailchimp_api_request( 'campaigns', 'POST', $body );
if ( $create_campaign ) {
if ( ! empty( $create_campaign->id ) && isset( $create_campaign->status ) && 'save' == $create_campaign->status ) {
// The campaign id:
$campaign_id = $create_campaign->id;
}
}
return $campaign_id ? $campaign_id : false;
}
function isa_set_mail_campaign_content( $campaign_id, $template_content ) {
$set_content = '';
$set_campaign_content = isa_mailchimp_api_request( "campaigns/$campaign_id/content", 'PUT', $template_content );
if ( $set_campaign_content ) {
if ( ! empty( $set_campaign_content->html ) ) {
$set_content = true;
}
}
return $set_content ? true : false;
}
$list_id='my_list_id_here'; // LIST HERE
$campaign_id = isa_create_mailchimp_campaign( $list_id, $subject );
if ( $campaign_id ) {
// Set the content for this campaign
$template_content = array(
'template' => array(
// The id of the template to use.
'id' => 47615, // INTEGER
'sections' => array(
'tst_content' => 'THIS IS THE CONTENT BODY OF MY EMAIL MESSAGE.'
)
)
);
$set_campaign_content = isa_set_mail_campaign_content( $campaign_id, $template_content );
if ( $set_campaign_content ) {
$send_campaign = isa_mailchimp_api_request( "campaigns/$campaign_id/actions/send", 'POST' );
if ( empty( $send_campaign ) ) {
// Campaign was sent!
} elseif( isset( $send_campaign->detail ) ) {
$error_detail = $send_campaign->detail;
}
}
}
I have updated all values, including API KEY, List ID, template ID etc. but still i am getting errors
Here is Error object
stdClass Object
(
[type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/
[title] => Bad Request
[status] => 400
[detail] => Your Campaign is not ready to send.
[instance] => 89dc8734-2611-4f3b-a4f7-d18bd181bded
)
I checked in Mail Chimp, campaigns are created there but they are saved as Draft.
Here are my API Logs
API Logs can be seen by clicking link below
https://drive.google.com/file/d/0BwIWuJmCDI1vNHgtVm9TQm1FMVU/view?usp=drivesdk
I am able to create campaign, set a template to campaign but i cannot send emails. My Domain is also verified and authenticated with Mailchimp using guidelines.
Please check and suggest a solution
While the "Your Campaign is not ready to send" message isn't very helpful, you can check for a more detailed message in MailChimp itself. Edit the draft that the API created, and navigate to the final Confirm step. You'll see a checklist where most of the items passed, but there will also be an item that explains why the campaign failed.
When I attempted to replicate the issue, the campaign failed to send because there was some default placeholder text left unchanged in the template. Since the code you posted only sets the content for one block, this is probably the same issue you're having.
Hope this helps!

Update CC info of Braintree in PHP

I am trying to update CC info of braintree but function provided in docs is not working . and I am unable to find out the reason.
Here is my function :
public function updateCC(){
$fname = $this->input->get_post('fname');
$lname = $this->input->get_post('lname');
$expirationYear = $this->input->get_post('expirationYear');
$expirationMonth = $this->input->get_post('expirationMonth');
$cardholderName = $this->input->get_post('cardholderName');
$cvv = $this->input->get_post('cvv');
$cc_no = $this->input->get_post('cc_no');
$token = $this->input->get_post('token');
$BTCustomerID = $this->input->get_post('BTCustomerID ');
$result = Braintree_Customer::update(
$BTCustomerID,
(
'firstName' => $fname,
'lastName' => $lname,
'creditCard' => (
'paymentMethodNonce' => 'fake-valid-nonce',
'options' => (
'updateExistingToken' => $token,
'verifyCard' => true
)
)
));
echo json_encode(array('error'=>-1));
}
Due to this code my application is crashing .
$result = Braintree_Customer::update(
$BTCustomerID,
(
'firstName' => $fname,
'lastName' => $lname,
'creditCard' => (
'paymentMethodNonce' => 'fake-valid-nonce',
'options' => (
'updateExistingToken' => $token,
'verifyCard' => true,
)
)
));
I’m a developer at Braintree. In your call to Braintree_Customer::update(), paymentMethodNonce must be passed inside of creditCard at the same level as options. In your code you are passing the it inside of options. See this code example for reference.

Mailchimp intergration error

We want to import all our database into one of your lists. We use php. The code is:
$api_key = "dXXXXXXX7eX276XXXXXXXX490";
$list_id = "7XXXb0XXXe";
$Mailchimp = new Mailchimp($api_key);
$Mailchimp_Lists = new Mailchimp_Lists($Mailchimp);
$batch = array();
$batch[] = array('email' => array(
'email' => 'xxxgmail.com',
"euid" => "xxx#gmail.com",
"leid" => "xxx#gmail.com"
),
'merge_vars' => array('FNAME' => 'XXX'));
$subscriber = $Mailchimp_Lists->batchSubscribe($list_id, $batch, false, true, true);
But I get an error
Mailchimp_User_DoesNotExist
Could not find user record with id
I tried many times, removed uuid and leid but no success. What is wrong? Thank you!
The API key used by me was incorrect.

How to edit Magento current logged user details via php code

$session = Mage::getSingleton('customer/session');
$customer_id = $session->getId();
$customer_data = Mage::getModel('customer/customer')->load($customer_id);
print_r($customer_data);
Through this code ican get user details
I need to know How to edit the user details like address,password ..etc in the same Way using Php codes
Thanks all
for Password you can set using $customer_id
$password = 'Any Things'
$customer = Mage::getModel('customer/customer')->load($customer_id);
$customer->setPassword($password);
$customer->save();
For Edit address you have to load address model
For Example if you want to edit billing Address :
$customer = Mage::getModel('customer/customer')->load($customer_id);
$address = $customer->getDefaultBilling();
$address->setFirstname("Test");
$address->save();
OR :
using address id get from customer object :
$address = Mage::getModel('customer/address')->load($customerAddressId);
$address->setFirstname("Test");
$address->save();
You can use php magic get and set methods
Suppose to set password you can use $customer_data->setPassword('1234567');
$customer_data->save();
For customer address
$_custom_address = array (
'firstname' => 'firstname',
'lastname' => 'lastname',
'street' => array (
'0' => 'Sample address part1',
'1' => 'Sample address part2',
),
'city' => 'city',
'region_id' => '',
'region' => '',
'postcode' => '31000',
'country_id' => 'US',
'telephone' => '0038531555444',
);
$customAddress = Mage::getModel('customer/address')
$customAddress->setData($_custom_address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
try {
$customAddress->save();
}
catch (Exception $ex) {
//Zend_Debug::dump($ex->getMessage());
}
For more info http://inchoo.net/ecommerce/magento/programming-magento/programatically-create-customer-and-order-in-magento-with-full-blown-one-page-checkout-process-under-the-hood/
You can use methods of Mage_Customer_Model_Customer class:
$customerSession = Mage::getSingleton('customer/session');
$customerModel = Mage::getModel('customer/customer')->load($customerSession->getId());
$customerModel->changePassword('new_password');

NuSoap equivalent of SoapVar()

I don't know much about NuSoap, but unfortunately I have to use it.
I'm trying to follow example code for the dotmailer web service api Here. I need to be able to add custom fields for subscribers.
In the example code custom/additional fields are defined like so;
new SoapVar($FirstName,XSD_STRING,"string","http://www.w3.org/2001/XMLSchema");
Heres the code I have right now;
<?php
function subscribeUserEmail($email)
{
$username = "********";
$password = "********";
$postURL = "http://apiconnector.com/api.asmx?WSDL";
$contact = array (
"Email" => $email,
"AudienceType" => "B2C",
"OptInType" => "Single",
"EmailType" => "Html",
"ID" => -1,
"DataFields" => array(
"Keys" => array("TEST"),
"Values" => array("Name")
)
);
$params = array(
"username" => $username,
"password" => $password,
"contact" => $contact,
"addressbookId" => "******"
);
$client = new soapclient($postURL, true);
$error = $client->getError();
$result = $client->call('AddContactToAddressBook', $params);
echo "<h2>Request</h2>";
print("<pre>".$client->request."</pre>");
echo "<h2>Response</h2>";
print("<pre>".$client->response."</pre>");
echo "<h2>Debug</h2>";
print("<pre>".$client->debug_str."</pre>");
}
?>
If the $contact array is changed to this;
$contact = array (
"Email" => $email,
"AudienceType" => "B2C",
"OptInType" => "Single",
"EmailType" => "Html",
"ID" => -1,
"DataFields" => array(
"Keys" => array("TEST"),
"Values" => array(new SoapVar("Name",XSD_STRING,"string","http://www.w3.org/2001/XMLSchema") )
)
);
And used with a regular soap client, the code works. So I'm pretty sure there aren't any other problems with my function.
I've tried using the following;
new soapval("string", XSD_STRING, "Name","http://www.w3.org/2001/XMLSchema");
As an alternative to the SoapVar() method, but I get the same errors as if I entered the value as plain text.
How can I replicate the functionality of SoapVar() in NuSoap? This appears to be the only issue.

Categories