最近对接百度营销的信息流接口,上传视频那块,我们使用的是自己的视频库,对应表里存的都是地址,所以需要转存到百度,后端需要用curl请求接口,故此拼接
//推送文件流
public static function putPart($url, $param){
$delimiter = uniqid();
$imageInfo = pathinfo($param['file']);
//编译请求头格式和数据流
$data = '';
$eol = PHP_EOL;
$upload = file_get_contents($param['file']);
$param['filename'] = $imageInfo['basename'];
unset($param['file']);
foreach ($param as $name => $content) {
$data .= "--" . $delimiter . PHP_EOL . 'Content-Disposition: form-data; name="' . $name . PHP_EOL . PHP_EOL . $content . PHP_EOL;
}
$data .= "--" . $delimiter . $eol . 'Content-Disposition: form-data; name="file"; filename="' . $param['filename'] . '"' . PHP_EOL . 'Content-Type:application/octet-stream'. PHP_EOL . PHP_EOL;
$data .= $upload . PHP_EOL;
$data .= "--" . $delimiter . "--" . PHP_EOL;
$post_data = $data;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type: multipart/form-data; boundary=" . $delimiter, "Content-Length: " . strlen($post_data)]);
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
}