Hey there, fellow developer! Ready to dive into the world of Tilda Publishing API integration? You're in for a treat. This guide will walk you through creating a robust PHP integration with Tilda's API, allowing you to harness the power of this versatile website builder programmatically. Let's get our hands dirty!
Before we jump in, make sure you've got:
Let's kick things off by setting up our project:
mkdir tilda-api-integration cd tilda-api-integration composer init composer require guzzlehttp/guzzle
We're using Guzzle here for our HTTP requests. It's a solid choice for handling API interactions.
Alright, time to get cozy with Tilda's API. First things first, let's handle authentication:
<?php require 'vendor/autoload.php'; use GuzzleHttp\Client; $client = new Client([ 'base_uri' => 'https://api.tildacdn.info/v1/', 'headers' => [ 'Authorization' => 'Bearer YOUR_API_KEY_HERE' ] ]);
Replace YOUR_API_KEY_HERE
with your actual API key. Keep it secret, keep it safe!
Now that we're all set up, let's make our first request:
try { $response = $client->request('GET', 'getprojectslist'); $projects = json_decode($response->getBody(), true); print_r($projects); } catch (\Exception $e) { echo "Oops! " . $e->getMessage(); }
This will fetch your project list. Pretty neat, huh?
Let's dive into some core functionalities:
$pageId = 123456; // Replace with your actual page ID $response = $client->request('GET', "getpage/?pageid=$pageId"); $pageData = json_decode($response->getBody(), true);
$pageId = 123456; // Replace with your actual page ID $response = $client->request('POST', "updatepage/", [ 'form_params' => [ 'pageid' => $pageId, 'title' => 'New Page Title', 'html' => '<h1>Hello, Tilda!</h1>' ] ]);
Ready to level up? Let's tackle some advanced features:
$projectId = 987654; // Replace with your actual project ID $response = $client->request('POST', "exportproject/", [ 'form_params' => [ 'projectid' => $projectId ] ]); $exportData = json_decode($response->getBody(), true);
$response = $client->request('POST', "uploadimage/", [ 'multipart' => [ [ 'name' => 'projectid', 'contents' => '987654' // Replace with your actual project ID ], [ 'name' => 'image', 'contents' => fopen('path/to/your/image.jpg', 'r') ] ] ]);
Don't forget to implement robust error handling:
try { // Your API request here } catch (\GuzzleHttp\Exception\ClientException $e) { $errorResponse = $e->getResponse(); $errorBody = json_decode($errorResponse->getBody(), true); error_log("Tilda API Error: " . $errorBody['message']); } catch (\Exception $e) { error_log("Unexpected error: " . $e->getMessage()); }
To keep things speedy, consider implementing caching:
$cacheFile = 'projects_cache.json'; if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < 3600)) { $projects = json_decode(file_get_contents($cacheFile), true); } else { $response = $client->request('GET', 'getprojectslist'); $projects = json_decode($response->getBody(), true); file_put_contents($cacheFile, json_encode($projects)); }
This caches your project list for an hour. Adjust the time as needed!
Don't forget to test your integration thoroughly. Here's a quick PHPUnit test to get you started:
use PHPUnit\Framework\TestCase; class TildaApiTest extends TestCase { public function testGetProjectsList() { $client = new \GuzzleHttp\Client([ 'base_uri' => 'https://api.tildacdn.info/v1/', 'headers' => ['Authorization' => 'Bearer YOUR_API_KEY_HERE'] ]); $response = $client->request('GET', 'getprojectslist'); $this->assertEquals(200, $response->getStatusCode()); $projects = json_decode($response->getBody(), true); $this->assertIsArray($projects); } }
When deploying, remember to:
And there you have it! You've just built a solid Tilda Publishing API integration in PHP. From basic authentication to advanced features like exporting projects and managing images, you're now equipped to harness the full power of Tilda programmatically.
Remember, the Tilda API is a powerful tool, so use it wisely and always refer to the official documentation for the most up-to-date information. Happy coding, and may your Tilda projects be ever awesome!