Skip to content

Instantly share code, notes, and snippets.

@keyvanakbary
Created March 6, 2015 07:39
Show Gist options
  • Save keyvanakbary/49d456dde2f53dc42aba to your computer and use it in GitHub Desktop.
Save keyvanakbary/49d456dde2f53dc42aba to your computer and use it in GitHub Desktop.
Simple HTTP client for GET and POST methods
<?php
class StreamHttpClient implements HttpClient
{
public function request($url, $method, array $parameters = [])
{
$content = http_build_query($parameters);
$options = ['method' => $method];
if (strtolower($method) === 'post') {
$options += [
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $content
];
}
if (strtolower($method) === 'get') {
$url .= '?' . $content;
}
return file_get_contents($url, false, stream_context_create([
'http' => $options
]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment