how can I pass POST data from a local box to my website?
I've tried the following code snippets but they do not seem to work:
(a page on my freehostia site would go where
www.url.com is)
Code:
<?php
do_post_request('http://www.url.com','post data');
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'post',
'content' => $data
));
if ($optional_headers!== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
?>
Code:
<?php
$data = "Here is my POST data";
$url = "http://www.url.com";
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=>"User-Agent: My Own Http Client\r\n".
"Content-length: " . strlen($data)."\r\n",
'content' => $data
)
);
$context = stream_context_create($opts);
$fp = fopen($url, 'r', false, $context);
fgets($fp);
?>