Last active
June 26, 2017 18:04
-
-
Save jrobinsonc/e4f58bf98ae368f2162e2052eb36a6b3 to your computer and use it in GitHub Desktop.
Send GET and POST requests with file_get_contents.
This file contains hidden or 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 | |
| function sendRequest($method, $url, $content = '', $headers = []) { | |
| $opts = [ | |
| 'http' => [ | |
| 'method' => $method, | |
| 'header' => implode("\r\n", $headers), | |
| 'content' => $content | |
| ] | |
| ]; | |
| return file_get_contents($url, false, stream_context_create($opts)); | |
| } | |
| // Usage | |
| $data = [ | |
| 'param1' => 'value1', | |
| 'param2' => 'value2' | |
| ]; | |
| $headers = [ | |
| 'Content-Type: application/x-www-form-urlencoded' | |
| ]; | |
| sendRequest('POST', 'https://requestb.in/xyz123', http_build_query($data), $headers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment