Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Created October 30, 2018 08:57
Show Gist options
  • Save kurozumi/72a86e7c5775ae22e6d58e74972699c6 to your computer and use it in GitHub Desktop.
Save kurozumi/72a86e7c5775ae22e6d58e74972699c6 to your computer and use it in GitHub Desktop.
WP REST API v2で画像投稿
<?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