assuming your function returns the correct amount, store the $myShippingAmount in a session variable, then use the session variable in your shipping rule
set the shipping rule to use the trigger:
Number of unique items in the cart > 0
and a flat rate shipping of '$_SESSION['myShippingAmount']'