Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagephp
<?php

require 'vendor/autoload.php';

use GuzzleHttp\Client;

// This is a demo script which will upload a file (./file.png) to member ID #1

// Variables which should be set

$url = 'https://mycompany.vtevents.com.au';
$uploadTo = 'members/1';
$auth = ['auth key', 'auth secret'];

$localFilePath = './file.png';
$fileMimeType = 'image/png';
$remoteFileName = 'myfile.png';
$fileSize = filesize($localFilePath);

$documentTitle = 'My document title';
$documentDescription = 'My document description';

// end vars to be set

$client = new Client([
    'base_uri' => $url
]);

// ------------------------------------------------------------------------
// Step 1 - Get a signed url which will be used to upload the files via

$response = $client->request(
    'POST',
 
$arr = json_decode('... JSON DATA GOES HERE ...', true);

   '/api/v2/documents/get-upload-url',
    [
        'auth' => $auth,
        'json' => [
            'name' => $remoteFileName,
            'type' => $fileMimeType,
            'size' => $fileSize
        ]
    ]
);

$signedUrlVars = json_decode($response->getBody(), true);


// ------------------------------------------------------------------------
// Step 2 - Upload the file-data to AWS S3


// prepare the post-data which will be sent to AWS S3
$postVals = [];
foreach ($arr$signedUrlVars['formData'] as $key => $val) {
    $postVals[] = [
        'name' => $key,
        'contents' => $val,
    ];
}

$localFilePath = './myfile.png';
$fileMimeType = 'image/png';
$remoteFileName
=
'myfile.png';

$multipart = array_merge(
    $postVals,
    [
        [
            'name' => 'Content-Type',
            'contents' => $fileMimeType,
        ],
        [
            'name' => 'file',
            'contents' => fopen($localFilePath, 'r'),
            'filename' => $remoteFileName,
            'headers' => [
                'Content-Type' => $fileMimeType,
            ],
        ],
    ]
);

try {
    $response 
print_r($multipart);

= $client->request(
        'POST',
        $signedUrlVars['url'],
        [
            'multipart' => $multipart,
        ]
    );
} catch (\GuzzleHttp\Exception\RequestException $e) {
    echo 'An error occurred while uploading to AWS S3; ' . (string)$e->getResponse()->getBody();
    exit;
}

$s3Response = (string)$response->getBody();


// ------------------------------------------------------------------------
// Step 3 - attach the uploaded file in S3 to an event/member/resource/etc

$response = $client->request(
    'POST',
    '/api/v2/' . $uploadTo . '/documents',
    [
        'auth' => $auth,
    $arr['url']    'json' => [
            'title' => $documentTitle,
            'description' => $documentDescription,
            'file' => [
                'multipart' => $multipart,'name' => $remoteFileName,
                'type' => $fileMimeType,
                'size' => $fileSize,
                'path' => $signedUrlVars['formData']['key'],
            ],
        ]
    ]
);

$docData print_r($response)= json_decode((string)$response->getBody(), true);

// ------------------------------------------------------------------------
// Done!

// Finally - Output the ID of the document which was just uploaded
echo 'Document ID: ' . $docData['id'] . "\n";

Running php test.php will output the metadata which is being sent, as well as information about the response received.

...