Skip to content

Instantly share code, notes, and snippets.

@nobodyplace
Last active July 10, 2019 12:36
Show Gist options
  • Select an option

  • Save nobodyplace/cf7c888111578b54ec32c3903eefe152 to your computer and use it in GitHub Desktop.

Select an option

Save nobodyplace/cf7c888111578b54ec32c3903eefe152 to your computer and use it in GitHub Desktop.
<?php
$client = new \GuzzleHttp\Client();
$response = $client->get('https://example.com/sample.jpg');
$image = $response->getBody()->getContents();
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://example.com/wp-json/wp/v2/'
]);
$response = $client->post('media',[
'auth' => [$user, $password],
'headers' => [
'Content-Disposition' => [
'form-data; filename=sample.jpg'
]
])
,'multipart' => [
[
'name' => 'file',
'contents' => $image,
'filename' => 'sample.jpg',
],
]
]);
if(preg_match('/(\d+)$/',$response->getHeader('Location')[0],$matches)){
$media_id = (int) $matches[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment