Skip to content

Instantly share code, notes, and snippets.

@hasinhayder
Last active August 29, 2015 13:57
Show Gist options
  • Save hasinhayder/9705763 to your computer and use it in GitHub Desktop.
Save hasinhayder/9705763 to your computer and use it in GitHub Desktop.
A new twig function which can fetch external URL via GET or POST and pass arbitrary parameters while fetching
<?php
//file: fetch.php
use Twig_Extension;
use Twig_Function_Method;
class Fetch extends Twig_Extension
{
public function fetch ($url, $params=array())
{
if($url){
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
if(count($params)>0){
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$params);
}
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}
public function getFunctions ()
{
return array(
'fetch' => new Twig_Function_Method($this, 'fetch', array('is_safe' => array('html')))
);
}
public function getName ()
{
return 'fetch';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment