Back

Step by Step Guide to Building a Tilda Publishing API Integration in PHP

Aug 18, 20247 minute read

Introduction

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!

Prerequisites

Before we jump in, make sure you've got:

  • A PHP environment up and running (I know you've probably got this sorted already)
  • A Tilda account with API credentials (if you haven't got these yet, hop over to your Tilda dashboard and grab them)

Setting up the project

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.

Authentication

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!

Making API Requests

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?

Core API Functionalities

Let's dive into some core functionalities:

Fetching page data

$pageId = 123456; // Replace with your actual page ID $response = $client->request('GET', "getpage/?pageid=$pageId"); $pageData = json_decode($response->getBody(), true);

Updating page content

$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>' ] ]);

Advanced Features

Ready to level up? Let's tackle some advanced features:

Exporting pages

$projectId = 987654; // Replace with your actual project ID $response = $client->request('POST', "exportproject/", [ 'form_params' => [ 'projectid' => $projectId ] ]); $exportData = json_decode($response->getBody(), true);

Managing images

$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') ] ] ]);

Error Handling and Logging

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()); }

Optimizing Performance

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!

Testing the Integration

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); } }

Deployment Considerations

When deploying, remember to:

  • Store your API key securely (use environment variables!)
  • Implement rate limiting to avoid hitting API limits
  • Set up proper logging for production environments

Conclusion

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!