PHP Examples

PHP 7.4+ code examples for Content Hub API

List Domains

<?php
$apiKey = 'YOUR_API_KEY';
$url = 'https://api.content-hub.net/api/domains';

$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) {
    $domains = json_decode($response, true);
    print_r($domains);
} else {
    echo "Error: $httpCode";
}
?>

Create Content

<?php
$apiKey = 'YOUR_API_KEY';
$url = 'https://api.content-hub.net/api/content';

$data = [
    'title' => 'My Article',
    'content' => 'Article content here',
    'domain' => 'example.com',
    'slug' => 'my-article',
    'meta_description' => 'Article description'
];

$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 "Content created: " . json_encode($result);
} else {
    echo "Error: $httpCode";
}
?>

Update Content

<?php
$apiKey = 'YOUR_API_KEY';
$contentId = 'content-123';
$url = "https://api.content-hub.net/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 "Content updated: " . $response;
} else {
    echo "Error: $httpCode";
}
?>

Delete Content

<?php
$apiKey = 'YOUR_API_KEY';
$contentId = 'content-123';
$url = "https://api.content-hub.net/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 "Content deleted successfully";
} else {
    echo "Error: $httpCode";
}
?>