<?php

require_once( '../../../../wp-config.php' );

function endl(): string {
	return "\r\n";
}

$file     = __DIR__ . '/dummy_file.png';
$filename = basename( $file );
$filetype = filetype( $file );

$path = 'http://your.wordpress.tldde/wp-json/wp/v2/media';


$boundary = wp_generate_password( 10 );
$headers  = array(
	'content-type' => 'multipart/form-data; boundary=' . $boundary
);


$payload = '';
// First, add the standard POST fields:
$post_fields = [
    'title' => 'test' . $i . '_' . time(),
];
foreach ( $post_fields as $name => $value ) {
    $payload .= '--' . $boundary . endl();
    $payload .= 'Content-Disposition: form-data; name="' . $name . '"' . endl() . endl();
    $payload .= $value . endl();
}

$payload .= '--' . $boundary . endl();
$payload .= 'Content-Disposition: form-data; name="meta[my_meta]"' . endl() . endl();
$payload .= '1' . endl();

$payload .= '--' . $boundary . endl();
$payload .= 'Content-Disposition: form-data; name="file"; filename="' . $filename . '"' . endl();
$payload .= 'Content-Type: ' . $filetype . endl() . endl();
$payload .= file_get_contents( $file ) . endl();
$payload .= '--' . $boundary . '--';


 $request = [
    'headers' => $headers,
    'body'    => $payload,
];
$response =  wp_remote_post( $path, $request );
//var_dump( json_decode( wp_remote_retrieve_body( $response ) ) );
//var_dump( $response );
echo $response['body'];
