Get started with PHP

You must first create a project in Plasmic.

To view this quickstart with all the IDs and token placeholders replaced with real values, open your project in Plasmic Studio and press the "Code" button in the top toolbar.

Get started with PHP

Render a page or component from Plasmic from PHP using the REST API. This returns plain HTML that you can render anywhere, optionally including Javascript-hydrated interactivity.

COMPONENTNAME refers to the name of the page or component that you want to render, such as Winter22LandingPage.

Copy
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://codegen.plasmic.app/api/v1/loader/html/preview/PROJECTID/COMPONENTNAME?hydrate=1&embedHydrate=1");
// Provide the project ID and public API token.
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"x-plasmic-api-project-tokens: PROJECTID:APITOKEN"
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
curl_close($curl);
$result = json_decode($response);
echo $result->html;
?>

The above use the /preview/ route, which fetches the latest revisions, whether or not they were published! Replace with /published/ to ensure you render only published changes, or /versioned/ to render specific versions.

If you want to publish only static content and don’t want to hydrate any Javascript (don’t need any interactivity), remove the query parameters hydrate and embedHydrate.

There’s much more to explore!

  • Perform server-side rendering.
  • Render different variants.
  • Override the content or props.
  • Add dynamic behavior.
Continue learning in the docs.

Give feedback on this page