2

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

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?

1
  • This really needs examples of what you've tried, where you've looked for answers and a title that wasn't written in all caps. Commented Jul 1, 2015 at 10:52

2 Answers 2

4

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

You use chrome browers for checking API
you use most post method for upload images
can you pls send the php code to do this.not this one.
thanks for your great effort .i am actually using github.com/WP-API/client-php/blob/master/library/WPAPI/… can you help in this way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.