By stevensi1018 - Tue Dec 04, 2018 10:50 am
- Tue Dec 04, 2018 10:50 am
#821915
I created a custom function:
Code: Select all
but when I try to call it with executeCurl($request,$data,$token) I just get a blank screen. Even if I remove the parameters, it doesn't work. The code works perfectly if I don,t put it in a function but I have so many similar calls that I would prefer to use a custom function because my code is way too long otherwise (500 lines and counting...)function executeCurl($request,$data,$token)
{
$data_string = json_encode($data);
@@data_string = $data_string;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $request,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $token,
"cache-control: no-cache",
"Content-Type: application/json",
'Content-Length: ' . strlen($data_string)
)
));
$result = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
@@statusCode = $statusCode;
$err = curl_error($ch);
curl_close($ch);
if ($err)
{
@@errorNo = "cURL Error #:" . $err . "<br>";
}
else
{
if ($statusCode != 200 && $statusCode != 201)
{
if (isset ($result) and isset($result->error))
{
@@error = "Error code: " . $result->error->code;
@@errorMessage = $result->error->message;
}
else
{
@@error = "Error: HTTP status code: " . $statusCode;
}
}
else
{
$arrayResponse = json_decode($result);
}
}
}
Attachments
blank.PNG (10.32 KiB) Viewed 5224 times