Last active
August 29, 2015 13:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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