<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>subscriber api &#8211; Chatbot Dojo</title>
	<atom:link href="https://v3.chatbotdojo.com/topic-tag/subscriber-api/feed/" rel="self" type="application/rss+xml" />
	<link>https://v3.chatbotdojo.com</link>
	<description></description>
	<lastBuildDate>Sat, 16 Jan 2021 12:10:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8.2</generator>

<image>
	<url>https://v3.chatbotdojo.com/wp-content/uploads/2020/07/favicon-56x56.png</url>
	<title>subscriber api &#8211; Chatbot Dojo</title>
	<link>https://v3.chatbotdojo.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to update a subscriber with /fb/subscriber/updateSubscriber</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-update-a-subscriber-with-fb-subscriber-updatesubscriber/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Thu, 14 Jan 2021 15:20:57 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25531</guid>

					<description><![CDATA[The curl command as displayed on the ManyChat API Swagger is: If you use cUrl, you need to replace the 0&#8217;s in the command with the appropriate data: ManyChat API key and enter all the new contact information that you want to edit. Let&#8217;s see how we can use this using PHP. What is a &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-update-a-subscriber-with-fb-subscriber-updatesubscriber/"> <span class="screen-reader-text">How to update a subscriber with /fb/subscriber/updateSubscriber</span> Read More &#187;</a></p>]]></description>
										<content:encoded><![CDATA[
<p>The curl command as displayed on the ManyChat API Swagger is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl -X POST "https://api.manychat.com/fb/subscriber/updateSubscriber" -H "accept: application/json" -H "Authorization: Bearer 0" -H "Content-Type: application/json" -d "{ \"subscriber_id\": 0, \"first_name\": \"string\", \"last_name\": \"string\", \"phone\": \"string\", \"email\": \"string\", \"gender\": \"string\", \"has_opt_in_sms\": true, \"has_opt_in_email\": true, \"consent_phrase\": \"string\"}"</pre>



<p>If you use cUrl, you need to replace the 0&#8217;s in the command with the appropriate data: ManyChat API key and enter all the new contact information that you want to edit.</p>



<p>Let&#8217;s see how we can use this using PHP. </p>



<h2>What is a good use case for this API?</h2>



<p>In the <a href="https://v3.chatbotdojo.com/topic/how-to-create-a-subscriber-with-fb-subscriber-createsubscriber/" class="rank-math-link">previous topic</a> we read about creating a user without going into Manychat or using a growth tool of any kind. This API allows you to also edit/update the contact information remotely. Very helpful if you use a platform or dashboard of your own, without having to go to Manychat each time when you need to make changes, or when your user/contact makes changes themselves in a profile page that you have in your platform.</p>



<p>In this topic we are going to change the name of the demo user, so we need a form and the userId. We will get that Id from our ManyChat contacts&#8217; dashboard in this course, but in a &#8220;real case&#8221; you will obviously have that information stored in your own system.</p>



<h3>The Form</h3>



<p>Let&#8217;s have a closer look at the fields we can enter, we will create a form field for each first_name and subscriber_id, where subscriber Id normally is NOT an editable form field, but a static, or hidden value (you don&#8217;t want to rename users by mistake:-)).</p>



<pre class="EnlighterJSRAW" data-enlighter-language="json" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "subscriber_id": 0,
  "first_name": "string",
  "last_name": "string",
  "phone": "string",
  "email": "string",
  "gender": "string",
  "has_opt_in_sms": true,
  "has_opt_in_email": true,
  "consent_phrase": "string"
}</pre>



<p><span class="has-inline-color has-vivid-red-color">Let&#8217;s copy the form from the previous topic and make a small change, adding the contact Id:</span></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="25-86" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;!DOCTYPE html>
&lt;html>
    &lt;head>
        &lt;title>Set Custom Field HTML Sample Page&lt;/title>
        &lt;meta charset="UTF-8">
        &lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
        &lt;!--Bootstrap CSS--> 
        &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    &lt;/head>
    &lt;body style="padding:50px;max-width:600px;">

        &lt;?php
        /*
         * Load the ManyChat Config File
         */
        require_once './config.inc.php';

        /*
         * Create Page and Form
         */
        ?>

        &lt;p style="margin:50px 0;">Update a Contact&lt;/p>

        &lt;form method="post" action="subscriber_updatesubscriber.php" id="updateSubscriber">
            
            &lt;div class="form-group">
                &lt;label for="subscriber_id">ManyChat User ID&lt;/label>                
                &lt;input type="text" class="form-control" value="" id="subscriber_id" name="subscriber_id" placeholder="Enter a valid userId in your Code First" required />
            &lt;/div>
            
            &lt;div class="form-group">
                &lt;label for="first_name">First name&lt;/label>                
                &lt;input type="text" class="form-control" value="" id="first_name" name="first_name" />
            &lt;/div>

            &lt;div class="form-group">
                &lt;label for="last_name">Last name&lt;/label>                
                &lt;input type="text" class="form-control" value="" id="last_name" name="last_name" />
            &lt;/div>

            &lt;div class="form-group">
                &lt;label for="phone">Phone&lt;/label>                
                &lt;input type="text" class="form-control" value="" id="phone" name="phone" />
            &lt;/div>
            &lt;div class="form-group">
                &lt;div>
                    &lt;input type="checkbox" id="has_opt_in_sms" name="has_opt_in_sms" value="TRUE">
                    &lt;label for="has_opt_in_sms">Opt-In SMS&lt;/label>&lt;/input>
                &lt;/div>           
            &lt;/div>            

            &lt;div class="form-group">
                &lt;label for="email">Email&lt;/label>                
                &lt;input type="email" class="form-control" value="" id="email" name="email" />
            &lt;/div>
            &lt;div class="form-group">
                &lt;div>
                    &lt;input type="checkbox" id="has_opt_in_email" name="has_opt_in_email" value="TRUE">
                    &lt;label for="has_opt_in_email">Opt-In Email&lt;/label>&lt;/input>
                &lt;/div>
            &lt;/div>            

            &lt;div class="form-group">
                &lt;label for="gender">Gender&lt;/label>  
                &lt;div>
                    &lt;input type="radio" id="male" name="gender" value="male">
                    &lt;label for="male">Male&lt;/label>&lt;/input>
                &lt;/div>
                &lt;div>
                    &lt;input type="radio" id="female" name="gender" value="female">
                    &lt;label for="female">Female&lt;/label>&lt;/input>
                &lt;/div>
                &lt;div>
                    &lt;input type="radio" id="other" name="gender" value="" checked>
                    &lt;label for="other">Other&lt;/label>&lt;/input>
                &lt;/div>
            &lt;/div>

            &lt;div class="form-group">
                &lt;label for="consent_phrase">Consent Phrase&lt;/label>                
                &lt;input type="text" class="form-control" value="" id="consent_phrase" name="consent_phrase" />
            &lt;/div>

            &lt;button type="submit" class="btn btn-primary">Update Subscriber&lt;/button>
        &lt;/form>

        &lt;!-- Optional JavaScript -->
        &lt;!-- jQuery first, Bootstrap JS -->
        &lt;script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">&lt;/script>
        &lt;script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">&lt;/script>
    &lt;/body>
&lt;/html>
</pre>



<h3>The Scripts</h3>



<p>This is the default response on a successful request, is, as always:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "status": "success"
}</pre>



<p>If we look at the contact information in the ManyChat contact pop-up, we see these contact details. Let&#8217;s change something, like the email address.</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="825" height="591" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-19.png" alt="" class="wp-image-25548" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-19.png 825w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-19-300x215.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-19-768x550.png 768w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-19-400x287.png 400w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-19-600x430.png 600w" sizes="(max-width: 825px) 100vw, 825px" /></figure>



<p>We can enter the information, like this:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="549" height="896" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-20.png" alt="" class="wp-image-25549" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-20.png 549w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-20-184x300.png 184w" sizes="(max-width: 549px) 100vw, 549px" /></figure>



<p>When we submit the form we get this result:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="184" height="41" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-17.png" alt="" class="wp-image-25546"/></figure>



<p>Looks like it worked, let&#8217;s double check in ManyChat&#8217;s contact window details!</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="838" height="578" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-21.png" alt="" class="wp-image-25550" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-21.png 838w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-21-300x207.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-21-768x530.png 768w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-21-400x276.png 400w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-21-600x414.png 600w" sizes="(max-width: 838px) 100vw, 838px" /></figure>



<p>The email address changed. So that worked out fine.</p>



<p>Let&#8217;s write down the scripts we used to make this work. First, the added method to the class.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="1199" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $contactDataFinal
     * @param type $mc_api
     * @return type
     */
    function subscriberUpdateSubscriber($contactDataFinal, $mc_api)
    {

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/subscriber/updateSubscriber");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $contactDataFinal . PHP_EOL);
        $headers       = [
            'Authorization: Bearer ' . preg_replace("/^(\w+\s+)/", "", $mc_api),
            'Content-Type: application/json',
            'Accept: application/json'
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $server_output = curl_exec($ch);
        curl_close($ch);

        $json_obj  = json_decode($server_output);
        $good_call = $json_obj->status;

        return $json_obj;
    }</pre>



<p>Now that we have the HTML and the Class model in a script, let&#8217;s show the final PHP file to process this API request.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="15-17,20" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Change Subscriber Data
 */

$clean_data_arr = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

// remove empty keys
$clean_data = array_filter($clean_data_arr, function($value) {
    return !is_null($value) &amp;&amp; $value !== '';
});

$contactData      = json_encode($clean_data, JSON_PRETTY_PRINT);
$contactDataFinal = preg_replace('/"true"/i', 'true', $contactData);

$subscriber_updated = $mc->subscriberUpdateSubscriber($contactDataFinal, $mc_api);

if ($subscriber_updated->status == "success") {
    echo "User has been added";
} else {
    $i = 0;
    foreach ($subscriber_updated->details->messages AS $key) {
        foreach ($key AS $message) {
            if (is_array($message)) {
                echo $message[$i] . "&lt;br \>";
                $i++;
            } else {
                echo $message . "&lt;br \>";
            }
        }
    }
}
</pre>



<p>Lines 15-17 will remove all the empty Array values so we only update what was entered in the form.</p>



<p>In line 20 we add an extra line of code to change <code>"true"</code> to <code>true</code>, otherwise the API won&#8217;t pick up on the boolean value IF changed here.</p>



<p>This wraps up ALL the available ManyChat API features that are available today. You are now capable of building ANYTHING you want. Restaurant portals, job application boards, cleaning service plans etcetera. </p>



<h2><strong>Congratulations</strong>!</h2>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to create a subscriber with /fb/subscriber/createSubscriber</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-create-a-subscriber-with-fb-subscriber-createsubscriber/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Thu, 14 Jan 2021 11:46:48 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25505</guid>

					<description><![CDATA[The curl command as displayed on the ManyChat API Swagger is: If you use cUrl, you need to replace the 0&#8217;s in the command with the appropriate data: ManyChat API key and enter all the new contact information that you want to add. Let&#8217;s see how we can use this using PHP. Why would you &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-create-a-subscriber-with-fb-subscriber-createsubscriber/"> <span class="screen-reader-text">How to create a subscriber with /fb/subscriber/createSubscriber</span> Read More &#187;</a></p>]]></description>
										<content:encoded><![CDATA[
<p>The curl command as displayed on the ManyChat API Swagger is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl -X POST "https://api.manychat.com/fb/subscriber/createSubscriber" -H "accept: application/json" -H "Authorization: Bearer 0" -H "Content-Type: application/json" -d "{ \"first_name\": \"string\", \"last_name\": \"string\", \"phone\": \"string\", \"email\": \"string\", \"gender\": \"string\", \"has_opt_in_sms\": true, \"has_opt_in_email\": true, \"consent_phrase\": \"string\"}"</pre>



<p>If you use cUrl, you need to replace the 0&#8217;s in the command with the appropriate data: ManyChat API key and enter all the new contact information that you want to add.</p>



<p>Let&#8217;s see how we can use this using PHP. </p>



<h2>Why would you need this?</h2>



<p>You don&#8217;t always want to add subscribers through your ManyChat bot. There are times, when you already have a list elsewhere and you just want to add contacts to your ManyChat contact list. This API is perfect for that and it is very easy to set up.</p>



<p>We will create an HTML form for this topic that we use to add a user. This is an option if you &#8211; form example &#8211; want to add users to the list when you do manual phone calls and add contacts as soon as they give you their info. In other cases, you may write a piece of code that loops through an existing list, liek a CSV file, and add them automatically.</p>



<p></p>



<h3>The Form</h3>



<p>Let&#8217;s have a closer look at the fields we can enter, we will create a form field for each entry.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="json" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "first_name": "string",
  "last_name": "string",
  "phone": "string",
  "email": "string",
  "gender": "string",
  "has_opt_in_sms": true,
  "has_opt_in_email": true,
  "consent_phrase": "string"
}</pre>



<p>Now we know what fields we can create, let&#8217;s make the simple form and wrap that in a bootstrap HTML page:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="26-91" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;!-- http://v3.chatbotdojo.com/course-files/subscriber_createsubscriber_form.php -->
&lt;!DOCTYPE html>
&lt;html>
    &lt;head>
        &lt;title>Set Custom Field HTML Sample Page&lt;/title>
        &lt;meta charset="UTF-8">
        &lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
        &lt;!--Bootstrap CSS--> 
        &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    &lt;/head>
    &lt;body style="padding:50px;max-width:600px;">

        &lt;?php
        /*
         * Load the ManyChat Config File
         */
        require_once './config.inc.php';

        /*
         * Create Page and Form
         */
        ?>

        &lt;p style="margin:50px 0;">Create a new Contact&lt;/p>

        &lt;form method="post" action="subscriber_createsubscriber.php" id="createSubscriber">
            &lt;div class="form-group">
                &lt;label for="first_name">First name&lt;/label>                
                &lt;input type="text" class="form-control" value="" id="first_name" name="first_name" />
            &lt;/div>

            &lt;div class="form-group">
                &lt;label for="last_name">Last name&lt;/label>                
                &lt;input type="text" class="form-control" value="" id="last_name" name="last_name" />
            &lt;/div>

            &lt;div class="form-group">
                &lt;label for="phone">Phone&lt;/label>                
                &lt;input type="text" class="form-control" value="" id="phone" name="phone" />
            &lt;/div>
            &lt;div class="form-group">
                &lt;label for="has_opt_in_sms">Opt-In SMS&lt;/label>                
                &lt;div>
                    &lt;input type="radio" id="has_opt_in_sms_yes" name="has_opt_in_sms" value="true">
                    &lt;label for="has_opt_in_sms_yes">Yes&lt;/label>&lt;/input>
                &lt;/div>
                &lt;div>
                    &lt;input type="radio" id="has_opt_in_sms_no" name="has_opt_in_sms" value="false" checked>
                    &lt;label for="has_opt_in_sms_no">No&lt;/label>&lt;/input>
                &lt;/div>             
            &lt;/div>            

            &lt;div class="form-group">
                &lt;label for="email">Email&lt;/label>                
                &lt;input type="email" class="form-control" value="" id="email" name="email" />
            &lt;/div>
            &lt;div class="form-group">
                &lt;label for="has_opt_in_email">Opt-In Email&lt;/label>                
                &lt;div>
                    &lt;input type="radio" id="has_opt_in_email_yes" name="has_opt_in_email" value="true">
                    &lt;label for="has_opt_in_email_yes">Yes&lt;/label>&lt;/input>
                &lt;/div>
                &lt;div>
                    &lt;input type="radio" id="has_opt_in_email_no" name="has_opt_in_email" value="false" checked>
                    &lt;label for="has_opt_in_email_no">No&lt;/label>&lt;/input>
                &lt;/div>
            &lt;/div>            

            &lt;div class="form-group">
                &lt;label for="gender">Gender&lt;/label>  
                &lt;div>
                    &lt;input type="radio" id="male" name="gender" value="male">
                    &lt;label for="male">Male&lt;/label>&lt;/input>
                &lt;/div>
                &lt;div>
                    &lt;input type="radio" id="female" name="gender" value="female">
                    &lt;label for="female">Female&lt;/label>&lt;/input>
                &lt;/div>
                &lt;div>
                    &lt;input type="radio" id="other" name="gender" value="" checked>
                    &lt;label for="other">Other&lt;/label>&lt;/input>
                &lt;/div>
            &lt;/div>

            &lt;div class="form-group">
                &lt;label for="consent_phrase">Consent Phrase&lt;/label>                
                &lt;input type="text" class="form-control" value="" id="consent_phrase" name="consent_phrase" />
            &lt;/div>

            &lt;button type="submit" class="btn btn-primary">Create Subscriber&lt;/button>
        &lt;/form>

        &lt;!-- Optional JavaScript -->
        &lt;!-- jQuery first, Bootstrap JS -->
        &lt;script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">&lt;/script>
        &lt;script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">&lt;/script>
    &lt;/body>
&lt;/html>
</pre>



<h3>The Scripts</h3>



<p>This is the default response on a successful request, is, as always:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "status": "success"
}</pre>



<p>Now we have a form in a web page, let&#8217;s have a look and enter some dummy data:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="588" height="826" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-10.png" alt="" class="wp-image-25515" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-10.png 588w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-10-214x300.png 214w" sizes="(max-width: 588px) 100vw, 588px" /></figure>



<p>When we submit the form and run a test (print the JSON result, instead of sending them to the Manychat API to see if the result look good), we get this result:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="335" height="159" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-9.png" alt="" class="wp-image-25514" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-9.png 335w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-9-300x142.png 300w" sizes="(max-width: 335px) 100vw, 335px" /></figure>



<p>Looks great! Let&#8217;s add the full scripts to this topic.</p>



<p>First, the added model to the class.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="1170" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $contactDataFinal
     * @param type $mc_api
     * @return boolean
     */
    function subscriberCreateSubscriber($contactDataFinal, $mc_api)
    {

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/subscriber/createSubscriber");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $contactDataFinal . PHP_EOL);
        $headers       = [
            'Authorization: Bearer ' . preg_replace("/^(\w+\s+)/", "", $mc_api),
            'Content-Type: application/json',
            'Accept: application/json'
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $server_output = curl_exec($ch);
        curl_close($ch);

        $json_obj  = json_decode($server_output);
        $good_call = $json_obj->status;
        
        return $json_obj;

    }</pre>



<p>Now that we have the HTML and the Class model in a script, let&#8217;s show the final PHP file for this API</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="15" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Add/Create a Subscriber 
 */

$clean_data = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

$contactData      = json_encode($clean_data, JSON_PRETTY_PRINT);
$contactDataFinal = preg_replace('/"true"/i', 'true', $contactData);
$subscriber_added = $mc->subscriberCreateSubscriber($contactDataFinal, $mc_api);

if ($subscriber_added->status == "success") {
    echo "User has been added";
} else {
    $i = 0;
    foreach ($subscriber_added->details->messages AS $key) {
        foreach ($key AS $message) {
            if (is_array($message)) {
                echo $message[$i] . "&lt;br \>";
                $i++;
            } else {
                echo $message . "&lt;br \>";
            }
        }
    }
}</pre>



<p>In line 15 we add an extra line of code to change <code>"true"</code> to <code>true</code>, otherwise the API won&#8217;t pick up on the boolean value.</p>



<p>Now let&#8217;s submit the same data without the PHP &#8216;check&#8217; and see if the user is added to our ManyChat account.</p>



<p>We actually got an error here: <code>has_opt_in_sms is required if phone is not empty</code></p>



<p>Let&#8217;s check that checkbox and try again.</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="562" height="258" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-11.png" alt="" class="wp-image-25518" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-11.png 562w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-11-300x138.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-11-400x184.png 400w" sizes="(max-width: 562px) 100vw, 562px" /></figure>



<p>Now the result shows this</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="164" height="50" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-12.png" alt="" class="wp-image-25519"/></figure>



<p>Let&#8217;s check this dummy user in ManyChat as well:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="745" height="119" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-13.png" alt="" class="wp-image-25520" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-13.png 745w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-13-300x48.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-13-400x64.png 400w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-13-600x96.png 600w" sizes="(max-width: 745px) 100vw, 745px" /></figure>



<p>Look at that! Isn&#8217;t that great?! Here&#8217;s the whole contact card:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="861" height="608" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-14.png" alt="" class="wp-image-25522" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-14.png 861w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-14-300x212.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-14-768x542.png 768w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-14-400x282.png 400w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-14-600x424.png 600w" sizes="(max-width: 861px) 100vw, 861px" /></figure>



<p>That&#8217;s it! Now you know how to add users to Manychat, without actually using ManyChat :-).</p>



<p>The next, and final API of this course is the <code>updateSubscriber </code>API, we&#8217;ll use the same dummy person to update the first name. </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to set a custom field by name with /fb/subscriber/setCustomFieldByName</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-set-a-custom-field-by-name-with-fb-subscriber-setcustomfieldbyname/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Wed, 13 Jan 2021 16:12:12 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25486</guid>

					<description><![CDATA[The curl command as displayed on the ManyChat API Swagger is: If you use cUrl, you need to replace the 0&#8217;s in the command with the appropriate data: ManyChat API key, subscriber ID, field_name (string) and field_value (&#8216;string&#8217;, 123, true, &#8216;2018-07-18&#8217;, &#8216;2018-07-02T00:00:00+00:00&#8217;). Why would you need this? Well, I don&#8217;t like to repeat myself 3 &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-set-a-custom-field-by-name-with-fb-subscriber-setcustomfieldbyname/"> <span class="screen-reader-text">How to set a custom field by name with /fb/subscriber/setCustomFieldByName</span> Read More &#187;</a></p>]]></description>
										<content:encoded><![CDATA[
<p>The curl command as displayed on the ManyChat API Swagger is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl -X POST "https://api.manychat.com/fb/subscriber/setCustomFieldByName" -H "accept: application/json" -H "Authorization: Bearer 0" -H "Content-Type: application/json" -d "{ \"subscriber_id\": 0, \"field_name\": \"string\", \"field_value\": \"'string', 123, true, '2018-07-18', '2018-07-02T00:00:00+00:00'\"}"</pre>



<p>If you use cUrl, you need to replace the 0&#8217;s in the command with the appropriate data: ManyChat API key, subscriber ID, field_name (string) and field_value (&#8216;string&#8217;, 123, true, &#8216;2018-07-18&#8217;, &#8216;2018-07-02T00:00:00+00:00&#8217;). </p>



<h2>Why would you need this?</h2>



<p>Well, I don&#8217;t like to repeat myself 3 times, and since this is part III in the &#8220;Set Custom Field&#8221;-series, I&#8217;ll go through this one rather quick. </p>



<p>I will however, emphasize that this method is not very reliable when it comes to setting/updating your custom fields. At first glance you&#8217;d think that using a custom field name is attractive because of the ease of use, since there&#8217;s no need to figure out what the ID of the field is. However, many programming languages do not accept FORM names with spaces or special characters and encode those names when submitting the form.</p>



<p>For example:<br><code>&lt;input type="text" name="<em>my special custom field</em>" value="my entered value"></code></p>



<p>If the form field is submitted the name <em>my special custom field</em> will get exncoded in the following format <em>my_special_custom_field</em>. As you can see this is now not the same anymore. You can write a fix for such cases in your code, or choose to do the extra work by getting the custom field ID first and use that in the forms. It will save you a lot of hassle, believe me:-)</p>



<p>So, now that you know, and understand why the &#8220;looks&#8221; of this being easier, is not what it seems in reality, I&#8217;ll wrap this chapter up with the scripts that show how to use the Name, instead of the Id.</p>



<h3>The Scripts</h3>



<p>This is the default response on a successful request, is, as always:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "status": "success"
}</pre>



<p>We are going to split this up in 2 parts again:<br>1. We create a webpage with a list of all custom fields and their current values, with an option to change it by adding a value in the form field. We don&#8217;t make a difference in string, number, boolean, date or datetime in this section, but you can if you want to in your system; <br>2. We create a PHP file to process the data.</p>



<p>But First, as always, let&#8217;s add a new function to our class file in classes/manychat.class.php.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="1086" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $subscriber_id
     * @param type $field_name
     * @param type $field_value
     * @param type $mc_api
     * @return boolean
     */
    function subscriberSetCustomFieldByName($subscriber_id, $field_name, $field_value, $mc_api)
    {

        $postfields_arr = [
            'subscriber_id' => $subscriber_id,
            'field_name'    => $field_name,
            'field_value'   => $field_value
        ];

        $postfields = json_encode($postfields_arr, JSON_NUMERIC_CHECK);

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/subscriber/setCustomFieldByName");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields . PHP_EOL);
        $headers       = [
            'Authorization: Bearer ' . preg_replace("/^(\w+\s+)/", "", $mc_api),
            'Content-Type: application/json',
            'Accept: application/json'
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $server_output = curl_exec($ch);
        curl_close($ch);

        $json_obj  = json_decode($server_output);
        $good_call = $json_obj->status;

        if ($good_call == "success") {
            return true;
        } else {
            return false;
        }
    }</pre>



<p>After we created the new function, we will create 2 new PHP files:</p>



<ol><li>subscriber_setcustomfieldbyname_form.php</li><li>subscriber_setcustomfieldbyname.php</li></ol>



<p>The First file is to get the collection for the specific user, each custom field with its own form text input field</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;!DOCTYPE html>
&lt;html>
    &lt;head>
        &lt;title>Set Custom Field HTML Sample Page&lt;/title>
        &lt;meta charset="UTF-8">
        &lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
        &lt;!--Bootstrap CSS--> 
        &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    &lt;/head>
    &lt;body style="padding:50px;max-width:600px;">

        &lt;?php
        /*
         * Load the ManyChat Config File
         */
        require_once './config.inc.php';

        /*
         * Create Page and Form
         */

        $subscriber_id   = "1929189923777499";
        $subscriber_info = $mc->getSubscriberInfo($subscriber_id, $mc_api);
        $subscriber_cufs = $subscriber_info->custom_fields;

        ?>

        &lt;p style="margin:50px 0;">Let's get all Custom Fields and assign a value to it manually. When you press the button, your custom field value for the subscriber will be updated.&lt;/p>

        &lt;form method="post" action="subscriber_setcustomfieldbyname.php" id="setCustomField">
            &lt;div class="form-group">
                &lt;label for="mc_uid">ManyChat User ID&lt;/label>                
                &lt;input type="text" class="form-control" value="&lt;?= $subscriber_id ?: "Enter a valid userId in your Code First" ?>" id="mc_uid" name="mc_uid" />
            &lt;/div>

            &lt;?php
            foreach ($subscriber_cufs AS $cuf) {
                ?>
                &lt;div class="form-group">
                    &lt;label for="mc_uid">&lt;strong>&lt;?= $cuf->name ?>&lt;/strong>&lt;/label>                
                    &lt;input type="text" class="form-control" value="" id="&lt;?= $cuf->name ?>" name="&lt;?= $cuf->name ?>" />
                    &lt;em>Current value: &lt;?= $cuf->value ?>&lt;/em>
                &lt;/div> 
            &lt;?php } ?>

            &lt;button type="submit" class="btn btn-primary">Set Custom Field Value&lt;/button>
        &lt;/form>

        &lt;!-- Optional JavaScript -->
        &lt;!-- jQuery first, Bootstrap JS -->
        &lt;script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">&lt;/script>
        &lt;script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">&lt;/script>
    &lt;/body>
&lt;/html>
</pre>



<p>If you run this page in your browser, you&#8217;ll see something similar to this again:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="539" height="898" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-4.png" alt="" class="wp-image-25475" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-4.png 539w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-4-180x300.png 180w" sizes="(max-width: 539px) 100vw, 539px" /></figure>



<p>It&#8217;s a large list with all custom fields set to your current user ID. In the &#8220;real&#8221; world, you&#8217;d of course get this user ID from your own system instead of hard-coding it in the source code.</p>



<p>To change a custom field, just enter the new value in the form field and submit it by using the button. On the next page, you will get a confirmation of the changed custom user field.</p>



<p>The PHP script we use for processing the data, is as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Set a Custom Field Value to a Subscriber 
 */

$clean_data    = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$subscriber_id = $clean_data['mc_uid'] ?? "1929189923777499";
$i             = 0;

// remove user id from array
unset($clean_data['mc_uid']);

foreach ($clean_data AS $customfield_name => $customfieldvalue) {
    if (!empty($customfieldvalue)) {
        echo "$customfield_name = $customfieldvalue &lt;br />";
        $subscriber_setcustomfield = $mc->subscriberSetCustomFieldByName($subscriber_id, $customfield_name, $customfieldvalue, $mc_api);
        if ($subscriber_setcustomfield) {
            $i++;
        }
    }
}

if ($i == 1) {
    echo "$i custom field has been updated";
} elseif ($i > 1) {
    echo "$i custom fields have been updated";
} else {
    echo "No fields have been changed";
}
</pre>



<p><code>$subscriber_setcustomfield</code> will return TRUE for a correct response, which we will use to increase $i for each successful change.</p>



<p>Let&#8217;s give it a go. Here&#8217;s what we edit:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="552" height="296" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-5.png" alt="" class="wp-image-25476" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-5.png 552w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-5-300x161.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-5-400x214.png 400w" sizes="(max-width: 552px) 100vw, 552px" /></figure>



<p>Once we clicked the button, we will get the following result:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="264" height="68" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-6.png" alt="" class="wp-image-25477"/></figure>



<p>As you may have noticed in our list screenshot, the custom fields I use already have an underscore in the name. It is always a good thing to ONLY use <code>az, AZ, 0-9, _</code> characters in your variable names. It&#8217;s never too late to start following this convention, so start doing that from now on:-)</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to set multiple custom fields  with /fb/subscriber/setCustomFields</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-set-multiple-custom-fields-with-fb-subscriber-setcustomfields/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Tue, 12 Jan 2021 23:38:14 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25480</guid>

					<description><![CDATA[The curl command as displayed on the ManyChat API Swagger is: The JSON format for this API is slightly different than other calls, because here, you can send an update in a batch of 10 records each time, making its way to less servercalls (1 instead of 10). The JSON format is: Why would you &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-set-multiple-custom-fields-with-fb-subscriber-setcustomfields/"> <span class="screen-reader-text">How to set multiple custom fields  with /fb/subscriber/setCustomFields</span> Read More &#187;</a></p>]]></description>
										<content:encoded><![CDATA[
<p>The curl command as displayed on the ManyChat API Swagger is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl -X POST "https://api.manychat.com/fb/subscriber/setCustomFields" -H "accept: application/json" -H "Authorization: Bearer 0" -H "Content-Type: application/json" -d "{ \"subscriber_id\": 0, \"fields\": [ { \"field_id\": 0, \"field_name\": 0, \"field_value\": \"'string', 123, true, '2018-07-18', '2018-07-02T00:00:00+00:00'\" } ]}"</pre>



<p>The JSON format for this API is slightly different than other calls, because here, you can send an update in a batch of 10 records each time, making its way to less servercalls (1 instead of 10).</p>



<p>The JSON format is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="json" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "subscriber_id": 0,
  "fields": [
    {
      "field_id": 0,
      "field_name": 0,
      "field_value": "'string', 123, true, '2018-07-18', '2018-07-02T00:00:00+00:00'"
    }
  ]
}</pre>



<h2>Why would you need this?</h2>



<p>If you use custom fields for user settings for example, chances are you need to update more than one at a time. Since you can also only update 10 queries per second, this API is a life saver for large updates.</p>



<p>We will use a similar webpage as in the previous chapter, and will show an altered PHP file that splits the updates up in batches of max 10 fields. More about that in a bit.</p>



<p>This is the default response on a successful request, is, as always:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "status": "success"
}</pre>



<h2>Let&#8217;s start with the scripts</h2>



<p>As mentioned, we are going to split this up in 2 parts again:<br>1. We create a webpage with a list of all custom fields and their current values, with an option to change it by adding a value in the form field. We don&#8217;t make a difference in string, number, boolean, date or datetime in this section, but you can if you want to in your system; <br>2. We create a PHP file to process the data in batches.</p>



<p>Let&#8217;s add a new function to our class file in classes/manychat.class.php.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="1045" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $subscriber_id
     * @param type $fields
     * @param type $mc_api
     * @return boolean
     */
    function subscriberSetCustomFields($subscriber_id, $fields, $mc_api)
    {

        $postfields_arr = [
            'subscriber_id' => $subscriber_id,
            'fields'        => $fields
        ];

        $postfields = json_encode($postfields_arr);

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/subscriber/setCustomFields");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields . PHP_EOL);
        $headers       = [
            'Authorization: Bearer ' . preg_replace("/^(\w+\s+)/", "", $mc_api),
            'Content-Type: application/json',
            'Accept: application/json'
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $server_output = curl_exec($ch);
        curl_close($ch);

        $json_obj  = json_decode($server_output);
        $good_call = $json_obj->status;

        if ($good_call == "success") {
            return true;
        } else {
            return false;
        }
    }</pre>



<p>After we created the new function, we will create 2 new PHP files:</p>



<ol><li>subscriber_setcustomfields_form.php</li><li>subscriber_setcustomfields.php</li></ol>



<p>The First file is to get the collection for the specific user, each custom field with its own form text input field</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;!DOCTYPE html>
&lt;html>
    &lt;head>
        &lt;title>Set Custom Field HTML Sample Page&lt;/title>
        &lt;meta charset="UTF-8">
        &lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
        &lt;!--Bootstrap CSS--> 
        &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    &lt;/head>
    &lt;body style="padding:50px;max-width:600px;">

        &lt;?php
        /*
         * Load the ManyChat Config File
         */
        require_once './config.inc.php';

        /*
         * Create Page and Form
         */

        $subscriber_id   = "1929189923777499";
        $subscriber_info = $mc->getSubscriberInfo($subscriber_id, $mc_api);
        $subscriber_cufs = $subscriber_info->custom_fields;

        ?>

        &lt;p style="margin:50px 0;">Let's get all Custom Fields and assign a value to it manually. When you press the button, your custom field value for the subscriber will be updated.&lt;/p>

        &lt;form method="post" action="subscriber_setcustomfields.php" id="setCustomField">
            &lt;div class="form-group">
                &lt;label for="mc_uid">ManyChat User ID&lt;/label>                
                &lt;input type="text" class="form-control" value="&lt;?= $subscriber_id ?: "Enter a valid userId in your Code First" ?>" id="mc_uid" name="mc_uid" />
            &lt;/div>

            &lt;?php
            foreach ($subscriber_cufs AS $cuf) {
                ?>
                &lt;div class="form-group">
                    &lt;label for="mc_uid">&lt;strong>&lt;?= $cuf->name ?>&lt;/strong>&lt;/label>                
                    &lt;input type="text" class="form-control" value="" id="" name="&lt;?= $cuf->id ?>" />
                    &lt;em>Current value: &lt;?= $cuf->value ?>&lt;/em>
                &lt;/div> 
            &lt;?php } ?>

            &lt;button type="submit" class="btn btn-primary">Set Custom Field Value&lt;/button>
        &lt;/form>

        &lt;!-- Optional JavaScript -->
        &lt;!-- jQuery first, Bootstrap JS -->
        &lt;script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">&lt;/script>
        &lt;script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">&lt;/script>
    &lt;/body>
&lt;/html>
</pre>



<p>If you run this page in your browser, you&#8217;ll see something similar to this (similar to the previous chapter&#8217;s page, we changed the form POST action:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="539" height="898" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-4.png" alt="" class="wp-image-25475" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-4.png 539w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-4-180x300.png 180w" sizes="(max-width: 539px) 100vw, 539px" /></figure>



<p>It&#8217;s a large list with all custom fields set to your current user ID. In the &#8220;real&#8221; world, you&#8217;d of course get this user ID from your own system instead of hard-coding it in the source code.</p>



<p>To change a custom field, just enter the new value in the form field and submit it by using the button. On the next page, you will get a confirmation of the changes custom user field.</p>



<p>The PHP script we use for processing the data, is as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="17,32,36" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Set Multiple Custom Field Values to a Subscriber 
 */

$clean_data    = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$subscriber_id = $clean_data['mc_uid'];
$i             = 0;

// remove user id from array
unset($clean_data['mc_uid']);

// count the array non empty variables
$number_of_non_empty_values = count(array_filter($clean_data, function($x) {
            return !empty($x);
        }));


// go through array and save in batches
foreach ($clean_data AS $customfield_id => $customfieldvalue) {
    if (!empty($customfieldvalue) &amp;&amp; $customfield_id !== "mc_uid") {

        $customfields[] = ["field_id" => $customfield_id, "field_value" => $customfieldvalue];
        $i++;

        if ($i % 10 == 0 &amp;&amp; $i &lt; $number_of_non_empty_values) {
            $subscriber_setcustomfields = $mc->subscriberSetCustomFields($subscriber_id, $customfields, $mc_api);
            $customfields_arr           = [];
            sleep(1);
        } elseif ($i === $number_of_non_empty_values) {
            $subscriber_setcustomfields = $mc->subscriberSetCustomFields($subscriber_id, $customfields, $mc_api);
        }
    }
}

if ($i == 1) {
    echo "$i custom field has been updated";
} elseif ($i > 1) {
    echo "$i custom fields have been updated";
} else {
    echo "No fields have been changed";
}
</pre>



<p>There are a few highlights in the script I&#8217;d like to point out. </p>



<p>On line 17 we <code>unset() </code>the ManyChat userId, so we get a clean count of the neccessary array values. <code>Unset </code>removes that value from the array.</p>



<p>On line 32 you see we gathered 10 lines and we will export that to the API, then, on line 36 we wait for 1 second before we move on.</p>



<p>Let&#8217;s give it a go. Here&#8217;s what we edit:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="552" height="296" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-5.png" alt="" class="wp-image-25476" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-5.png 552w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-5-300x161.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-5-400x214.png 400w" sizes="(max-width: 552px) 100vw, 552px" /></figure>



<p>Once we clicked the button, we will get the following result:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="264" height="68" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-6.png" alt="" class="wp-image-25477"/></figure>



<p>This is how you serve multiple records in one go. Your turn!</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to set a custom field with /fb/subscriber/setCustomField</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-set-a-custom-field-with-fb-subscriber-setcustomfield/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Tue, 12 Jan 2021 12:28:59 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25471</guid>

					<description><![CDATA[The curl command as displayed on the ManyChat API Swagger is: You need to replace the 0&#8217;s in the command with the appropriate data: ManyChat API key, subscriber ID, field_id, field_value (&#8216;string&#8217;, 123, true, &#8216;2018-07-18&#8217;, &#8216;2018-07-02T00:00:00+00:00&#8217;). I&#8217;ll show you how to use this and what it does below. Why would you need this? Setting a &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-set-a-custom-field-with-fb-subscriber-setcustomfield/"> <span class="screen-reader-text">How to set a custom field with /fb/subscriber/setCustomField</span> Read More &#187;</a></p>]]></description>
										<content:encoded><![CDATA[
<p>The curl command as displayed on the ManyChat API Swagger is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl -X POST "https://api.manychat.com/fb/subscriber/setCustomField" -H "accept: application/json" -H "Authorization: Bearer 0" -H "Content-Type: application/json" -d "{ \"subscriber_id\": 0, \"field_id\": 0, \"field_value\": \"'string', 123, true, '2018-07-18', '2018-07-02T00:00:00+00:00'\"}"</pre>



<p>You need to replace the 0&#8217;s in the command with the appropriate data: ManyChat API key, subscriber ID, field_id, field_value (&#8216;string&#8217;, 123, true, &#8216;2018-07-18&#8217;, &#8216;2018-07-02T00:00:00+00:00&#8217;). I&#8217;ll show you how to use this and what it does below.</p>



<h2>Why would you need this?</h2>



<p>Setting a custom field has a lot of use cases. Loyalty card points, progression values, user settings etcetera. In our platforms we use it to set Application settings/preferences that we use to create JSON output for our apps&#8217;functionality. Another use case is a shopping cart, you will store the cart contents in a custom field, an item price and more of these values. A custom field is used in every chatbot.</p>



<p>This is the default response on a successful request, is, as always:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "status": "success"
}</pre>



<h2>So how can we show you how to use it?</h2>



<p>We are going to split this up in 2 parts again:<br>1. We create a webpage with a list of all custom fields and their current values, with an option to change it by adding a value in the form field. We don&#8217;t make a difference in string, number, boolean, date or datetime in this section, but you can if you want to in your system; <br>2. We create a PHP file to process the data.</p>



<p>But First, as always, let&#8217;s add a new function to our class file in classes/manychat.class.php.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="1002" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $subscriber_id
     * @param type $field_id
     * @param type $field_value
     * @param type $mc_api
     * @return boolean
     */
    function subscriberSetCustomField($subscriber_id, $field_id, $field_value, $mc_api)
    {

        $postfields_arr = [
            'subscriber_id' => $subscriber_id,
            'field_id'      => $field_id,
            'field_value'   => $field_value
        ];

        $postfields = json_encode($postfields_arr);

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/subscriber/setCustomField");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields . PHP_EOL);
        $headers       = [
            'Authorization: Bearer ' . preg_replace("/^(\w+\s+)/", "", $mc_api),
            'Content-Type: application/json',
            'Accept: application/json'
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $server_output = curl_exec($ch);
        curl_close($ch);

        $json_obj  = json_decode($server_output);
        $good_call = $json_obj->status;

        if ($good_call == "success") {
            return true;
        } else {
            return false;
        }
    }</pre>



<p>After we created the new function, we will create 2 new PHP files:</p>



<ol><li>subscriber_setcustomfield_form.php</li><li>subscriber_setcustomfield.php</li></ol>



<p>The First file is to get the collection for the specific user, each custom field with its own form text input field</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;!DOCTYPE html>
&lt;html>
    &lt;head>
        &lt;title>Set Custom Field HTML Sample Page&lt;/title>
        &lt;meta charset="UTF-8">
        &lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
        &lt;!--Bootstrap CSS--> 
        &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    &lt;/head>
    &lt;body style="padding:50px;max-width:600px;">

        &lt;?php
        /*
         * Load the ManyChat Config File
         */
        require_once './config.inc.php';

        /*
         * Create Page and Form
         */

        $subscriber_id   = "1929189923777499";
        $subscriber_info = $mc->getSubscriberInfo($subscriber_id, $mc_api);
        $subscriber_cufs = $subscriber_info->custom_fields;

        ?>

        &lt;p style="margin:50px 0;">Let's get all Custom Fields and assign a value to it manually. When you press the button, your custom field value for the subscriber will be updated.&lt;/p>

        &lt;form method="post" action="subscriber_setcustomfield.php" id="setCustomField">
            &lt;div class="form-group">
                &lt;label for="mc_uid">ManyChat User ID&lt;/label>                
                &lt;input type="text" class="form-control" value="&lt;?= $subscriber_id ?: "Enter a valid userId in your Code First" ?>" id="mc_uid" name="mc_uid" />
            &lt;/div>

            &lt;?php
            foreach ($subscriber_cufs AS $cuf) {
                ?>
                &lt;div class="form-group">
                    &lt;label for="mc_uid">&lt;strong>&lt;?= $cuf->name ?>&lt;/strong>&lt;/label>                
                    &lt;input type="text" class="form-control" value="" id="" name="&lt;?= $cuf->id ?>" />
                    &lt;em>Current value: &lt;?= $cuf->value ?>&lt;/em>
                &lt;/div> 
            &lt;?php } ?>

            &lt;button type="submit" class="btn btn-primary">Set Custom Field Value&lt;/button>
        &lt;/form>

        &lt;!-- Optional JavaScript -->
        &lt;!-- jQuery first, Bootstrap JS -->
        &lt;script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">&lt;/script>
        &lt;script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">&lt;/script>
    &lt;/body>
&lt;/html>
</pre>



<p>If you run this page in your browser, you&#8217;ll see something similar to this:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="539" height="898" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-4.png" alt="" class="wp-image-25475" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-4.png 539w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-4-180x300.png 180w" sizes="(max-width: 539px) 100vw, 539px" /></figure>



<p>It&#8217;s a large list with all custom fields set to your current user ID. In the &#8220;real&#8221; world, you&#8217;d of course get this user ID from your own system instead of hard-coding it in the source code.</p>



<p>To change a custom field, just enter the new value in the form field and submit it by using the button. On the next page, you will get a confirmation of the changes custom user field.</p>



<p>The PHP script we use for processing the data, is as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Set a Custom Field Value to a Subscriber 
 */

$clean_data    = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$subscriber_id = $clean_data['mc_uid'] ?: exit("No subscriber found");
$i             = 0;

foreach ($clean_data AS $customfield_id => $customfieldvalue) {
    if (!empty($customfieldvalue) &amp;&amp; $customfield_id !== "mc_uid") {
        echo "$customfield_id = $customfieldvalue &lt;br />";
        $subscriber_setcustomfield = $mc->subscriberSetCustomField($subscriber_id, $customfield_id, $customfieldvalue, $mc_api);
        if ($subscriber_setcustomfield) {
            $i++;
        }
    }
}

if ($i == 1) {
    echo "$i custom field has been updated";
} elseif ($i > 1) {
    echo "$i custom fields have been updated";
} else {
    echo "No fields have been changed";
}
</pre>



<p><code>$subscriber_setcustomfield</code> will return TRUE for a correct response, which we will use to increase $i for each successful change.</p>



<p>Let&#8217;s give it a go. Here&#8217;s what we edit:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="552" height="296" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-5.png" alt="" class="wp-image-25476" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-5.png 552w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-5-300x161.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-5-400x214.png 400w" sizes="(max-width: 552px) 100vw, 552px" /></figure>



<p>Once we clicked the button, we will get the following result:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="264" height="68" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-6.png" alt="" class="wp-image-25477"/></figure>



<p>If you have tried to actually integrate this into your system now, you may have noticed a little caveat. Want to guess what?</p>



<p>I&#8217;ll tell you: what if you want to assign a value to a custom user field that has NOT been set earlier? It is not in this current list, since we only retrieve current custom fields that are already set and can be edited.</p>



<p>It is a great practice for you to learn the API system. Here&#8217;s how you can do it, go ahead and try!</p>



<ul><li>get a list of all custom fields in your bot</li><li>get your user Id</li><li>match all existing user fields and show the values</li><li>create a list of new fields that can still be assigned, you could cross reference them, or use arrays to intersect</li></ul>



<p>There are plenty of ways to do this though. Go crazy! <img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>If you have questions, please let me know.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to remove a TAG by name with /fb/subscriber/removeTagByName</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-remove-a-tag-by-name-with-fb-subscriber-removetagbyname/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Mon, 11 Jan 2021 23:00:51 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25457</guid>

					<description><![CDATA[The curl command as displayed on the ManyChat API Swagger is: You need to replace the 0&#8217;s in the command with the appropriate data: ManyChat API key, subscriber ID, TagName (string). I&#8217;ll show you how to use this and what it does below. Why would you need this? I&#8217;ll quote the exact text from the &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-remove-a-tag-by-name-with-fb-subscriber-removetagbyname/"> <span class="screen-reader-text">How to remove a TAG by name with /fb/subscriber/removeTagByName</span> Read More &#187;</a></p>]]></description>
										<content:encoded><![CDATA[
<p>The curl command as displayed on the ManyChat API Swagger is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl -X POST "https://api.manychat.com/fb/subscriber/removeTagByName" -H "accept: application/json" -H "Authorization: Bearer 0" -H "Content-Type: application/json" -d "{ "subscriber_id": 0, "tag_name": 0}"</pre>



<p>You need to replace the 0&#8217;s in the command with the appropriate data: ManyChat API key, subscriber ID, TagName (string). I&#8217;ll show you how to use this and what it does below.</p>



<h2>Why would you need this?</h2>



<p>I&#8217;ll quote the exact text from the previous chapter here:</p>



<blockquote class="wp-block-quote"><p>This API is perfect for managing data in your platform or application. For example: you may have tagged your user for a certain interest or follow-up action. Once this action is marked as complete, you can remove the Tag automatically.</p></blockquote>



<p>Because this method is mostly the same as the Remove Tag by ID, I will not just duplicate the previous chapter and rename ID with Name, but I will make a combination of both methods, in one file.</p>



<p>This is the default response on a successful request, is, as always:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "status": "success"
}</pre>



<h2>So how do we use this in our own system?</h2>



<p>We are going to split this up in 2 parts again:<br>1. We are going to get all tags by the user ID and display them in a &#8220;double&#8221; list, so we can &#8230;<br>2. Click a link which will remove the specified Tag from the user, either by ID, or Name, whatever is preferred. We will make 2 different links for this in this file, which could replace the file from the previous chapter.</p>



<p>IF you have the chance to use the TagID, I recommend using that. Although it is more obvious you know the name of a tag by hand, it is also more prone to errors. In general, programming languages don&#8217;t like anything other than <em>aZ0-9_</em> in variable values.</p>



<p>Let&#8217;s First add a new function to our class file in classes/manychat.class.php.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="961" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $tag_name
     * @param type $subscriber_id
     * @param type $mc_api
     * @return boolean
     */
    function subscriberRemoveTagByName($tag_name, $subscriber_id, $mc_api)
    {

        $postfields_arr = [
            'subscriber_id' => $subscriber_id,
            'tag_name'      => $tag_name
        ];

        $postfields = json_encode($postfields_arr);

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/subscriber/removeTagByName");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields . PHP_EOL);
        $headers       = [
            'Authorization: Bearer ' . preg_replace("/^(\w+\s+)/", "", $mc_api),
            'Content-Type: application/json',
            'Accept: application/json'
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $server_output = curl_exec($ch);
        curl_close($ch);

        $json_obj  = json_decode($server_output);
        $good_call = $json_obj->status;

        if ($good_call == "success") {
            return true;
        } else {
            return false;
        }
    }</pre>



<p>After we created the new function, we will create 2 new PHP files:</p>



<ol><li>subscriber_get_tags_by_id_and_name.php</li><li>subscriber_remove_tag_by_id_or_name.php</li></ol>



<p>The First file is to get a collection for the specific user, we display those for this time in a plain text list, with a hyperlink to delete them. It looks like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Get the Subscriber Tags by Name AND Id
 */

$subscriber_id   = "1929189923777499";
$subscriber_info = $mc->getSubscriberInfo($subscriber_id, $mc_api);

$subscriber_tags = $subscriber_info->tags;

foreach ($subscriber_tags AS $tag) {
    echo "TAG name and ID" . $tag->name . " (" . $tag->id . ") &lt;a href='./subscriber_remove_tag_by_id_or_name.php?tag_id=" . $tag->id . "&amp;mc_uid=" . $subscriber_id . "'>Remove by ID&lt;/a> -- &lt;a href='./subscriber_remove_tag_by_id_or_name.php?tag_name=" . $tag->name . "&amp;mc_uid=" . $subscriber_id . "'>Remove by Name&lt;/a>&lt;br />"; 
}</pre>



<p>It you run this script in your browser, you&#8217;ll see something similar to this:</p>



<figure class="wp-block-image size-large is-resized is-style-default"><img loading="lazy" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-2.png" alt="" class="wp-image-25462" width="714" height="325" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-2.png 707w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-2-300x137.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-2-400x182.png 400w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-2-600x273.png 600w" sizes="(max-width: 714px) 100vw, 714px" /></figure>



<p>It doesn&#8217;t look nice, but if you put this little snippet of code inside a basic Bootstrap theme, it will immediately look great. Just wrap the hyperlinks in HTML Buttons instead of textlinks and add a little padding between the paragraph lines and voilá! </p>



<p>Now let&#8217;s create the PHP file for the links, so we can remove and cleanup the Tag. The second PHP file will look like this, note that I created a simple if&lt;&gt;else clause here to check what variable is sent through the link. Since the Id is most accurate, we look fo rthat First, if it is not there, we move on to the next clause and we finish with an exit routine if nothing is found:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Remove Subscriber TAG by Id or Name
 */

$clean_data          = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$subscriber_tag_id   = $clean_data['tag_id'] ?? "";
$subscriber_tag_name = $clean_data['tag_name'] ?? "";
$mc_uid              = $clean_data['mc_uid'];

if (!empty($subscriber_tag_id)) {
    $subscriber_remove_tag = $mc->subscriberRemoveTag($subscriber_tag_id, $mc_uid, $mc_api);
    $subscriber_tag        = $subscriber_tag_id;
} elseif (!empty($subscriber_tag_name)) {
    $subscriber_remove_tag = $mc->subscriberRemoveTagByName($subscriber_tag_name, $mc_uid, $mc_api);
    $subscriber_tag        = $subscriber_tag_name;
} else {
    exit("No Tags set to remove");
}

if ($subscriber_remove_tag) {
    echo "Bingo! You removed the following Tag : " . $subscriber_tag . " from subscriber number " . $mc_uid;
} else {
    echo "We could not remove your Tag";
}</pre>



<p><code>$subscriber_remove_tag</code> will return TRUE for a correct response, which will allow us to report back the result.</p>



<p>So, when we click the <code>Remove by Name</code> link for <code>TAG Name and ID Language Preference is English (16205530)</code> from the Tag list above, we get this result:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="776" height="51" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-3.png" alt="" class="wp-image-25465" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-3.png 776w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-3-300x20.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-3-768x50.png 768w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-3-400x26.png 400w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-3-600x39.png 600w" sizes="(max-width: 776px) 100vw, 776px" /></figure>



<p>Pretty handy, right?</p>



<p>Next, we are going to set Custom fields, a very common and much used API.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to remove a TAG from a subscriber with /fb/subscriber/removeTag</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-remove-a-tag-from-a-subscriber-with-fb-subscriber-removetag/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Mon, 11 Jan 2021 17:14:00 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25445</guid>

					<description><![CDATA[The curl command as displayed on the ManyChat API Swagger is: You need to replace the 0&#8217;s in the command with the appropriate data: ManyChat API key, subscriber ID, TagID. I&#8217;ll show you how to use this and what it does below. Why would you need this? This API is perfect for managing data in &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-remove-a-tag-from-a-subscriber-with-fb-subscriber-removetag/"> <span class="screen-reader-text">How to remove a TAG from a subscriber with /fb/subscriber/removeTag</span> Read More &#187;</a></p>]]></description>
										<content:encoded><![CDATA[
<p>The curl command as displayed on the ManyChat API Swagger is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl -X POST "https://api.manychat.com/fb/subscriber/removeTag" -H "accept: application/json" -H "Authorization: Bearer 0" -H "Content-Type: application/json" -d "{ \"subscriber_id\": 0, \"tag_id\": 0}"</pre>



<p>You need to replace the 0&#8217;s in the command with the appropriate data: ManyChat API key, subscriber ID, TagID. I&#8217;ll show you how to use this and what it does below.</p>



<h2>Why would you need this?</h2>



<p>This API is perfect for managing data in your platform or application. For example: you may have tagged your user for a certain interest or follow-up action. Once this action is marked as complete, you can remove the Tag automatically.</p>



<p>One of the methods is removing the Tag by ID, which is what we will show you in this subject. <em>Removing Tag by Name</em> is reserved for the next chapter.</p>



<p>This is the default response on a successful request</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "status": "success"
}</pre>



<h2>So how do we use this in our own system?</h2>



<p>We are going to split this up in 2 parts:<br>1. We are going to get all tags by the user ID and display them in a list, so we can &#8230;<br>2. Click a link which will remove the specified Tag from the user (in this chapter, by ID)</p>



<p>First we will add a new function to our class file in classes/manychat.class.php.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="920" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $tag_id
     * @param type $subscriber_id
     * @param type $mc_api
     * @return boolean
     */
    function subscriberRemoveTag($tag_id, $subscriber_id, $mc_api)
    {

        $postfields_arr = [
            'subscriber_id' => $subscriber_id,
            'tag_id'        => $tag_id
        ];

        $postfields = json_encode($postfields_arr);

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/subscriber/removeTag");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields . PHP_EOL);
        $headers       = [
            'Authorization: Bearer ' . preg_replace("/^(\w+\s+)/", "", $mc_api),
            'Content-Type: application/json',
            'Accept: application/json'
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $server_output = curl_exec($ch);
        curl_close($ch);

        $json_obj  = json_decode($server_output);
        $good_call = $json_obj->status;

        if ($good_call == "success") {
            return true;
        } else {
            return false;
        }
    }</pre>



<p>After we created the new function, we will create 2 new PHP files:</p>



<ol><li>subscriber_get_tags.php</li><li>subscriber_remove_tag.php</li></ol>



<p>The First file is to get a collection for the specific user, we display those for this time in a plain text list, with a hyperlink to delete them. It looks like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Get the Subscriber Tags 
 */

$subscriber_id   = "1929189923777499";
$subscriber_info = $mc->getSubscriberInfo($subscriber_id, $mc_api);

$subscriber_tags = $subscriber_info->tags;

foreach ($subscriber_tags AS $tag) {
    echo "&lt;a href='./subscriber_remove_tag.php?tag_id=" . $tag->id . "&amp;mc_uid=" . $subscriber_id . "'>Remove TAG " . $tag->name . " (" . $tag->id . ")&lt;/a>&lt;br />";
}</pre>



<p>It you run this script in your browser, you&#8217;ll see something similar to this:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="466" height="268" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image.png" alt="" class="wp-image-25452" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image.png 466w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-300x173.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-400x230.png 400w" sizes="(max-width: 466px) 100vw, 466px" /></figure>



<p>Now let&#8217;s create the PHP file for the clicked links, so we can remove and cleanup the Tag. The second PHP file will look like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Remove Subscriber TAG 
 */

$clean_data        = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$subscriber_tag_id = $clean_data['tag_id'];
$mc_uid            = $clean_data['mc_uid'];

$subscriber_remove_tag = $mc->subscriberRemoveTag($subscriber_tag_id, $mc_uid, $mc_api);

if ($subscriber_remove_tag) {
    echo "Removed the Tag with ID " . $subscriber_tag_id . " from subscriber number " . $mc_uid;
} else {
    echo "We could not remove your Tag ID";
}</pre>



<p><code>$subscriber_remove_tag</code> will return TRUE for a correct response, which will allow us to report back the result.</p>



<p>So, when we click the link <code>Remove TAG scratchcard_has_result (15267260)</code> from the Tag list above, we get this result:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="536" height="61" src="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-1.png" alt="" class="wp-image-25455" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-1.png 536w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-1-300x34.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2021/01/image-1-400x46.png 400w" sizes="(max-width: 536px) 100vw, 536px" /></figure>



<p>In the next chapter, we will do this again, but with a Tag name instead of the ID.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to assign a TAG to a subscriber with /fb/subscriber/addtagbyname</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-assign-a-tag-to-a-subscriber-with-fb-subscriber-addtagbyname/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Fri, 08 Jan 2021 13:50:56 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25436</guid>

					<description><![CDATA[The curl command as displayed on the ManyChat API Swagger is: You need to replace the 0&#8217;s and &#8220;string&#8221; in the command with the appropriate data. I&#8217;ll show you how to use this and what it does below. The purpose of this API call is to assign a new TAG by NAME to your ManyChat &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-assign-a-tag-to-a-subscriber-with-fb-subscriber-addtagbyname/"> <span class="screen-reader-text">How to assign a TAG to a subscriber with /fb/subscriber/addtagbyname</span> Read More &#187;</a></p>]]></description>
										<content:encoded><![CDATA[
<p>The curl command as displayed on the ManyChat API Swagger is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl -X POST "https://api.manychat.com/fb/subscriber/addTagByName" -H "accept: application/json" -H "Authorization: Bearer 0" -H "Content-Type: application/json" -d "{ \"subscriber_id\": 0, \"tag_name\": \"string\"}"</pre>



<p>You need to replace the 0&#8217;s and &#8220;string&#8221; in the command with the appropriate data. I&#8217;ll show you how to use this and what it does below.</p>



<p>The purpose of this API call is to assign a new TAG by NAME to your ManyChat subscriber. This API requires a Tag NAME which is a lot easier than the Tag ID. However the downside is if you use it via forms, the spaces or special characters in the form entry fields, may be altered by PHP. Forexample, a space might be translated into an underscore (_). If you use your own fixed code, or verification methods, you&#8217;ll fine fine!</p>



<blockquote class="wp-block-quote"><p>Your Tag must pre-exist</p></blockquote>



<h2>Why would you need this?</h2>



<p>The reasons for needing this, are obviously identical to those of the addTag via an ID. It&#8217;s just a different approach. If you want to TAG users for specific actions in your system, for example a completed step in your flow, this TAG has to pre-exist. With <a href="https://v3.chatbotdojo.com/topic/how-to-add-a-page-tag-with-page-createtag/" class="rank-math-link">this API call</a> you can create such a TAG automatically. Once your Tag is created, you can assign it to a user with this API. It&#8217;s a nice tool if you want to segment your users from your own CMS or (ecom) platform. You can document the entire path your user takes in your bot.</p>



<p>This is the default response on a successful request</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "status": "success"
}</pre>



<h2>So how do we use this in our own system?</h2>



<p>First we will add a new function to our class file in classes/manychat.class.php.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="879" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $subscriber_id
     * @param type $tag_name
     * @param type $mc_api
     * @return boolean
     */
    function subscriberAddTagByName($subscriber_id, $tag_name, $mc_api)
    {

        $postfields_arr = [
            'subscriber_id' => $subscriber_id,
            'tag_name'        => $tag_name
        ];

        $postfields = json_encode($postfields_arr);

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/subscriber/addTagByName");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields . PHP_EOL);
        $headers       = [
            'Authorization: Bearer ' . preg_replace("/^(\w+\s+)/", "", $mc_api),
            'Content-Type: application/json',
            'Accept: application/json'
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $server_output = curl_exec($ch);
        curl_close($ch);

        $json_obj  = json_decode($server_output);
        $good_call = $json_obj->status;

        if ($good_call == "success") {
            return true;
        } else {
            return false;
        }
    }</pre>



<p>After we created the new function, we will create a new PHP file and call it <strong><em>subscriber_addtagbyname.php</em></strong>. It look like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Set a Tag to a Subscriber with addTag by Name
 */

$subscriber_id     = "1929189923777499";
$tag_name            = "Received Coupon";
$subscriber_addtag = $mc->subscriberAddTagByName($subscriber_id, $tag_name, $mc_api);

if ($subscriber_addtag) {
    echo "Successfully added tag ID $tag_name to user $subscriber_id";
}</pre>



<p><code>$subscriber_addtag</code> will return TRUE for a correct response, which will allow us to report back the result.</p>



<p>What is your use case?</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to assign a TAG to a subscriber with /fb/subscriber/addTag</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-assign-a-tag-to-a-subscriber-with-fb-subscriber-addtag/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Sun, 18 Oct 2020 14:41:51 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25427</guid>

					<description><![CDATA[The curl command as displayed on the ManyChat API Swagger is: You need to replace the 0&#8217;s in the command with the appropriate data. I&#8217;ll show you how to use this and what it does below. The purpose of this API call is to assign a new TAG by ID to your ManyChat subscriber. This &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-assign-a-tag-to-a-subscriber-with-fb-subscriber-addtag/"> <span class="screen-reader-text">How to assign a TAG to a subscriber with /fb/subscriber/addTag</span> Read More &#187;</a></p>]]></description>
										<content:encoded><![CDATA[
<p>The curl command as displayed on the ManyChat API Swagger is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl -X POST "https://api.manychat.com/fb/subscriber/addTag" -H "accept: application/json" -H "Authorization: Bearer 0" -H "Content-Type: application/json" -d "{ \"subscriber_id\": 0, \"tag_id\": 0}"</pre>



<p>You need to replace the 0&#8217;s in the command with the appropriate data. I&#8217;ll show you how to use this and what it does below.</p>



<p>The purpose of this API call is to assign a new TAG by ID to your ManyChat subscriber. This API requires a Tag ID which is pretty annoying if I may say so, the API after this lesson is the one you&#8217;re most likely be using, which is the Tag by Name.</p>



<p>In both cases, your Tag must pre-exist.</p>



<h2>Why would you need this?</h2>



<p>If you want to TAG users for specific actions in your system, for example a completed step in your flow, this TAG has to pre-exist. With <a href="https://v3.chatbotdojo.com/topic/how-to-add-a-page-tag-with-page-createtag/" class="rank-math-link">this API call</a> you can create such a TAG automatically. Once your Tag is created, you can assign it to a user with this API. It&#8217;s a nice tool if you want to segment your users from your own CMS or (ecom) platform. You can document the entire path your user takes in your bot.</p>



<p>This is the default response on a successful request</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "status": "success"
}</pre>



<h2>So how do we use this in our own system?</h2>



<p>First we will add a new function to our class file in classes/manychat.class.php.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="838" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $subscriber_id
     * @param type $tag_id
     * @param type $mc_api
     * @return boolean
     */
    function subscriberAddTag($subscriber_id, $tag_id, $mc_api)
    {

        $postfields_arr = [
            'subscriber_id' => $subscriber_id,
            'tag_id'        => $tag_id
        ];

        $postfields = json_encode($postfields_arr);

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/subscriber/addTag");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields . PHP_EOL);
        $headers       = [
            'Authorization: Bearer ' . preg_replace("/^(\w+\s+)/", "", $mc_api),
            'Content-Type: application/json',
            'Accept: application/json'
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $server_output = curl_exec($ch);
        curl_close($ch);

        $json_obj  = json_decode($server_output);
        $good_call = $json_obj->status;

        if ($good_call == "success") {
            return true;
        } else {
            return false;
        }
    }</pre>



<p>After we created the new function, we will create a new PHP file and call it <strong><em>subscriber_addtag.php</em></strong>. It look like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Set a Tag to a Subscriber with addTag 
 */

$subscriber_id     = "1929189923777499";
$tag_id            = "23169190";
$subscriber_addtag = $mc->subscriberAddTag($subscriber_id, $tag_id, $mc_api);

if ($subscriber_addtag) {
    echo "Successfully added tag ID $tag_id to user $subscriber_id";
}</pre>



<p><code>$subscriber_addtag</code> will return TRUE for a correct response.</p>



<p>The Tag ID can be looked up in your settings menu inside ManyChat, but we know that is of course a pain in the butt.</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="307" src="https://v3.chatbotdojo.com/wp-content/uploads/2020/10/image-5-1024x307.png" alt="" class="wp-image-25433" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2020/10/image-5-1024x307.png 1024w, https://v3.chatbotdojo.com/wp-content/uploads/2020/10/image-5-300x90.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2020/10/image-5-768x230.png 768w, https://v3.chatbotdojo.com/wp-content/uploads/2020/10/image-5-400x120.png 400w, https://v3.chatbotdojo.com/wp-content/uploads/2020/10/image-5-600x180.png 600w, https://v3.chatbotdojo.com/wp-content/uploads/2020/10/image-5.png 1498w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>A better, cooler way which you now know how to do (because we learned that in <a href="https://v3.chatbotdojo.com/topic/how-to-get-all-page-tags-with-fb-page-gettags/" class="rank-math-link">this lesson</a>), is to get this info with the page API. But even then, we&#8217;d rather use the next one, which is addTagByName. So let&#8217;s head on to the next lesson.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
