-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_whatsapp_api_rc.php
112 lines (89 loc) · 3.47 KB
/
send_whatsapp_api_rc.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
// Include the CSV processing logic
include 'process_csv.php';
// Initialize variables for redirect URLs
$successRedirect = 'send_whatsapp_success.php';
$errorRedirect = 'send_whatsapp_error.php';
if (isset($_POST['submit'])) {
// Check if both message and CSV are provided
if (!empty($_POST['message']) && !empty($phoneNumbers)) {
// Message input from the form
$message = $_POST['message'];
$appKey = 'Your API Key';
$authKey = 'Your Auth Key';
$sandbox = 'false';
// Flag to check if any message was successfully sent
$messageSent = false;
// Loop through each phone number and send the WhatsApp message
foreach ($phoneNumbers as $mobno) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://whats-api.rcsoft.in/api/create-message',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array(
'appkey' => $appKey,
'authkey' => $authKey,
'to' => $mobno,
'message' => $message,
'sandbox' => $sandbox,
),
));
$response = curl_exec($curl);
// Display HTTP status code for debugging
$httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
echo "HTTP Status Code: $httpStatus";
if ($response === false) {
echo "Error: " . curl_error($curl);
}
else {
// Parse the response (assuming it's in JSON format)
$responseData = json_decode($response, true);
if (isset($responseData['error'])) {
if ($httpStatus == 401) {
echo "Authentication failed. Please check your credentials.";
} else {
echo "Error sending message to $mobno: " . $responseData['error'];
}
} else {
$messageSent = true;
echo "Message sent to $mobno: " . $response;
}
}
curl_close($curl);
}
// Redirect based on the result
if ($messageSent) {
header("Location: $successRedirect");
exit;
} else {
header("Location: $errorRedirect");
exit;
}
}
elseif (empty($_POST['message']) && !empty($phoneNumbers)) {
// Case: Message not typed but CSV uploaded
header("Location: $errorRedirect");
exit;
}
elseif (!empty($_POST['message']) && empty($phoneNumbers)) {
// Case: Message typed but CSV not uploaded
header("Location: $errorRedirect");
exit;
}
else {
// Case: Neither message nor CSV uploaded
header("Location: $errorRedirect");
exit;
}
}
// // After sending the message, redirect to the result page with the appropriate status
// $status = ($response && isset($response->message_status) && $response->message_status === 'Success') ? 'success' : 'error';
// header("Location: send_whatsapp_result.php?status=$status");
// exit;
?>