PHP 5: Send HTTP post request with file_get_contents


The following code snippet shows an easy way how to send a HTTP post request using the PHP function file_get_contents:

	$url = 'http://server.com/path';
	$data = array('key1' => 'value1', 'key2' => 'value2')

	// use key 'http' even if you send the request to https://...
	$options = array('http' => array(
		'method'  => 'POST',
		'content' => http_build_query($data)
	));
	$context  = stream_context_create($options);
	$result = file_get_contents($url, false, $context);

	var_dump($result);
,

One response to “PHP 5: Send HTTP post request with file_get_contents”

Leave a Reply

Your email address will not be published. Required fields are marked *