Skip to content

Instantly share code, notes, and snippets.

@slav123
Last active September 17, 2021 10:30
Show Gist options
  • Save slav123/4e3a6c2f671cf0c2960a to your computer and use it in GitHub Desktop.
Save slav123/4e3a6c2f671cf0c2960a to your computer and use it in GitHub Desktop.
PHP POST request with file_get_contents
<?php
$data = [];
// www-form-urlencoded
$opts = array('http' => array('method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => http_build_query($data)
));
// json
$opts = array('http' => array('method' => 'POST',
'header' => 'Content-Type: application/json\r\nAccept: application/json\r\n',
'content' => json_encode($data)
));
$context = stream_context_create($opts);
$result_json = file_get_contents('http://...', FALSE, $context);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment