Commit 728e78dc by hanccc

还没改完上传问题。。。

1 parent a39fe166
......@@ -20,7 +20,7 @@ $robot = new Robot([
$robot->server->setMessageHandler(function($message){
/** @var $message Message */
if($message->content === '测试图片'){
Image::send($message->username, realpath(__DIR__ . '/./../tmp/jpg/1483791274935140574833210401.jpg'));
Image::send($message->username, realpath(__DIR__ . '/./../tmp/jpg/843242266304844229.jpg'));
}
});
......
......@@ -18,8 +18,6 @@ class Http
protected $client;
public $cookies;
/**
* @return Http
*/
......
......@@ -18,6 +18,8 @@ namespace Hanson\Robot\Message;
trait UploadAble
{
static $file;
/**
* @param $username
* @param $file
......@@ -27,15 +29,15 @@ trait UploadAble
{
$url = 'https://file.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json';
static::$mediaCount = ++static::$mediaCount;
static::$file = $file;
$lastModifyDate = gmdate('D M d Y H:i:s TO', filemtime($file));
list($mime, $mediaType) = static::getMediaType($file);
$data = [
'id' => 'WU_FILE_' .static::$mediaCount,
'name' => $file,
'name' => basename($file),
'type' => $mime,
'lastModifieDate' => $lastModifyDate,
'lastModifieDate' => date('D M m Y H:i:s').' GMT+0800 (CST)',
'size' => filesize($file),
'mediatype' => $mediaType,
'uploadmediarequest' => json_encode([
......@@ -51,12 +53,21 @@ trait UploadAble
// 'FileMd5' => md5_file($file)
], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
'webwx_data_ticket' => static::getTicket(),
'pass_ticket' => server()->passTicket,
'filename' => '@'.$file
'pass_ticket' => urldecode(server()->passTicket),
// 'filename' => "@{$file};filename=".basename($file),
// 'filename' => file_get_contents($file),
'filename' => fopen($file, 'r'),
// 'filename' => $file
// 'filename' => curl_file_create($file, $mime, basename($file))
];
$data = static::dataToMultipart($data);
$result = http()->post($url, $data, true);
// $result = http()->request($url, 'post', [
// 'multipart' => $data
// ]);
print_r($data);
print_r($result);
......@@ -84,4 +95,21 @@ trait UploadAble
return $cookies[$key]['Value'];
}
private function dataToMultipart($data)
{
$result = [];
foreach ($data as $key => $item) {
$field = [
'name' => $key,
'contents' => $item
];
if($key === 'filename') $field['filename'] = basename(static::$file);
$result[] = $field;
}
return $result;
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!