Maybe I donโt understand what you mean by the "canonical url", but if you mean the product URL with its identifier and the key at the end, which is usually the "canonical url" for magento, since it must be unique in one moment, you should just remove getUrl parameters. If you don't need a key, you can still use:
$url = substr($url, 0, strrpos('/s/'));
I hope this helps, if not, please clarify the result you want.
Christophe ferreboeuf
source share