Wordpress Rest Api V2,如何添加新媒体


Wordpress Rest Api V2 ,How to add a new Media

我在使用新的WordPress REST Api时遇到了一些问题。我无法使用以下示例上传任何图像。我通过了Oauth 2的认证,我可以创建帖子、类别,一切都很好。

我的代码是:

$site = Sites::findOrFail($id);
        $token=$this->token($site->url,$site->clientId,$site->clientSecret);
        $token=$token->getToken();
        $site_url = $site->url."/wp-json/wp/v2/media?access_token=".$token;

            $options  = array (
              'http' => 
              array (
                'ignore_errors' => true,
                'method' => 'POST',
                'header' => 
                array (
                  1 => 'Content-Type: application/x-www-form-urlencoded',
                  2 =>'Content-Disposition: attachment; filename="codeispoetry-rgb.png"',
                ),
                'content' => 
                 http_build_query(  array (
                    'media_urls' => 'https://s.w.org/about/images/logos/codeispoetry-rgb.png',
                  )),
              ),
            );
            $context  = stream_context_create( $options );
            $response = file_get_contents(
                $site_url,
                false,
                $context
            );
            $response = json_decode( $response );
            echo "<pre>";
            print_r($response);
            echo "</pre>";

post方法可以,没有任何错误,响应如下:

stdClass Object

([id]=>1842【日期】=>2016-05-18T15:08:57〔date_gmt〕=>2016-05-18T15:08:57[guid]=>stdClass对象([rendered]=>wordpres.domain/wp-content/uploads/2016/05/codeispoetry-rgb-1.png[raw]=>wordpres.domain/wp-content/uploads/2016/05/codeispoetry-rgb-1.png)

[modified] => 2016-05-18T15:08:57
[modified_gmt] => 2016-05-18T15:08:57
[password] => 
[slug] => codeispoetry-rgb-1
[status] => inherit
[type] => attachment
[link] => wordpres.domain/codeispoetry-rgb-1/
[title] => stdClass Object
    (
        [raw] => codeispoetry-rgb-1
        [rendered] => codeispoetry-rgb-1
    )
[author] => 1
[comment_status] => closed
[ping_status] => closed
[alt_text] => 
[caption] => 
[description] => 
[media_type] => image
[mime_type] => image/png
[media_details] => stdClass Object
    (
    )
[post] => 
[source_url] => wordpres.domain/wp-content/uploads/2016/05/codeispoetry-rgb-1.png
[_links] => stdClass Object
    (
        [self] => Array
            (
                [0] => stdClass Object
                    (
                        [href] => wordpres.domain/wp-json/wp/v2/media/1842
                    )
            )
        [collection] => Array
            (
                [0] => stdClass Object
                    (
                        [href] => wordpres.domain/wp-json/wp/v2/media
                    )
            )
        [about] => Array
            (
                [0] => stdClass Object
                    (
                        [href] => wordpres.domain/wp-json/wp/v2/types/attachment
                    )
            )
        [author] => Array
            (
                [0] => stdClass Object
                    (
                        [embeddable] => 1
                        [href] => wordpres.domain/wp-json/wp/v2/users/1
                    )
            )
        [replies] => Array
            (
                [0] => stdClass Object
                    (
                        [embeddable] => 1
                        [href] => wordpres.domain/wp-json/wp/v2/comments?post=1842
                    )
            )
    )

)

但是在WordPress媒体中:上传了一张添加了名称的黑色图像:内容处置:附件;filename="codeispoetry rgb.png"像这样:http://prntscr.com/b5juch

有人能帮助在v2 REST API中发布媒体的测试php示例吗?谢谢大家。

大家好,我解决了这个问题:

                 $name =$file->getClientOriginalName();
                $thumbnailUrl=url('/').$assetPath . '/' .$name;
                //start api image upload
                   $site_url = $site->url."/wp-json/wp/v2/media?access_token=".$token;
                    // die();
                    $img=file_get_contents($thumbnailUrl);
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL,            $site_url );
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
                    curl_setopt($ch, CURLOPT_POST,           1 );
                    curl_setopt($ch, CURLOPT_POSTFIELDS,     $img); 
                    curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Disposition: attachment; filename="'.$name.'"')); 
                    $result=curl_exec ($ch);

感谢大家