PHP Examples
PHP 7.4+ code examples for Content Hub API
List Pages
<?php
$apiKey = 'YOUR_API_KEY';
$url = 'https://api.contenhub.co/api/content';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
$pages = json_decode($response, true);
print_r($pages);
} else {
echo "Error: $httpCode";
}
?>Create Page with CTA
<?php
$apiKey = 'YOUR_API_KEY';
$url = 'https://api.contenhub.co/api/content';
$data = [
'title' => 'Get a Free Quote',
'content' => 'Tell us what you need and we will get back to you.',
'slug' => 'free-quote',
'meta_description' => 'Request a free quote today.',
'cta' => [
'type' => 'button',
'label' => 'Request Quote',
'url' => 'https://example.com/quote'
]
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 201) {
$result = json_decode($response, true);
echo "Page created: " . json_encode($result);
} else {
echo "Error: $httpCode";
}
?>Update Page
<?php
$apiKey = 'YOUR_API_KEY';
$contentId = 'content-123';
$url = "https://api.contenhub.co/api/content/$contentId";
$updates = [
'title' => 'Updated Title',
'content' => 'Updated content'
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($updates));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
echo "Page updated: " . $response;
} else {
echo "Error: $httpCode";
}
?>Delete Page
<?php
$apiKey = 'YOUR_API_KEY';
$contentId = 'content-123';
$url = "https://api.contenhub.co/api/content/$contentId";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 204) {
echo "Page deleted successfully";
} else {
echo "Error: $httpCode";
}
?>