我编写了一个代码来缩短我的URL,但是在URL编码过程中遇到了问题,因为URL用{$CWunschliste->cURLID}调用ID
{php}$short = file_get_contents('http://ur.l/api.php?url=' . urlencode({$ShopURL}/index.php?wlid={$CWunschliste->cURLID}));{/php}
{php}$url=json_decode($short,TRUE);{echo $url['short'];}{/php}如何重写url代码以调用{$CWunschliste->cURLID}?
发布于 2014-06-09 22:06:55
如果在Smarty模板中使用{php}标记,则应该将PHP代码放在那里,因此不能使用Smarty语法。您的代码应该如下所示:
{php}
$short = file_get_contents('http://ur.l/api.php?url=' . urlencode($this->getTemplateVars('ShopURL').'/index.php?wlid='.$this->getTemplateVars('CWunschliste')->cURLID));
$url=json_decode($short,TRUE);
echo $url['short'];
{/php}然而,不应该这样做。您应该在PHP中做这样的事情。Smarty只用于显示视图,而不是从远程urls或其他模型任务中获取数据。
此外,不推荐使用{php}标记,并且只能在SmartyBC类中使用。
编辑
我忘记从答案中删除{ (已经修复了),但即使在本例中,我也得到了以下错误
在对象上下文中不使用$this
这真的很奇怪,因为如果您查看本例中的Smarty php标记示例,$this用于访问Smarty对象。
然而,工作解决方案将是将$this转换为$template (我必须强调,在这种情况下,$template不是用PHP创建的Smarty对象的名称--我已经创建了Smarty对象$smarty,但是在{php}标记中,我必须使用$template访问Smarty )。
下面的代码应该适用于您:
{php}
$short = file_get_contents('http://ur.l/api.php?url=' . urlencode($template->getTemplateVars('ShopURL').'/index.php?wlid='.$template->getTemplateVars('CWunschliste')->cURLID));
$url=json_decode($short,TRUE);
echo $url['short'];
{/php}https://stackoverflow.com/questions/24129751
复制相似问题