I have a php script that needs to download a file over http/https and specify POST parameters to a request.
There should be no browser popups, just silent download, for example to ~/. Unfortunately, wrapping wget
is not an allowed solution.
Is there any easy way to do this ?
You can use:
file_get_contents() function — IMO the easiest way to get (or POST) simple content over HTTP (or HTTPS). Example of usage:
<?php
$opts = array('http' => array(
'method' => 'POST',
'content' => $body, // your x-www-form-urlencoded POST payload
'timeout' => 60,
));
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context, -1, 40000);
CURL — another easy way to send POST request. The very basic code example:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
// $body is your x-www-form-urlencoded POST payload
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);
Any other PHP HTTP client (Zend_Http_Client, HTTP_Client, Whatever_Client) which you have or which you can download.