If it can be done in PHP, it can probably be done in Wappler

I am not going to do one of my huge posts for this because I am pretty sure anybody that knows PHP will be able to adapt the concept to what their needs are.

Sometimes I have the need to use something that I already know how to do in PHP very easily, but I either do not have the available option in App Connect or just have not found it.

Take this example, I would like to GeoLocate a client based upon their IP Address, so I create a new page, add my standard App Connect, and Bootstrap 4 CDN, next I add the browser component, and realise there is nothing in there to retrieve the users IP Address, so I add a GeoLocation component thinking it might be in there, and besides Lat and Long coords speed, altitude etc. I see no provision for obtaining the users IP Address.
Obviously I know I could get this data from the Server Connect side, but in this case I really need it in the App Connect side.

This is how
Create an App Connect Variable from Data > Variable
This gives the variable properties of ID, so lets set that to, my_user_ip_address then it gives value, the issue is if you use that value area, it does a dmx-bind:value which will not work in this case, instead I need an actual value and not a bound one.

This is the simplest version of how to get PHP code into App Connect
Add > Data > Variable, then in code view add value=""

<!--ADD PHP TO APP CONNECT VARIABLE-->
<dmx-value id="var1" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"></dmx-value>

<!--CALL THE APP CONNECT VALUE TO DISPLAY IT-->
{{var1.value}}

Lets try it a slightly different way by adding the php into a php variable first

<?php
  $php_var = $_SERVER['REMOTE_ADDR'];
?>

<!--ADD PHP VARIABLE TO APP CONNECT VARIABLE-->
<dmx-value id="var1" value="<?php echo $php_var; ?>"></dmx-value>

<!--CALL THE APP CONNECT VALUE TO DISPLAY IT-->
{{var1.value}}

How about a little more complex, the actual code i use for the https://www.geoplugin.com/webservices/php php API, if it does not work please ensure your php.ini has allow_url_include / allow_url_fopen on

<?php
  function countrychecker($amount) {
    global $geoPlugin_array;

    if (isset($geoPlugin_array['geoplugin_currencyCode']) && $geoPlugin_array['geoplugin_currencyCode'] != 'GBP') { 
      return ($amount * $geoPlugin_array['geoplugin_currencyConverter']);
    }
    return $amount;
  }

  $geoPlugin_array = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR'] . '&base_currency=GBP') );
?>

<dmx-value id="var_geo_price" value="<?php echo countrychecker(800) ?>"></dmx-value>
<dmx-value id="var_geo_price_symbol" value="<?php echo $geoPlugin_array['geoplugin_currencySymbol'] ?>"></dmx-value>
<dmx-value id="var_geo_price_formatted" dmx-bind:value="var_geo_price.value.toNumber().formatCurrency(var_geo_price_symbol.value, '.', ',', '2')"></dmx-value>
<dmx-value id="var_gb_checker" value="<?php echo $geoPlugin_array['geoplugin_countryName'] ?>"></dmx-value>

Formatted Price {{var_geo_price_formatted.value}}
Currency symbol {{var_geo_price_symbol.value}}
Unformatted Price {{var_geo_price.value}}
Country Name {{var_gb_checker.value}}

<div id="conditional_check_loadedA" is="dmx-if" dmx-bind:condition="var_gb_checker.value != 'United Kingdom'">
  Runs when country is NOT United Kindgom
</div>
<div id="conditional_check_loadedB" is="dmx-if" dmx-bind:condition="var_gb_checker.value == 'United Kingdom'">
  Runs when country is United Kindgom
</div>

Obviously with the concept above in mind I think almost anything can be done.

Lets see what other users and @wappler_ambassadors think about this, and lets hope i do not get in trouble for posting this, it is NOT the normal Wappler way, it is very hacky, probably not recommended, but sometimes when needs must.

Community Page
Last updated: