execute_post_test.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Script pour exécuter une requête POST_userV2.php de test
  4. */
  5. // Charger le JSON de la requête
  6. $json_file = __DIR__ . '/post_user_request.json';
  7. $post_data = json_decode(file_get_contents($json_file), true);
  8. if ($post_data === null) {
  9. die("❌ Erreur: Impossible de charger le fichier post_user_request.json\n");
  10. }
  11. // Générer un nouveau login unique pour éviter les conflits
  12. $timestamp = time();
  13. $post_data['login'] = "test.user.{$timestamp}@sipa-automobiles.fr";
  14. $post_data['mail'] = $post_data['login'];
  15. $post_data['employeeNumber'] = "TEST{$timestamp}";
  16. $post_data['dtContractStart'] = date('Y-m-d\TH:i:s', strtotime('+1 day'));
  17. $post_data['seniorityDate'] = $post_data['dtContractStart'];
  18. echo "🚀 Exécution de la requête POST_userV2.php...\n\n";
  19. echo "📋 Données envoyées:\n";
  20. echo json_encode($post_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n\n";
  21. // Simuler une requête POST en appelant directement le script PHP
  22. $_SERVER['REQUEST_METHOD'] = 'POST';
  23. $_GET = [];
  24. $_POST = [];
  25. // Capturer le body de la requête
  26. $json_body = json_encode($post_data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  27. // Créer un fichier temporaire pour simuler php://input
  28. $temp_file = tempnam(sys_get_temp_dir(), 'php_input_');
  29. file_put_contents($temp_file, $json_body);
  30. // Rediriger php://input vers notre fichier temporaire
  31. // Note: On ne peut pas vraiment rediriger php://input, donc on va utiliser curl
  32. echo "📡 Envoi de la requête via curl...\n\n";
  33. $ch = curl_init();
  34. curl_setopt($ch, CURLOPT_URL, 'http://localhost/api/user/user/POST_userV2.php');
  35. curl_setopt($ch, CURLOPT_POST, true);
  36. curl_setopt($ch, CURLOPT_POSTFIELDS, $json_body);
  37. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  38. 'Content-Type: application/json',
  39. 'Content-Length: ' . strlen($json_body)
  40. ]);
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  42. curl_setopt($ch, CURLOPT_VERBOSE, false);
  43. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  44. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  45. $response = curl_exec($ch);
  46. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  47. $curl_error = curl_error($ch);
  48. curl_close($ch);
  49. echo "📊 Résultat:\n";
  50. echo "HTTP Code: {$http_code}\n\n";
  51. if ($curl_error) {
  52. echo "❌ Erreur cURL: {$curl_error}\n";
  53. echo "\n💡 Astuce: Assurez-vous que votre serveur web local est démarré (Apache/Nginx)\n";
  54. echo " et que l'URL http://localhost/api/user/user/POST_userV2.php est accessible.\n";
  55. } else {
  56. echo "📄 Réponse:\n";
  57. // Essayer de formater la réponse JSON
  58. $response_json = json_decode($response, true);
  59. if ($response_json !== null) {
  60. echo json_encode($response_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";
  61. } else {
  62. echo $response . "\n";
  63. }
  64. if ($http_code === 200 || $http_code === 201) {
  65. echo "\n✅ Utilisateur créé avec succès!\n";
  66. } else {
  67. echo "\n⚠️ La requête a retourné le code HTTP {$http_code}\n";
  68. }
  69. }
  70. // Nettoyer
  71. if (file_exists($temp_file)) {
  72. unlink($temp_file);
  73. }