Unable to publish an uploaded Asset. (CMA PHP SDK)

So I have been successful in uploading an asset (a PDF), via the Contentful Management API, however it only seems to upload, process it, but not publish it. I seem to get an error message of ‘Validation error’. Here’s my markup:

$file = new \Contentful\Core\File\RemoteUploadFile(
				'filename.pdf', // File name
				'application/pdf', // Mime type
				'http://myapp.com/assets/file.pdf' // URL where the file is located
			);
			$asset = new \Contentful\Management\Resource\Asset();
			$asset->setTitle('en-AU', 'A File')
			    ->setFile('en-AU', $file);

			$environmentProxy->create($asset);

			$asset->process('en-AU');
			$assetEntry = $environmentProxy->getAsset($asset->getId());

		  $assetEntry->publish();

Asset processing is an asynchronous process and so you might be trying to publish the asset prior to the asset processing finishing. With PHP you can check the class of the asset’s file: if it is a Contentful\Core\FileRemoteUploadFile the processing hasn’t finished; if it is a Contentful\Core\File\ImageFile processing has finished.

$file = new \Contentful\Core\File\RemoteUploadFile(
    'filename.pdf', // File name
    'application/pdf', // Mime type
    'http://myapp.com/assets/file.pdf' // URL where the file is located
);
$asset = new \Contentful\Management\Resource\Asset();
$asset->setTitle('en-AU', 'A File')->setFile('en-AU', $file);

$environmentProxy->create($asset);

$asset->process('en-AU');

$assetEntry = $environmentProxy->getAsset($asset->getId());

while (is_a($assetEntry->getFile('en-AU'), 'Contentful\Core\File\RemoteUploadFile')) {
  sleep(1);
  $assetEntry = $environmentProxy->getAsset($asset->getId());
}

$assetEntry->publish();

Also what is the default locale of your Contentful space? If you are creating an asset and only giving a en-AU title, but the default locale is not en-AU then you’ll get a 422 Validation Error

I’m having the same issue.

{"sys":{"type":"Error","id":"ValidationFailed"},"message":"Validation error","details":{"errors":[{"name":"required","path":["fields","file","en-US","url"],"details":"The property \"url\" is required here"}]},"requestId":"595596f9aba4ec5f313e86f30d9583b3"}

Adding a ‘timeout’ of 5 seconds, to test against the asynchronous approach gives another issue.

{"sys":{"type":"Error","id":"VersionMismatch"}...

However, even passing the version id (which would be 1 as it’s a new asset) in the header, it fails to publish the asset and keeps throwing the same error.

When you create the asset it will be at version 1, then after you process it, it is at version 3 (I don’t know why it isn’t 2, but anyways…). So you need to pass the version number of 3 when making the request to publish the asset.

Best is if you poll the asset until the processing is done and take the version number given in the most recent polling and use that when making the publish request.

Hope this helps!

Cheers. I’ll give that ago.