Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add alt text for image uploads? #954

Closed
mbourne opened this issue May 17, 2021 · 7 comments
Closed

How to add alt text for image uploads? #954

mbourne opened this issue May 17, 2021 · 7 comments

Comments

@mbourne
Copy link

mbourne commented May 17, 2021

Image uploading is working fine, but I want to alt text.

I tried my luck (since this is the Twitter API's syntax) with adding 'alt_text' => 'My alt text here' as in the following block, but it fell over, and didn't post the tweet at all.

$media = $connection->upload('media/upload', ['media' => 'path/to/image']);
$parameters = [
	'status' => 'My status here',
	'media_ids' => $media->media_id_string,
	'alt_text' => "My alt text here"
];
$connection->post('statuses/update', $parameters);

How do I add alt text? TIA.

@abraham
Copy link
Owner

abraham commented May 17, 2021

You should set the alt text with POST media/metadata/create. It will require three API calls.

  1. Upload media
  2. Add metadata to the media
  3. Create tweet with media
@mbourne
Copy link
Author

mbourne commented May 20, 2021

Thank you for your response. I'm no OOP programmer, but I tried.

I added the following functions to TwitterOAuth.php:

    public function uploadMeta($path, array $parameters = [])
    {
        return $this->postMetadata($path, $parameters);
    }

and

    private function postMetadata($path, array $parameters)
    {		
        return $this->http('POST', self::UPLOAD_HOST, $path, $parameters, false);
    }

I'm calling it like this:

$result1 = $media = $connection->upload('media/upload', ['media' => "path/to/img"]);
				
$parameters = [
	'alt_text' => 'alt text here',
	'media_id' => $media->media_id_string
];

$result2 = $connection->uploadMeta('media/metadata/create', $parameters);
				
$parameters = [
	'status' => $status,
	'media_ids' => $media->media_id_string
];

$result3 = $connection->post('statuses/update', $parameters);

$result2 is giving me

  public 'request' => string '/1.1/media/metadata/create.json' (length=31)
  public 'error' => string 'Invalid json payload.' (length=21)

I tried to json_encode the $parameters array, but that was a folly.

The $parameters array within my postMetadata function is correctly (I presume) showing as Array ( [alt_text] => Correct_alt_text_here [media_id] => correct_image_ID_here )

The image upload and the status post worked fine, but of course, the alt-text did not.

So it appears I'm close.

What am I missing? TIA.

@mbourne
Copy link
Author

mbourne commented May 24, 2021

I'm still trying on this and noticed an error I was making. I'm now defining $parameters as

$parameters['media_id'] = "media ID here as string";
$parameters['alt_text']['text'] = $meta;

When I json_encode this to check if it looks right, it produces:

{
"media_id":"correct media id as string here",
"alt_text":
  {
     "text":"Correct alt text here"
  }
}

This is the same format as given on https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-metadata-create

So it appears the $parameter array I'm passing is correctly set up, yet I'm still getting public 'error' => string 'Invalid json payload.' and it's not posting.

Can anyone see the problem?

@junaidshad
Copy link

Hey, Did you manage to add Alt-texts to the uploading media ?

@mbourne
Copy link
Author

mbourne commented Nov 17, 2021

I only managed to get it to add a empty alt tag. I think I got busy on other things and never managed to solve it.

@junaidshad
Copy link

I followed this thread and it helped: #456

$twitter->post('media/metadata/create', $alt_text_payload, true);

According to the thread, passing true parameter sets the content-type to json.

@abraham
Copy link
Owner

abraham commented Nov 21, 2021

$connection = new TwitterOAuth(...);

$media_payload = [
    'media' => __DIR__ . '/tests/kitten.jpg',
];
$response = $connection->upload('media/upload', $media_payload);

$metadata_payload = [
    'media_id' => $response->media_id_string,
    'alt_text' => [
        'text' => 'Good kitty',
    ],
];
$connection->post('media/metadata/create', $metadata_payload, true);

var_dump($connection->getLastHttpCode());
$status_payload = [
    'status' => 'Hello World ' . time(),
    'media_ids' => $response->media_id_string,
];
$response = $connection->post('statuses/update', $status_payload);

var_dump($response);
@abraham abraham closed this as completed Nov 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants