51 lines
1.7 KiB
PHP

<?php
namespace Drupal\FreshdeskShim\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class FDShim_Controller extends ControllerBase {
public function fd_route(Request $request) {
if ($request->isMethod('POST')) {
$postData = $request->request->all();
// Initial variables
$apiUrl = 'https://jvmanufacturing-help.freshdesk.com/api/v2/tickets';
$apiKey = 'OTBuVjM4cVA0dDdHaXVJeUhoWDpY';
$fd = curl_init($apiUrl);
$headers = [
'Content-Type: multipart/form-data',
'Authorization: ' . $apiKey
];
// cURL Options
curl_setopt($fd, CURLOPT_RETURNTRANSFER, true);
curl_setopt($fd, CURLOPT_POST, true);
curl_setopt($fd, CURLOPT_POSTFIELDS, http_build_query($postData));
curl_setopt($fd, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($fd);
// HTTP Response Codes:
// 200 - Request OK
// 405 - Method Not Allowed
// 500 - Internal Error
if (curl_errno($fd)) {
// cURL error handling
$error = curl_error($fd);
curl_close($fd);
return new JsonResponse(['error' => $error], 500);
} else {
// return cURL response if successful
curl_close($fd);
return new JsonResponse(json_decode($response, true), 200);
}
} else {
// reject all methods that aren't POST requests
return new JsonResponse(['error'=> 'Method Not Allowed'], 405);
}
}
}