Last active
November 24, 2015 21:25
-
-
Save s-hiroshi/b41efdfc551af5d3989a to your computer and use it in GitHub Desktop.
This file contains 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 1.2.4 | |
* 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. | |
*/ | |
$finfo = new finfo(FILEINFO_MIME_TYPE); | |
$fmime = $finfo->file($path); | |
$handle = fopen($path, 'r'); | |
$fdata = fread($handle, filesize($path)); | |
/* | |
* Post media. | |
* Request to WP REST API media endpoint | |
*/ | |
$headers = [ | |
'Content-Type' => $fmime, | |
'Content-Disposition' => 'attachment; filename='.basename($path), | |
'Content-Transfer-Encoding' => 'binary', | |
]; | |
$response = $client->request( | |
'POST', | |
'/media', | |
[ | |
'headers' => $headers, | |
'body' => $fdata, | |
] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment