<?php
class UploadPart
{
protected static $url;
protected static $delimiter;
protected static $instance;
public function __construct() {
$token = '21_MMnF5YHlZnuNVwWtBO8DVnt4CSwOIqWwLG6aIyjfS6f7-G9g49bo0hjiblqV8GyzPkAUugyKuvCBnHiI1kE5lwhEnLjwXWkKCy1V--2oqWkAjfRmPN_PLhXKMp4HFSeCFAPNA';
static::$url = "https://api.weixin.qq.com/card/invoice/platform/setpdf?access_token={$token}";
static::$delimiter = uniqid();
}
public function putPart($param) {
$post_data = static::buildData($param);
$curl = curl_init(static::$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Content-Type: multipart/form-data; boundary=" . static::$delimiter,
"Content-Length: " . strlen($post_data)
]);
$response = curl_exec($curl);
curl_close($curl);
$info = json_decode($response, true);
var_dump($info);
return $response;
}
private static function buildData($param){
$data = '';
$eol = "\r\n";
$upload = $param['upload'];
unset($param['upload']);
foreach ($param as $name => $content) {
$data .= "--" . static::$delimiter . "\r\n"
. 'Content-Disposition: form-data; name="' . $name . "\"\r\n\r\n"
. $content . "\r\n";
}
// 拼接文件流
$data .= "--" . static::$delimiter . $eol
. 'Content-Disposition: form-data; name="pdf"; filename="' . $param['filename'] . '"' . "\r\n"
. 'Content-Type:application/octet-stream'."\r\n\r\n";
$data .= $upload . "\r\n";
$data .= "--" . static::$delimiter . "--\r\n";
return $data;
}
public static function getInstance() {
if(!static::$instance){
static::$instance = new static();
}
return static::$instance;
}
}
$fields = array(
'type' => 'pdf',
'filename' => 'kgkxinvoice20190510101146_144000930000_10673744.pdf',
'filesize' => '',
'offset' => 0,
'filetype' => '.pdf',
'originName' => 'kgkxinvoice20190510101146_144000930000_10673744.pdf',
'upload'=>file_get_contents('http://www.aisinogz.com:19876/AisinoFp-test/pd?id=kgkxinvoice20190510101146_144000930000_10673744.pdf')
);
$part = UploadPart::getInstance()->putPart($fields);