Upload an image in wordpress using REST API

Using rest api i can create a post, get blog category etc. but i cannot upload an image i am referring
https://github.com/WP-API/client-php/blob/master/library/WPAPI/Media.php
and

http://wp-api.org/#entities_media-meta_width

Read More

and my code is

$data = array('file'=>$filePath,'is_image'=>true);
print_r($data);

$headers = array('Content-Type' => 'application/octet-stream');
$response = $this->api->post(WPAPI::ROUTE_MEDIA, $headers, $data);

they talk about [$data in $response = $this->api->post(WPAPI::ROUTE_MEDIA, $headers, $data);]
what are the key value pairs used in $data?

Related posts

2 comments

  1. Finally, I fixed it myself:

    1. specify the file path
    2. put necessary headers
    3. send post request

    Here is the code:

    $filePath = 'URL of image'; //right click your fav img and copy url.
    $imageData = @file_get_contents($filePath); //get image content
    $headers = array('Content-Type' => 'application/json; charset= UTF-8',
           'Content-Disposition' => 'attachment; filename='.basename($filepath)');
    $response = $this->api->post('/wp-json/media', $headers, $imageData);
    print_r($response);
    

    Check the WordPress Media Gallery.

    Enjoy!

  2. try this.

    1 open chrome browser.
    2 install apps (https://chrome.google.com/webstore/search/rest%20client?utm_source=chrome-ntp-icon)
    3 open this apps and use
    

    Screenshort

    enter image description here

Comments are closed.