I have to make a request post using PHP, but I can not make it work. the low html request works
<form action="http://example.com/form.php" method="post" id="form-success">
<input type="hidden" name="id" id="itemid" value="12345">
<input type="hidden" name="key" id="sh" value="79c830e5bf78218685a350cd5df3cdac">
<input type="submit"/>
</form>
I tried this two ways to request in php but both does not work.
$content = http_build_query(array('id'=>'12345','key'=>'79c830e5bf78218685a350cd5df3cdac'));
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'content' => $content,
)
));
$result = file_get_contents('http://example.com/form.php', null, $context);
echo $result;
and
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/form.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('id'=>'12345','key'=>'79c830e5bf78218685a350cd5df3cdac');
$pagina = curl_exec ($ch);
echo $pagina;
has no error message, just know that the page is not redirected correctly. any ideas? thank you
I use this :
function file_get_contents_post($url, $var_post = array()) {
define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));
$header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;
$content = '' ;
foreach ( $var_post as $key => $value ) {
$content .= "--".MULTIPART_BOUNDARY."
".
"Content-Disposition: form-data; name=\"".$key."\"
".
$value."
";
}
$content .= "--".MULTIPART_BOUNDARY."--
";
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $header,
'content' => $content,
)
));
return file_get_contents($url, false, $context) ;
}