简体   繁体   English

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

[英]Wordpress Rest Api V2 ,How to add a new Media

I'm having some problems with the new WordPress REST Api. 我在使用新的WordPress REST Api时遇到一些问题。 I can't upload any image using the below example. 我无法使用以下示例上传任何图像。 I'm authenticated by Oauth 2, I can create posts, categories, everything is all right. 我已通过Oauth 2的身份验证,可以创建帖子,类别,一切都很好。

My code is: 我的代码是:

$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>";

The post method is ok without any error, the response is like this: post方法可以,没有任何错误,响应是这样的:

stdClass Object

( [id] => 1842 [date] => 2016-05-18T15:08:57 [date_gmt] => 2016-05-18T15:08:57 [guid] => stdClass Object ( [rendered] => wordpres.domain/wp-content/uploads/2016/05/codeispoetry-rgb-1.png [raw] => wordpres.domain/wp-content/uploads/2016/05/codeispoetry-rgb-1.png ) ([id] => 1842 [date] => 2016-05-18T15:08:57 [date_gmt] => 2016-05-18T15:08:57 [guid] => stdClass对象([呈现] => wordpres。 domain / wp-content / uploads / 2016/05 / codeispoetry-rgb-1.png [原始] => 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
                    )

            )

    )

)

But in the WordPress media: There is uploaded a black image with the name added into: Content-Disposition: attachment; 但是在WordPress媒体中:上传了一个黑色图片,其名称已添加到其中:Content-Disposition:附件; filename="codeispoetry-rgb.png" like this : http://prntscr.com/b5juch filename =“ codeispoetry-rgb.png”就像这样: http ://prntscr.com/b5juch

Can someone help with a tested php example of posting media in v2 REST API? 有人可以通过v2 REST API中发布媒体的经过测试的php示例提供帮助吗? Thanks everyone. 感谢大家。

hello Everyone i solved this : 大家好,我解决了这个问题:

                 $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);

Thanks everyone 感谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM