Skip to content

Instantly share code, notes, and snippets.

@stephenreid
Last active December 13, 2015 17:49
Show Gist options
  • Save stephenreid/4951024 to your computer and use it in GitHub Desktop.
Save stephenreid/4951024 to your computer and use it in GitHub Desktop.
Takes GET url w/ Post Parameters and sends the request on to a very basic rest server.
<?php
class apiProcessor{
private $gets;
private $posts;
public function __construct($gets,$posts){
$this->gets = $gets;
$this->posts = $posts;
}
public function process(){
$url = $this->gets['url'];
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',//never want to send credentials over GET
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($this->posts),
'timeout' => 30.0, //in seconds
'user_agent'=> 'XMLToJsonAPIClient',
)
));
$res = file_get_contents($url,false,$context);
$xmlObject = simplexml_load_string($res);
$json = json_encode($xmlObject);
return $json;
}
}
$processor = new apiProcessor($_GET,$_POST);
echo $processor->process();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment