note 119350 added to function.curl-setopt
| From: | tceburashka at yandex dot ru | Date: | Thu, 19 May 2016 02:41:21 +0000 |
| Subject: | note 119350 added to function.curl-setopt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205511@lists.php.net to get a copy of this message | ||
Send body file use RFC7578 ("multipart/form-data")
// class contain file
class oFile
{
private $name;
private $mime;
private $content;
public function __construct($name, $mime=null, $content=null)
{
if(is_null($content))
{
$info = pathinfo($name);
// check is exist and readable file
if(!empty($info['basename']) && is_readable($name))
{
$this->name = $info['basename'];
// get MIME
$this->mime = mime_content_type($name);
// load file
$content = file_get_contents($name);
if($content!==false) $this->content = $content;
else throw new Exception('Don`t get content -
"'.$name.'"');
} else throw new Exception('Error param');
} else
{
$this->name = $name;
if(is_null($mime)) $mime = mime_content_type($name);
$this->mime = $mime;
$this->content = $content;
};
}
public function Name() { return $this->name; }
public function Mime() { return $this->mime; }
public function Content() { return $this->content; }
};
// create body for POST request
class BodyPost
{
// part "multipart/form-data"
public static function PartPost($name, $val)
{
$body = 'Content-Disposition: form-data; name="' . $name .
'"';
// check instance of oFile
if($val instanceof oFile)
{
$file = $val->Name();
$mime = $val->Mime();
$cont = $val->Content();
$body .= '; filename="' . $file . '"' .
"\r\n";
$body .= 'Content-Type: ' . $mime ."\r\n\r\n";
$body .= $cont."\r\n";
} else $body .= "\r\n\r\n".urlencode($val)."\r\n";
return $body;
}
public static function Get(array $post, $delimiter='-------------0123456789')
{
if(is_array($post) && !empty($post))
{
$bool = false;
foreach($post as $val) if($val instanceof oFile) {$bool = true; break; };
if($bool)
{
$ret = '';
foreach($post as $name=>$val)
$ret .= '--' . $delimiter. "\r\n". self::PartPost($name,
$val);
$ret .= "--" . $delimiter . "--\r\n";
} else $ret = http_build_query($post);
} else throw new \Exception('Error input param!');
return $ret;
}
};
$delimiter = '-------------'.uniqid();
$file = new oFile('sample.txt', 'text/plain', 'Content file');
$post = BodyPost::Get(array('field'=>'text', 'file'=>$file),
$delimiter);
var_dump($post);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://server/upload/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; boundary='
. $delimiter,
'Content-Length: ' . strlen($post)));
curl_exec($ch);
----
Server IP: 72.52.91.14
Probable Submitter: 176.112.238.63
----
Manual Page -- http://php.net/manual/en/function.curl-setopt.php
Edit -- https://master.php.net/note/edit/119350
Del: integrated -- https://master.php.net/note/delete/119350/integrated
Del: useless -- https://master.php.net/note/delete/119350/useless
Del: bad code -- https://master.php.net/note/delete/119350/bad+code
Del: spam -- https://master.php.net/note/delete/119350/spam
Del: non-english -- https://master.php.net/note/delete/119350/non-english
Del: in docs -- https://master.php.net/note/delete/119350/in+docs
Del: other reasons-- https://master.php.net/note/delete/119350
Reject -- https://master.php.net/note/reject/119350
Search -- https://master.php.net/manage/user-notes.php