从symfony2中的URL导入JSON

时间:2015-09-06 08:08:33

标签: json symfony twig

从Symfony2中的URL导入json内容并在twig模板中输出变量的最佳做法是什么?

$url = https://btc-e.com/api/2/btc_usd/ticker

1 个答案:

答案 0 :(得分:0)

您可以使用file_get_contents检索json内容,然后将其传递给视图。您的控制器可能如下所示:

//…
class FooController extends Controller
{
    //…
    public function getJsonContentAction()
    {
        $jsonContent = file_get_contents("https://btc-e.com/api/2/btc_usd/ticker");

        return $this->render(
            'AppBundle:Foo:bar.html.twig', // Edit it to insert the view you want to render
            [
                'ticker' => json_decode($jsonContent),
            ]
        );
    }
}

然后在你的树枝模板中,显示:

{{ ticker.ticker.high }}