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