Skip to content

Instantly share code, notes, and snippets.

@jrobinsonc
Last active June 26, 2017 18:04
Show Gist options
  • Select an option

  • Save jrobinsonc/e4f58bf98ae368f2162e2052eb36a6b3 to your computer and use it in GitHub Desktop.

Select an option

Save jrobinsonc/e4f58bf98ae368f2162e2052eb36a6b3 to your computer and use it in GitHub Desktop.
Send GET and POST requests with file_get_contents.
<?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