Hello! I’m trying to write a cURL request for my Wordpress site to integrate Discourse with woo subscriptions. Here’s my function, which doesn’t seem to work. Am I missing something?
function execute_discourse_curl_request($subscription) {
$subscription_status = $subscription->get_status();
$user_id = $subscription->get_user_id();
$user = get_user_by('ID', $user_id);
$username = $user->user_login;
$request = "";
if ($subscription_status == 'active') {
$request = "PUT";
}
if ($subscription_status == 'cancelled' || $subscription_status == 'on-hold') {
$request = "DELETE";
}
$api_key = get_option( 'discourse_custom_api_key' );
$group_id = 41;
$api_url = 'https://forum.example.com/groups/' . $group_id . '/members.json';
$data = [
'usernames' => [$username],
];
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $api_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $request,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => [
"Api-Key: " . $api_key,
"Api-Username: system",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$to = 'hello@example.com';
$subject = 'Error in Discourse User Update cURL Request';
$message = 'There was an error';
wp_mail($to, $subject, $message);
}
}