简体   繁体   中英

Wordpress Rest Api V2 ,How to add a new Media

I'm having some problems with the new 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.

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:

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 )

[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; filename="codeispoetry-rgb.png" like this : http://prntscr.com/b5juch

Can someone help with a tested php example of posting media in v2 REST API? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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