1

我正在尝试将图片发布到我的博客。是的,我可以上传图片,我可以在帖子的内容中看到这些图片,但它不在附件列表中。

如何将我上传的图片放入附件列表中?

当我将 MS word 2010 发布到我的博客时,我放在 word 上的图像总是在附件列表中更新。但我的 php 源代码没有。

有没有办法将文件附加到博客文章中?


$cBlog = new blog;
$title = "This is a article's title";
$desc = "IT will be the content";
$return = $cBlog->writePost($title, $desc);

class blog
{       
    public $g_blog_url;
    public $user_id;
    public $blogid;
    public $password;
    public $publish;

    function __construct()
    {           
        $this->g_blog_url = "https://api.blog.naver.com/xmlrpc";
        $this->user_id = "globeseen";
        $this->blogid = "globeseen";
        $this->password = "password";
        $this->publish = true; 
    }

    function writePost($title, $description, $category="") 
    { 
        $client = new xmlrpc_client($this->g_blog_url); 
        $client->setSSLVerifyPeer(false); 
        $GLOBALS['xmlrpc_internalencoding']='UTF-8';


        $img = $this->upload_image("D:\\1.jpg");

        $struct = array(
        'title' => new xmlrpcval($title, "string"), 
        'description' => new xmlrpcval($description."<img src=$img>", "string"),
        'categories' => new xmlrpcval($category, "string"),
        'tags' => new xmlrpcval('clothing', "string")
        );

        $f = new xmlrpcmsg("metaWeblog.newPost", 
        array( 
        new xmlrpcval($this->blogid, "string"),
        new xmlrpcval($this->user_id, "string"),
        new xmlrpcval($this->password, "string"),
        new xmlrpcval($struct , "struct"), 
        new xmlrpcval($this->publish, "boolean")
             ));

        $f->request_charset_encoding = 'UTF-8';
        return $response = $client->send($f);
    }

    function upload_image($fpath) {
  global $api_url, $blog_user, $blog_passwd;

  $api_url = $this->g_blog_url;
  $blog_user = $this->user_id;
  $blog_passwd = $this->password;

  $imgbit = file_get_contents($fpath, FILE_BINARY);
      $img = new xmlrpcval(
       array (
        'bits' => new xmlrpcval($imgbit, 'base64'),
        'type' => new xmlrpcval('image/jpeg', 'string'),
        'name' => new xmlrpcval(basename($fpath), 'string')
       ), 'struct');

      $c = new xmlrpc_client($api_url);

      $c->setSSLVerifyPeer(false);
      $x = new xmlrpcmsg("metaWeblog.newMediaObject");
      $x->addParam(new xmlrpcval($blog_user, 'string'));
      $x->addParam(new xmlrpcval($blog_user, 'string'));
      $x->addParam(new xmlrpcval($blog_passwd, 'string'));
      $x->addParam($img);

      $c->return_type = 'phpvals';
      $r =$c->send($x);
      if ($r->errno=="0") {
          return $r->val['url'];
      } else {
          echo "There was an error<pre>";
          print_r($r);
          echo "</pre>";
          return null;
      }
     } 


}
4

0 回答 0