| Server IP : 103.48.194.25 / Your IP : 216.73.217.33 Web Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.33 System : Linux VMHostDefault 3.10.0-1160.42.2.el7.x86_64 #1 SMP Tue Sep 7 14:49:57 UTC 2021 x86_64 User : sieuthimay ( 1016) PHP Version : 8.2.20 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/quagiare.com-bk/public_html/ |
Upload File : |
<?php
$site_url = "https://linhkiencasu.com";
$username = "tritigi"; // user WP
$password = "tudongcapnhat"; // application password
// --------------------
// Bước 1: Upload ảnh
// --------------------
$image_path = "sample.jpg";
$ch = curl_init($site_url . "/wp-json/wp/v2/media");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Disposition: attachment; filename="' . basename($image_path) . '"',
'Content-Type: image/jpeg'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($image_path));
$response = curl_exec($ch);
curl_close($ch);
$media = json_decode($response, true);
$image_id = $media['id'] ?? 0;
$image_url = $media['source_url'] ?? '';
echo "Ảnh upload ID: $image_id - URL: $image_url\n";
// --------------------
// Bước 2: Tạo bài viết
// --------------------
$post_data = [
"title" => "Demo API: Thêm bài viết có ảnh",
"content" => "<p>Đây là nội dung bài viết với ảnh minh họa.</p><img src=\"$image_url\" />",
"status" => "publish",
"featured_media" => $image_id
];
$ch = curl_init($site_url . "/wp-json/wp/v2/posts");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
$response = curl_exec($ch);
curl_close($ch);
$post = json_decode($response, true);
echo "Bài viết mới tạo: " . $post['link'] . "\n";