Forked from s-hiroshi/WP API v2 image upload from media endpoint
Created
March 5, 2020 08:34
-
-
Save phamngsinh/4f2bbe1c1de0880ea278b72d45779dd8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * WP REST API version 2.0-beta7 | |
| * API base url ishttp://www.example.com/wp-json | |
| * | |
| * Reference | |
| * https://wordpress.org/support/topic/new-post-with-image | |
| */ | |
| /* | |
| * Get Guzzle HTTP Client. That client has been authenticated. | |
| */ | |
| $client = ... | |
| /* | |
| * Get binary data of image. | |
| * $path is file path to be uploaded. | |
| */ | |
| $handle = fopen($path, 'r'); | |
| $fdata = fread($handle, filesize($path)); | |
| /* | |
| * Post media. | |
| * Request to WP REST API media endpoint | |
| */ | |
| $response = $client->request( | |
| 'POST', | |
| 'wp/v2/media', | |
| [ | |
| 'multipart' => [ | |
| [ | |
| 'name' => 'file', | |
| 'contents' => $fdata, | |
| 'filename' => basename($path), | |
| ], | |
| ], | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment