I am trying to submit some post variables along with some files on 3rd party URL via CURL call.
For that i have used ajax from where i am passing whole form data on controller and from controller, i am calling CURL with form data.
but unfortunately those data is not passed to 3rd party URL.
here is controllers code `
$clientId = $this->request->data['clientSsoCert'];
//file details which i wants to upload
$xmlTmpPath = $this->request->data['clientSsoXml']['tmp_name'];
$xmlType = $this->request->data['clientSsoXml']['type'];
$xmlName = $this->request->data['clientSsoXml']['name'];
$xmlNameCmps = explode(".", $xmlName);
$xmlExtension = strtolower(end($xmlNameCmps));
CURL calls logic
$strRequestURL = 'https://abcd.com';
try {
$conn = curl_init($strRequestURL);
curl_setopt($conn, CURLOPT_URL, $strRequestURL);
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
curl_setopt($conn, CURLOPT_POST, true);
curl_setopt($conn, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data'));
curl_setopt($conn, CURLOPT_POSTFIELDS, ['custId' => $clientId , 'xmlfile' => $xmlTmpPath]);
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($conn, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($conn);
if (curl_errno($conn)) {
return ['status' => false, 'error' => curl_error($conn)];
} else {
$response = json_decode($output);
if ($response->status == 'error') {
return ['status' => false, 'error' => $response->errorMessage];
} else {
return ['status' => true, 'xmlFileName' => ($response->xmlfileName) ?? "", 'certFileName' => ($response->certfileName) ?? ""];
}
}
curl_close($conn);
} catch (\Exception $e) {
return ['status' => false, 'error' => $e->getMessage()];
}
Above codes returns error which i shows on SS.
I am not sure how to upload image using CURL so any suggestions for that? i have used
curl_setopt($conn, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data'));
and also the same on html form. while calling the same API via postman without passing any params the same error i got, from there i found my params are not reaching there, thats why that error is showing.
I feel you can't upload files with the temp name only. You need to convert your file into a CURL object. with the help of curl_file_create
then only you can upload it.
Eg: convert file to CURL Object.
$xmlFileObj = curl_file_create($xmlTmpPath, $xmlType, $xmlName);
Then need to pass that object on CURL.
$strRequestURL = 'https://abcd.com';
try {
$conn = curl_init($strRequestURL);
curl_setopt($conn, CURLOPT_URL, $strRequestURL);
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
curl_setopt($conn, CURLOPT_POST, true);
curl_setopt($conn, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data'));
curl_setopt($conn, CURLOPT_POSTFIELDS, ['custId' => $clientId , 'xmlfile' => $xmlFileObj]);
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($conn, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($conn);
if (curl_errno($conn)) {
return ['status' => false, 'error' => curl_error($conn)];
} else {
$response = json_decode($output);
if ($response->status == 'error') {
return ['status' => false, 'error' => $response->errorMessage];
} else {
return ['status' => true, 'xmlFileName' => ($response->xmlfileName) ?? "", 'certFileName' => ($response->certfileName) ?? ""];
}
}
curl_close($conn);
} catch (\Exception $e) {
return ['status' => false, 'error' => $e->getMessage()];
}
Let me know if it is working or not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.