I’m working in a Cake PHP 2 project running PHP 7.4. I’m trying to make a POST request via CURL to a server. I’m getting some odd behaviour with CURLOPT_POSTFIELDS
. I need to send my data as an encoded JSON array so I’m doing json_encode
, but it seems that when checking the character count of this, anything over 1024 causes curl to timeout and not send?
I’ve checked max_input_vars
which is default to 1,000, and my post_max_size
is 2048M so plenty big enough, what’s going on here?
/**
* Perform a request to store the application.
* TODO: WIP
*/
private function storeApplication()
{
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getEndpoint());
curl_setopt($ch, CURLOPT_POST, true);
$customer = $this->application;
$fields = json_encode([
'product_type' => 'foo',
'data_source' => 'foo',
'data' => [
'ApiKey' => 'foo',
'AffId' => 'foo',
'Application' => [
'SubAffId' => '',
'ReqRepaymentTerm' => $this->formatField($customer, 'ReqRepaymentTerm'),
'AppTitle' => $this->formatField($customer, 'AppTitle'),
'AppFirstName' => $this->formatField($customer, 'AppFirstName'),
'AppLastName' => $this->formatField($customer, 'AppLastName'),
'AppDOBDay' => $this->formatField($customer, 'AppDOBDay'),
'AppDOBMonth' => $this->formatField($customer, 'AppDOBMonth'),
'AppDOBYear' => $this->formatField($customer, 'AppDOBYear'),
'AppMaritalStatus' => $this->formatField($customer, 'AppMaritalStatus'),
'AppDependants' => $this->formatField($customer, 'AppDependants'),
'AppHomeStatus' => $this->formatField($customer, 'AppHomeStatus'),
'AppAddressYears' => $this->formatField($customer, 'AppAddressYears'),
'AppHouseNumber' => $this->formatField($customer, 'AppHouseNumber'),
'AppPostCode' => $this->formatField($customer, 'AppPostCode'),
'AppStreet' => $this->formatField($customer, 'AppStreet'),
'AppTown' => $this->formatField($customer, 'AppTown'),
'AppCounty' => $this->formatField($customer, 'AppCounty'),
'AppEmail' => $this->formatField($customer, 'AppEmail'),
'AppMobilePhone' => $this->formatField($customer, 'AppMobilePhone'),
'EmpIncomeType' => $this->formatField($customer, 'EmpIncomeType'),
'EmpEmployerName' => $this->formatField($customer, 'EmpEmployerName'),
'EmpIndustry' => 25,
'EmpEmployedMonths' => $this->formatField($customer, 'EmpEmployedMonths'),
'EmpPayFrequency' => $this->formatField($customer, 'EmpPayFrequency'),
'EmpNetMonthlyPay' => $this->formatField($customer, 'EmpNetMonthlyPay'),
'AppLoanPurpose' => $this->formatField($customer, 'AppLoanPurpose'),
'AppLoanPrposeOther' => '',
'AppHousingSpend' => $this->formatField($customer, 'AppHousingSpend'),
'AppCreditSpend' => $this->formatField($customer, 'AppCreditSpend'),
'AppUtilitiesSpend' => $this->formatField($customer, 'AppUtilitiesSpend'),
'AppFoodSpend' => $this->formatField($customer, 'AppFoodSpend'),
'AppTransportSpend' => $this->formatField($customer, 'AppTransportSpend'),
'AppOtherSpend' => $this->formatField($customer, 'AppOtherSpend'),
'BankName' => $this->formatField($customer, 'BankName'),
'BankSortcode' => $this->formatField($customer, 'BankSortcode'),
'BankAccount' => $this->formatField($customer, 'BankAccount'),
]
]
]);
CakeLog::write('debug', "A: ".strlen($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable verbose output
$string = curl_exec($ch);
curl_close($ch);
$res = json_decode($string, true);
CakeLog::write('debug', json_encode($res));
// success
return $res;
} catch (Exception $e) { }
// it never saved
return null;
}