当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 52

Ceifei / YurunHttp
暂停

forked from 宇润 / YurunHttp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
HttpRequestMultipartBody.php 3.05 KB
一键复制 编辑 原始数据 按行查看 历史
<?php
/**
* Created by PhpStorm.
* User: xiaozhuai
* Date: 17/4/23
* Time: 下午2:14
*/
namespace Yurun\Until;
class HttpRequestMultipartBody
{
/**
* 类型,键值对
*/
const TYPE_KV = 0;
/**
* 类型,文件
*/
const TYPE_FILE = 1;
/**
* @var array 列表
*/
private $list = array();
/**
* @var string 边界字符串
*/
private $boundary;
/**
* 添加键值对
* @param $key string
* @param $value string
*/
public function add($key, $value)
{
$this->list[] = array(
'type' => static::TYPE_KV,
'key' => $key,
'value' => $value,
);
}
/**
* 添加文件
* @param $key string
* @param $file string 文件路径
* @param $file_name string 文件名
*/
public function addFile($key, $file, $file_name)
{
$this->list[] = array(
'type' => static::TYPE_FILE,
'key' => $key,
'file' => $file,
'file_name' => $file_name
);
}
public function remove($key)
{
$count = count($this->list);
for($i = 0; $i < $count; $i++)
{
if($this->list[$i]['key'] === $key)
{
array_splice($this->list, $i, 1);
}
}
}
public function clear()
{
$this->list = array();
}
/**
* @return string 最终构建的body内容
*/
public function content()
{
$this->generateBoundary();
$content = '';
foreach ($this->list as $item)
{
switch ($item['type'])
{
case static::TYPE_KV :
default :
$content .= sprintf("--%s\r\n", $this->boundary);
$content .= sprintf("Content-Disposition: form-data; name=\"%s\"\r\n\r\n", $item["key"]);
$content .= $item["value"] . "\r\n";
break;
case static::TYPE_FILE :
$content .= sprintf("--%s\r\n", $this->boundary);
$content .= sprintf("Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n", $item["key"], $item["file_name"]);
$content .= sprintf("Content-Type: application/octet-stream\r\n\r\n");
$content .= file_get_contents($item['file']) . "\r\n";
break;
}
}
$content .= sprintf("--%s--\r\n\r\n", $this->boundary);
return $content;
}
/**
* 随机生成一个新的boundary
*/
private function generateBoundary()
{
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randStr = '';
$max = strlen($chars) - 1;
for ($i = 0; $i < 64; $i++)
{
$randStr .= $chars[ mt_rand(0, $max) ];
}
$this->boundary = '__BOUNDARY__' . $randStr . '__BOUNDARY__';
}
public function getBoundary()
{
return $this->boundary;
}
}
PHP
1
https://gitee.com/hubupas/YurunHttp.git
git@gitee.com:hubupas/YurunHttp.git
hubupas
YurunHttp
YurunHttp
master

搜索帮助