在 symfony2 中从 URL 导入 JSON
Importing JSON from URL in symfony2
从 Symfony2 中的 URL 导入 json 内容并在 twig 模板中输出变量的最佳实践是什么?
$url = https://btc-e.com/api/2/btc_usd/ticker
您可以使用 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 }}
从 Symfony2 中的 URL 导入 json 内容并在 twig 模板中输出变量的最佳实践是什么?
$url = https://btc-e.com/api/2/btc_usd/ticker
您可以使用 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 }}