POSTing to /api/v1/media/ returns 500 Internal Server Error? - API


#1

I’m trying to POST a media_attachment (image to be exact) so that I can attach it to a status update but the API keeps returning 500 internal server error. Posting of statuses work fine via API, and I can upload images via the web interface as well as Tusky fine too.

I’ve tried it on 3 different instances already so I’m sure it must be something I’m doing wrong.

Here’s a snippet of my code:

$data = array(
	'file' => '@'.realpath($my_image),
	'description' => $my_description
);
$ch = curl_init(API_URL.'media?access_token='.CLIENT_TOKEN);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
if ($result === FALSE) {
	die(curl_error($ch));
}

Supposedly, this should return the Attachment object but I get the error instead.


#2

Okay nevermind. I figured it out. I didn’t know PHP 7 deprecated using @ to reference files. I had to use CurlFile now for it to work. :+1:


#3

glad to hear!