Custom tags that access my API

ARMGAMES

New Member
Hey, is there any way to make a custom tag that will access to my api and get some data when i call it?
I need to generate keys for my subscribers, how can i do that? Thank you for any answer.
 

twisted1919

Administrator
Staff member
@ARMGAMES - You can do that, here's how:
1. Create a extension for it: https://forum.mailwizz.com/threads/example-extensions-for-extension-developemnt.1958/
2. In your extensions run() method, add something like:
PHP:
Yii::app()->hooks->addFilter('campaigns_get_common_tags_search_replace', function($searchReplace){

    // see /apps/common/components/helpers/CampaignHelper.php#L657 for more tags:
    $subscriber_uid   = $searchReplace['[SUBSCRIBER_UID]'];
    $campaign_uid     = $searchReplace['[CAMPAIGN_UID]'];
    $subscriber_email = $searchReplace['[EMAIL]'];

    // do a curl call for this subscriber to generate whatever you need.
    // put it in the $content variable and be done with it:

    $content = super_duper_function_that_makes_remote_call($subscriber_email);

    // inject the custom tag
    $searchReplace['[YOUR_CUSTOM_UNIQUE_TAG_NAME]'] = $content;

    // return new search/replace array
    return $searchReplace;
});
Save the file, enable the extension and that's it.
 

ARMGAMES

New Member
PHP:
Yii::app()->hooks->addFilter('campaigns_get_common_tags_search_replace', function($searchReplace){
    $mask = $this->getOption('mask', '1hour');
    $amount = $this->getOption('amount', 1);

    $request = $this->tgoGiftRequest($mask, $amount);

    $searchReplace['[TGO_MAKE_GIFT]'] = $request;
    return $searchReplace;
});

API request work's (i see that in my logs), but my tag replaced with 1. My api answer with json.
What i do wrong?
 
Last edited:

ARMGAMES

New Member
It was curl.
PHP:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //- fix the problem.

Now i have other have. I receive a "https://link.com" from my api.
But it's insert it at my email template like a html tag "a".
PHP:
<a href="https://link.com" target="_blank" rel="noopener">https://link.com</a>
How can i change it to text?
PHP:
Yii::app()->hooks->addFilter('campaigns_get_common_tags_search_replace', function($searchReplace){
    $mask = $this->getOption('mask', '1hour');
    $amount = $this->getOption('amount', 1);
    $wrapStart = $this->getOption('wrapStart');
    $wrapStop = $this->getOption('wrapStop', '<br />');

    $request = $this->tgoGiftRequest($mask, $amount);
    $json = json_decode($request);

    $content = '';
    if (count($json) == 1)
        $content = $wrapStart . $json[0] . $wrapStop;
    else {
        foreach ($json as $gift) {
            $content .= $wrapStart . $gift . $wrapStop;
        }
    }

    $searchReplace['[TGO_MAKE_GIFT]'] = $content;
    return $searchReplace;
});
P.S.
wrapsStart and wrapStop are empty
 
Last edited:

ARMGAMES

New Member
@twisted1919
Hey, today i send 4200 emails to my customers, but API request won't work. It's works when i was at "Template" and use Test template, but when i send it, there was 0 requests to my api. So all 4200 customers have not received gifts(link for gift they must receive from my api). What can that be?

And this is what i see when i go to "Web version" (attach a screenshot)
 

Attachments

  • 2016-06-13_21-18-13.png
    2016-06-13_21-18-13.png
    24.7 KB · Views: 5
Last edited:

twisted1919

Administrator
Staff member
@ARMGAMES - the fact you still have the [TGO_MAKE_GIFT] in the web version/email template it's okay. you're accessing the page without a subscriber context.
As for why the api calls didn't suceed, that beats me, you have to troubleshoot and see what fails.
 

twisted1919

Administrator
Staff member
I think the problem is in this code:
PHP:
$request = $this->tgoGiftRequest($mask, $amount);
                $json = json_decode($request);

                $content = "";
                if (count($json) == 1)
                    $content = $this->checkWrap($json[0]);
                else {
                    foreach ($json as $gift) {
                        $content .= $this->checkWrap($gift) . '<br />';
                    }
                }
$json is an array of objects and you pass each of it's objects to the checkWrap method which looks like it requires a string not an object:
PHP:
public function checkWrap($url){
        return ($this->getOption('wrapWithA', 'no') == 'yes') 
                ? $url : CHtml::encode($url);
    }
So maybe actually the $gift object has a url property and you need to use that?
 

ARMGAMES

New Member
It's just array of urls that look like this.
PHP:
[
    "https://test.url",
    "https://test.url2",
    "https://test.url3"
]
 

twisted1919

Administrator
Staff member
It's just array of urls that look like this.
PHP:
[
    "https://test.url",
    "https://test.url2",
    "https://test.url3"
]
The result returned from your api ?
If so, here's how your code should look like:

PHP:
$urls = json_decode($this->tgoGiftRequest($mask, $amount), true);
$content = "";
foreach ($urls as $url) {
    $content .= $this->checkWrap($url) . '<br />';
}

// 
public function checkWrap($url){
    return ($this->getOption('wrapWithA', 'no') == 'yes') ? $url : CHtml::encode($url);
}
 
Top