Commit 9800285b authored by Max Koder's avatar Max Koder

[MAJ] Media attachment

Status : visibility + attachments
parent ca6bf859
......@@ -237,7 +237,44 @@ TootoPHP give you possibility to toot a new status from PHP :
$app->postStatus('@maxk@mastodon.xyz This status is posted by #PHP #TootoPHP');
At the moment, only text is supported.
3 parameters are supported :
- `$content` : The text of the status
- `$visibility` : (optionnal) Toot's visibility. Either "direct", "private", "unlisted" or "public"
- `$medias` : (optional) array of media IDs to attach to the status (maximum 4)
Medias can be upload with the following method :
## Create an attachement
$app->createAttachement('path/to/the/media.png');
Only the path to the media is needed.
Returns an Attachment that can be used when creating a status.
Attachment is an associative array :
| Attribute | Description |
| ------------------------ | ----------- |
| `id` | ID of the attachment |
| `type` | One of: "image", "video", "gifv" |
| `url` | URL of the locally hosted version of the image |
| `remote_url` | For remote images, the remote URL of the original image |
| `preview_url` | URL of the preview image |
| `text_url` | Shorter URL for the image, for insertion into text (only present on local images) |
To use them in a new status, specify their IDs in a array like this :
```php
// Create Attachments
$img1 = $app->createAttachement("C:\Users\Max\Pictures\\tootophp.png");
$img2 = $app->createAttachement("C:\Users\Max\Pictures\mastodonauth.png");
// Post the new status with both medias
$app->postStatus('This is a test with #TootoPHP #PHP', 'public', [$img1['id'], $img2['id']]);
```
In order to use Attachments with TootoPHP, you need the `libcurl` package.
Enjoy !
......
......@@ -25,4 +25,4 @@ if ( $app === false) {
//var_dump($app->getStatuses('1629'));
//var_dump($app->getFollowers(1629, ['limit' => 5]));
\ No newline at end of file
//var_dump($app->getFollowers(1629, ['limit' => 5]));
......@@ -265,19 +265,28 @@ class TootoPHP
* Post a new status
*
* Post a new status in Mastodon instance
* Actuallay, only text is allowed
*
* Return entire status as an array
*
* @param string $content
* @param string $content Toot content
* @param string $visibility Toot visibility (optionnal)
* Values are :
* - public
* - unlisted
* - private
* - direct
* @param array $medias Medias IDs
* @return array
*/
public function postStatus($content)
public function postStatus($content, $visibility = 'public', $medias = [])
{
return $this->http->post(
$this->http->apiURL . 'statuses',
$this->headers,
[
'status' => $content
'status' => $content,
'visibility' => $visibility,
'media_ids' => $medias
]
);
}
......@@ -354,6 +363,44 @@ class TootoPHP
);
}
/**
* Upload a media from drive to use it in a toot
*
* @param string $filePath Path to the media
* @return array
*/
public function createAttachement($filePath)
{
$mimeType = mime_content_type($filePath);
$curlFile = curl_file_create( $filePath, $mimeType, 'file' );
$url = $this->http->domainURL . $this->http->apiURL . 'media';
$headers = [
'Content-Type' => 'multipart/form-data',
];
$postField =[
'access_token' => $this->appCredentials['bearer'],
'file' => $curlFile,
];
$opts = [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postField,
];
$curlHandle = curl_init();
curl_setopt_array( $curlHandle, $opts );
$response = curl_exec( $curlHandle );
curl_close( $curlHandle );
return json_decode( $response, true );
}
/**
* Authentify user
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment