代码拉取完成,页面将自动刷新
同步操作将从 宇润/YurunHttp 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<?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;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。