sm.ms wordpress 插件

2015-09-06 15:57:54 +08:00
 icoder
新手,弄了个 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>
2299 次点击
所在节点    分享发现
1 条回复
Showfom
2015-10-07 12:47:33 +08:00
感谢,我们也有计划等空下来的时候弄个专门的 API 方便第三方开发

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/218632

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX