code-rhapsodie/get-url-contents-ez-twig-bundle
composer require code-rhapsodie/get-url-contents-ez-twig-bundle
config/bundles.php:
return [
// ...
CodeRhapsodie\GetUrlContentsEzTwigBundle\GetUrlContentsEzTwigBundle::class => ['all' => true],
];
{% set externalContent = get_url_contents('https://api.example.com/data') %}
{{ externalContent|raw }} {# Display raw HTML/JSON if needed #}
Dynamic Content Injection
{% if get_url_contents('https://status.example.com') contains 'operational' %}
<div class="status-ok">All systems go!</div>
{% endif %}
{% cache %} to avoid repeated HTTP calls:
{% cache for 3600 seconds %}
{{ get_url_contents('https://slow-api.example.com') }}
{% endcache %}
Legacy eZ Publish Integration
{def $content = get_url_contents($url)}{/def}
{assign var="remoteData" value=$content}
content.view.full.tpl for footer scripts or ads:
{get_url_contents('https://ads.example.com/banner')|raw}
API Proxying
{% set userData = get_url_contents('https://user-service.example.com/' ~ currentUser.id) %}
{% if %} to handle failures gracefully:
{% set content = get_url_contents('https://unreliable-api.example.com') %}
{% if content == '' %}
<p>Failed to load external content.</p>
{% else %}
{{ content|striptags }}
{% endif %}
curl options (e.g., timeouts) via a custom service (extend the bundle’s UrlFetcher class).No CORS/Authentication Support
curl without headers/cookies. For APIs requiring auth:
{# ❌ Won't work #}
{{ get_url_contents('https://api.example.com/protected', { 'headers': { 'Authorization': 'Bearer token' } }) }}
Empty String on Failure
''. Use {% if %} checks or a custom filter to distinguish:
{% set result = get_url_contents(url) %}
{% if result == '' %}
{# Handle error #}
{% endif %}
Legacy Template Quirks
curl Errors: Enable debug mode in config/packages/dev.php to log HTTP failures:
framework:
twig:
debug: true
{{ get_url_contents(url)|json_encode|raw }} {# Debug JSON APIs #}
Customize curl Options
Override the bundle’s UrlFetcher service (defined in Resources/config/services.yaml):
services:
CodeRhapsodie\GetUrlContentsEzTwigBundle\Service\UrlFetcher:
arguments:
$timeout: 10 # Default: 30 seconds
$allowedDomains: ['api.example.com', 'cdn.example.com'] # Whitelist
Add Headers/Proxies
Extend the UrlFetcher class to support headers:
// src/Service/CustomUrlFetcher.php
class CustomUrlFetcher extends UrlFetcher {
public function fetch(string $url, array $options = []): string {
$options['headers'] = $options['headers'] ?? [
'User-Agent' => 'MyApp/1.0',
];
return parent::fetch($url, $options);
}
}
Update services.yaml to use your class.
Twig Filter for Validation Create a filter to validate responses (e.g., check for JSON):
// src/Twig/AppExtension.php
class AppExtension extends \Twig\Extension\AbstractExtension {
public function getFilters() {
return [
new \Twig\TwigFilter('is_valid_json', function($content) {
return json_decode($content) !== null;
}),
];
}
}
Usage:
{% if get_url_contents(url)|is_valid_json %}
{{ get_url_contents(url)|json_decode|json_encode|raw }}
{% endif %}
config/packages/ files, so all settings must be injected via services.{assign}) may behave differently than Twig.
```markdown
## Maturity Considerations
- **Last Release (2020)**: Verify compatibility with your eZ Platform/Laravel version (if using eZ).
- **No Tests/Documentation**: Assume minimal edge-case handling; test thoroughly for production use.
- **MIT License**: Safe for commercial projects, but audit the `curl`-based implementation for your needs.
How can I help you explore Laravel packages today?