wordpress遠端上傳圖片
使用API遠端上傳圖片;
include("xmlrpc.inc");
$c = new xmlrpc_client("/xmlrpc.php", "blog.zaodianshuo.com", 80);
$struct = new xmlrpcval (
array (
"name" => new xmlrpcval ( '1234.jpg','string'),
"type" => new xmlrpcval ( 'image/jpeg','string'),
"bits" => new xmlrpcval ( $_POST['pic'],'base64') // $_POST['pic']得到的值是把圖片編碼過的
), "struct"
);
$msg = new xmlrpcmsg("metaWeblog.newMediaObject");
$msg ->addParam(new xmlrpcval(1,"string"));
$msg ->addParam(new xmlrpcval("userName","string"));
$msg ->addParam(new xmlrpcval("pass","string"));
$msg ->addParam($struct);
$msg ->addParam(new xmlrpcval("1","string"));
$r =$c->send($msg);
var_dump (php_xmlrpc_decode($r));
忽略下面的文字。
中途遇到一點問題,上傳上去的圖片無法顯示,下載到本地,轉成Base64之後的值跟上傳之前圖片編碼後的值再編碼一次的值一樣,
找到wp-includes\functions.php 1720行左右,方法wp_upload_bits裡,把@fwrite( $ifp, $bits );改成@fwrite( $ifp, base64_decode($bits) );這樣肯定是不行的,上傳其他型別媒體檔案也會用到這個方法,待詳細研究。