WP REST API v2で画像投稿
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
<?php | |
require_once( dirname( __FILE__ ) . '/wp-load.php' ); | |
// APIエンドポイントのURL | |
$url = 'http://sample.com/wp-json/wp/v2/media'; | |
// ユーザー名(Application Passwordsプラグインでパスワードを発行したユーザーのもの) | |
$username = 'test'; | |
// パスワード(Application Passwordsプラグインで発行されたパスワード) | |
$password = 'c42C RIop uj9I QAlm ZOWx wwww'; | |
$file = "https://grapee.jp/wp-content/uploads/32187_main2.jpg"; | |
$data = wp_remote_get($file); | |
$response = wp_remote_post( $url, array( | |
'headers' => array( | |
'Authorization' => 'Basic ' . base64_encode( $username.':'.$password ), | |
'Content-Disposition' => 'form-data;filename=' . basename($file) | |
), | |
'body' => $data["body"] | |
) ); | |
$data = json_decode($response['body']); | |
echo $data->id; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment