Created
March 6, 2015 07:39
-
-
Save keyvanakbary/49d456dde2f53dc42aba to your computer and use it in GitHub Desktop.
Simple HTTP client for GET and POST methods
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 | |
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