-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.php
50 lines (41 loc) · 1.46 KB
/
publish.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
// Habilitar CORS para solicitudes entre dominios
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type");
// Obtener datos del cuerpo de la solicitud
$request_body = file_get_contents('php://input');
$data = json_decode($request_body, true);
if (!isset($data['title']) || !isset($data['content'])) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'Faltan datos requeridos.']);
exit();
}
$title = $data['title'];
$content = $data['content'];
// Configurar la solicitud a la API de WordPress
$wp_api_url = 'http://www.wordpress.lan/wp-json/wp/v2/posts/';
$wp_auth_token = 'YWxiZXJ0bzpxWEJLIG5ncUsgdVdzMiB1N0JlIFVtZjkgSlFyRQo='; // Token codificado en base64
$post_data = [
'status' => 'publish',
'title' => $title,
'content' => $content
];
$ch = curl_init($wp_api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Basic ' . $wp_auth_token
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_status === 201) {
echo json_encode(['success' => true]);
} else {
$error = curl_error($ch);
echo json_encode(['success' => false, 'error' => $error ? $error : $response]);
}
curl_close($ch);
?>