PHP code example of consatan / weibo_image_uploader

1. Go to this page and download the library: Download consatan/weibo_image_uploader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

consatan / weibo_image_uploader example snippets



// 引入 composer autoload
eUploader\Client();

// 默认返回的是 https 协议的图床 URL,调用该方法返回的是 http 协议的图床 URL
// $weibo->useHttps(false);

// 上传示例图片
$url = $weibo->upload('./example.jpg', '微博帐号', '微博帐号密码');

// 输出新浪图床 URL
echo $url . PHP_EOL;



// 文件路径
$url1 = $weibo->upload('./example.jpg', '微博帐号1', '密码');
// 同一用户名只有第一次上传需要登入,之后使用缓存的登入 cookie 进行上传
// 如果使用 cookie 上传失败,将尝试重新登入一次,还是失败的话抛出异常
// 除非使用的是无法持久化保存的缓存适配器(如 ArrayAdapter)
// 否则以后同一用户名都将使用缓存的 cookie 进行登入
// echo $weibo->upload('./example.jpg', '微博帐号1', '密码');

// resource
$url2 = $weibo->upload(fopen('./example.jpg', 'r'), '微博帐号2', '密码', [
    'proxy' => 'http://192.168.1.100:8080'
]);

// 字符串
$url3 = $weibo->upload(file_get_contents('./example.jpg'), '微博帐号3', '密码', [
    'proxy' => 'http://192.168.1.200:8090'
]);

// \Psr\Http\Message\StreamInterface
$url4 = $weibo->upload(\GuzzleHttp\Psr7\stream_for(file_get_contents('./example.jpg')), '微博帐号4', '密码', [
    'proxy' => 'http://192.168.1.250:9080'
]);

// 开启水印
$url = $weibo->upload('./example.jpg', '微博帐号', '密码', ['mark' => true]);

// 水印位置
$url = $weibo->upload('./example.jpg', '微博帐号', '密码', [
    'mark' => true,
    'markpos' => Consatan\Weibo\ImageUploader\Client::MARKPOS_BOTTOM_CENTER,
]);

// 可使用任意的水印暱称(也许以后的版本就会和谐了)
$url = $weibo->upload('./example.jpg', '微博帐号', '密码', ['mark' => true, 'nickname' => '任意暱称']);

// 默认使用 large (原始)尺寸,此处使用 thumbnail (缩略图) 尺寸
$url = $weibo->upload('./example.jpg', '微博帐号', '密码', [
    'size' => Consatan\Weibo\ImageUploader\Client::IMAGE_SIZE_THUMBNAIL
]);

// 获取多个尺寸的图片链接
$urls = $weibo->upload('./example.jpg', '微博帐号', '密码', ['size' => [
    Consatan\Weibo\ImageUploader\Client::IMAGE_SIZE_SMALL,
    Consatan\Weibo\ImageUploader\Client::IMAGE_SIZE_LARGE,
    Consatan\Weibo\ImageUploader\Client::IMAGE_SIZE_THUMBNAIL,
]]);
// 返回
// array (
//   'small' => 'https://ws2.sinaimg.cn/small/0068M0xKgy1fetd4l7x6vj30bo0bximx.jpg',
//   'large' => 'https://ws2.sinaimg.cn/large/0068M0xKgy1fetd4l7x6vj30bo0bximx.jpg',
//   'thumbnail' => 'https://ws2.sinaimg.cn/thumbnail/0068M0xKgy1fetd4l7x6vj30bo0bximx.jpg',
// )

// 以下 2 种传参顺序最终效果是一致的
$url = $weibo->upload('./example.jpg', '微博帐号', '密码', [
    'size' => Consatan\Weibo\ImageUploader\Client::IMAGE_SIZE_THUMBNAIL
], [
    'proxy' => 'http://192.168.1.250:9080'
]);

$url = $weibo->upload('./example.jpg', '微博帐号', '密码', [
    'proxy' => 'http://192.168.1.250:9080'
], [
    'size' => Consatan\Weibo\ImageUploader\Client::IMAGE_SIZE_THUMBNAIL
]);