nabeel ahmed
nabeel ahmed

Reputation: 11

How to Upload Video to Pinterest Media via API

                $curl = curl_init();

                curl_setopt_array($curl, array(
                    CURLOPT_URL => 'https://api.pinterest.com/v5/media',
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => '',
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 0,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => 'POST',
                    CURLOPT_POSTFIELDS =>'{
                   "media_type": "video"
                    }',
                    CURLOPT_HTTPHEADER => array(
                        "Authorization: Bearer  {$pages['token']}",
                        'Content-Type: application/json',
                        'Accept: application/json'
                    ),
                ));

                $response = curl_exec($curl);

                curl_close($curl);

                $manage = json_decode($response, true);
                return $manage;

Output I get:

media_id: "5248805388878404421" media_type: "video" upload_parameters: {x-amz-date: "20220818T052950Z",…} Content-Type: "multipart/form-data" key: "uploads/ae/7a/15/2:video:1074179086030577351:5248805388878404421" policy: "security_token" x-amz-signature: "65d1ca782e381e17a7b4329079c9ff9c2c44524497c084ebcd5d84823dc4187d" upload_url: "https://pinterest-media-upload.s3-accelerate.amazonaws.com/"

How do I upload my video on that amazon aws link. Documentation don't provide any details about that. Please share the code.

Upvotes: 0

Views: 503

Answers (1)

M_Z
M_Z

Reputation: 1

Some informations upfront.

The server you're uploading the media to, will only respond with http headers so you need to check for the http code 204.

The server wants those upload parameters from the request you did, so just put them into an array and send it with the CURLFile to the url you have.

You won't need any authentication header, the server will know which upload this is based of those upload parameters you're sending back.

And that's it. Here's how i did it:

$data = array();

$upload_parameters = array(); //in your case $manage['upload_parameters']
foreach ($upload_parameters as $key => $value) {
    $data[$key] = $value;
}
$data['file'] = new CURLFile('/example/media.mp4');

$options = array(
   CURLOPT_URL => 'https://pinterest-media-upload.s3-accelerate.amazonaws.com/',
   CURLOPT_POST => 1,
   CURLOPT_HTTPHEADER => array('Content-Type: multipart/form-data'),
   CURLOPT_POSTFIELDS => $data,
   CURLOPT_RETURNTRANSFER => 1,
   CURLOPT_HEADER => 1 
);

$curl = curl_init();
curl_setopt_array($curl, $options);
curl_exec($curl);

$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($http_code == 204) {
    //upload worked
}

The server only returns header, so you need to check for http code 204.

Upvotes: 0

Related Questions