V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
icoder
V2EX  ›  分享发现

sm.ms wordpress 插件

  •  
  •   icoder · 2015-09-06 15:57:54 +08:00 · 2259 次点击
    这是一个创建于 3348 天前的主题,其中的信息可能已经有所发展或是发生改变。

    新手,弄了个 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 条回复    2015-10-07 12:47:33 +08:00
    Showfom
        1
    Showfom  
       2015-10-07 12:47:33 +08:00
    感谢,我们也有计划等空下来的时候弄个专门的 API 方便第三方开发
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5585 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 03:20 · PVG 11:20 · LAX 19:20 · JFK 22:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.