Mailchimp API : Batch Delete subscribers - mailchimp-api-v3.0

Is there any reference to the PHP warpper that I can use to perform batch delete subscribers. we have around 100k+ spam subscribers in Mailchimp list that we need to delete using batch delete.
Thanks

There is no official PHP wrapper for API v3, but you can use third-party wrappers such as this one from DrewM. He has provided good documentation on how to use it.
Here is an example of how you can create a batch operation to delete (not unsubscribe) each spam address in the $spamAddresses array. Of course, you'd have to populate the array first.
<?php
include('mailchimp-api-master/src/MailChimp.php');
include('mailchimp-api-master/src/Batch.php');
use \DrewM\MailChimp\MailChimp;
use \DrewM\MailChimp\Batch;
$apiKey = '********************************';
$listId = '**********';
$spamAddresses = [];
$MailChimp = new MailChimp($apiKey);
$Batch = $MailChimp->new_batch();
//Loop through array of spam addresses.
for($i = 0; $i < sizeof($spamAddresses); $i++){
$subscriberHash = $MailChimp->subscriberHash($spamAddresses[$i]);
$Batch->delete("op$i", "lists/$listId/members/$subscriberHash");
}
//Execute batch operation.
$result = $Batch->execute();
echo $result['id'];
?>
Make sure to grab the batch ID that's stored in $result['id'] if you want to check up on the status of the batch operation later, as DrewM's example shows in his documentation:
$MailChimp->new_batch($batch_id);
$result = $Batch->check_status();

Related

Retrieve visitor count from redis 'incrby' and create Popular Post

I have a news website and i want to create popular post based on user visit with interval between 7 Days.
I created counting mechanism using Redis::incrby() with prefix :
Redis::incrby( 'news:popular:count:' . $news_id . ':' . \Carbon\Carbon::now()->format('d-m-Y'), 1 );
but the problem is, i don't know how to retrieve it using GET and sort the value.
So i expect to get the popular post based on user count.
Is there any way to achieve it and get the popular post? or is there any better method to do this?
I've found my own solution and i will publish it here so if another user facing similar issue like me, you can use this solution.
First, we retrieve the key using Redis::keys using wildcard to get a list of a keys that contain the post count :
$popular = Redis::keys('news:popular:count:*');
and then we create empty array to store the count data :
$get_count = [];
after that we use foreach to explode and get the news_id and then get the post count
foreach ($popular as $key => $res) {
// Get news_id
$news_id = explode( ":", $res );
// Get count each post
$get_count[ $news_id[3] ] = Redis::get( $res );
}
and then we sort the array result from high to low using arsort() method
// Sort news count from high to low
arsort( $get_count );
That's it, the hardest part that i'm facing already solved. Maybe you can share a better answer if you have.

How to import data from a file into Redis database?

I am a very NOOB in Redis and this is the first time that I am using this application.I hope you can help me.
I am trying to create a list of words from a dictionary and put in into Redis database. I have a text file containing 200,000 words. How can I put it in my database?
I am using Laravel and My Redis configuration is working fine, because I am able to execute this command.
$redis = Redis::connection();
Redis::set('name', 'MYname');
$name = Redis::get('name');
echo $name;
Thanks in advance!
What is the dictionary used for?
If it is just 200,000 words, you can simply put all of them into a set.
for word in text_file:
Redis::sadd('dictionary_name', word)
and use
Redis::sismember('dictionary_name', word)
to check whethor or not the word is in the dictionary.
Here's documentation about redis set
Finally, I am able to upload my data through this command.
$filename = 'file.txt';
$fp = #fopen($filename, 'r');
if ($fp) {
$array = explode("\n", fread($fp, filesize($filename)));
}
$redis = Redis::connection();
Redis::sadd('dictionary', $array);

Using X-Editable - Backend part

So I'm just trying to use xeditable (http://vitalets.github.io/x-editable/docs.html#gettingstarted) to make changes to my database via AJAX.
Since I'm new to this concept and I'm (forcefully) working with PHP for the first time, I need some help.
I setup the frontend part, and a script called (say) script.php is handling the data for me (I need to write the new value in my database).
I can't really understand what to do in the script. Can someone guide me towards it? The docs above don't really do it for me.
Looking in a project I worked on a few months back (sorry about the mysql_ stuff – not my choice!)
Something like:
<?
include your/database/connection_stuff.php;
// Can't remember if x-editable passes the table in as well or not
$table = mysql_real_escape_string($_GET['table']);
// If not,
$table = 'name_of_table';
$value = mysql_real_escape_string($_POST['value']);
$name = mysql_real_escape_string($_POST['name']);
$pk = mysql_real_escape_string($_POST['pk']);
$result = mysql_query("UPDATE `$table` SET `$name` = '$value' WHERE id = '$pk'");
?>
Will do the trick.

Getting the last insert of your table

I have a table RECEIPT and the ID that i created is
REC-201290001 = "REC-"+"YEAR"+"MONTH"+"0001"
I create the number with a String.Format but in order to create the next one i need the last ID i inserted so i can increase it.
Thanks very much for your help.
$query = mysql_query("select value from config where name = 'next_receipt_id'");
$row = mysql_fetch_assoc($query);
$receiptId = "REC-"+"YEAR"+"MONTH"+$row['value'];
$insert_query = mysql_query("INSERT into order (`id`,...) values($receiptId,...)");
$updateReceiptId = mysql_query("update config set value = value+1 where name = 'next_receipt_id');
You need some kind of persistent storage for a counter. You generate fresh IDs using that counter as a seed.
You can use a simple file, or, more elegant, a database for the purpose of the persistent storage. There are hundreds of tutorials in the internet about this.

How to get a list of last viewed products in Magento?

I am trying to access the list of last viewed items using the below code:
$attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
$model = Mage::getModel('reports/product_index_viewed');
//
$_collection = $model->getCollection()->addAttributeToSelect($attributes)
->excludeProductIds($model->getExcludeProductIds())
->addUrlRewrite()
->setPageSize($columnCount)
->setCurPage(1);
//
$_collection->addPriceData();
$_collection->addIndexFilter();
$_collection->setAddedAtOrder();
//
Mage::getSingleton('catalog/product_visibility')->addVisibleInSiteFilterToCollection($_collection);
I copied this from the Mage_Reports_Block_Product_Abstract but this is giving the products in the creation order.
I doubt Prasanth will ever come back to this but I too having been trying to get a simple list of products without using a block which may not always be available. Eventually I found you need this:
$viewedCollection = Mage::getModel('reports/product_index_viewed')
->getCollection()
->addIndexFilter();
The secret is in addIndexFilter(), it uses the current customer or - if not logged in - the current visitor instead. From this you can loop through as with any other collection or extract a single array:
$viewedProductIds = $viewedCollection->getColumnValues('product_id');

Resources