note 90411 added to function.stream-context-create
| From: | dresel at gmx dot at | Date: | Tue, 21 Apr 2009 09:23:06 +0000 |
| Subject: | note 90411 added to function.stream-context-create | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-153535@lists.php.net to get a copy of this message | ||
I use this script to send normal data and images (you may have to change Content-Type to send other
data), works fine for me :)
function do_post_request($url, $postdata, $files = null)
{
$data = "";
$boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10);
//Collect Postdata
foreach($postdata as $key => $val)
{
$data .= "--$boundary\n";
$data .= "Content-Disposition: form-data;
name=\"".$key."\"\n\n".$val."\n";
}
$data .= "--$boundary\n";
//Collect Filedata
foreach($files as $key => $file)
{
$fileContents = file_get_contents($file['tmp_name']);
$data .= "Content-Disposition: form-data; name=\"{$key}\";
filename=\"{$file['name']}\"\n";
$data .= "Content-Type: image/jpeg\n";
$data .= "Content-Transfer-Encoding: binary\n\n";
$data .= $fileContents."\n";
$data .= "--$boundary--\n";
}
$params = array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: multipart/form-data;
boundary='.$boundary,
'content' => $data
));
$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;
}
//set data (in this example from post)
//sample data
$postdata = array(
'name' => $_POST['name'],
'age' => $_POST['age'],
'sex' => $_POST['sex']
);
//sample image
$files['image'] = $_FILES['image'];
do_post_request("http://whatever.com", $postdata,
$files);
----
Server IP: 208.69.120.35
Probable Submitter: 81.217.114.241
----
Manual Page -- http://www.php.net/manual/en/function.stream-context-create.php
Edit -- https://master.php.net/note/edit/90411
Del: integrated -- https://master.php.net/note/delete/90411/integrated
Del: useless -- https://master.php.net/note/delete/90411/useless
Del: bad code -- https://master.php.net/note/delete/90411/bad+code
Del: spam -- https://master.php.net/note/delete/90411/spam
Del: non-english -- https://master.php.net/note/delete/90411/non-english
Del: in docs -- https://master.php.net/note/delete/90411/in+docs
Del: other reasons-- https://master.php.net/note/delete/90411
Reject -- https://master.php.net/note/reject/90411
Search -- https://master.php.net/manage/user-notes.php