新手,弄了个 wordpress 插件,方便 wordpress 使用,功能只支持 xmlrpc 上传 。欢迎随意修改
<pre>
<?php
/**
* @package SM.MS UPLOAD IMG
* @version 0.0.1
/
/
Plugin Name: SM.MS UPLOAD IMG
Plugin URI: http://blog.firetry.com
Description: This is a plugin for SM.MS.
Author: icoder
Version: 0.0.1
Author URI: http://blog.firetry.com
*/
//hook 所有 xmlrpc 的上传
add_filter ( 'xmlrpc_methods', 'xml_to_SMMS' );
function xml_to_SMMS ($methods ) {
$methods['wp.uploadFile'] = 'xmlrpc_upload';
$methods['metaWeblog.newMediaObject'] = 'xmlrpc_upload';
return $methods;
}
function xmlrpc_upload ($args ){
$data = $args[3];
$name = sanitize_file_name ( $data['name'] );
$type = $data['type'];
$bits = $data['bits'];
$target_url = "https://sm.ms/index.php?act=upload";
$data = array ();
$mimeBoundary = uniqid ();
array_push ($data, "--".$mimeBoundary );
$mimeType = empty ($type ) ? 'application/octet-stream' : $type;
array_push ($data, "Content-Disposition: form-data; name=\"smfile\"; filename=\"$name\"");
array_push ($data, "Content-Type: $mimeType");
array_push ($data, '');
array_push ($data, $bits );
array_push ($data, '');
array_push ($data, "--".$mimeBoundary."--");
$post_data = implode ("\r\n", $data );
$length = strlen ($post_data );
$headers =array ();
array_push ($headers, "Content-Type: multipart/form-data; boundary=$mimeBoundary");
array_push ($headers, "Content-Length: {$length}");
array_push ($headers, "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0 ) Gecko/20100101 Firefox/40.0");
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, $target_url );
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ($ch, CURLOPT_POST, 1 );
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_data );
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false );
$result = curl_exec ($ch );
if (!empty (curl_error ($ch ))){
echo "CURL error";
var_dump (curl_error ($ch ));
}
curl_close ($ch );
$r = json_decode ($result );
return array ( 'file' => $r->url , 'url' => $r->url , 'type' => $type );
}
</pre>
1
Showfom 2015-10-07 12:47:33 +08:00
感谢,我们也有计划等空下来的时候弄个专门的 API 方便第三方开发
|