0

I'm trying to send some form fields along with multiple files using CURL in php, got the below error.

"Required Multipartfile parameter utterance is not present". 

Below listed is the piece of code, I have used in my application.

$a1614_1 = "@" .realpath("RecordFiles/1614_1.wav");


$payload = array("sessionid"=>$sessionid,"clientid"=> $clientid,"userid"=>$userid,"utterance"=> $a1614_1,"type"=>$type,"action"=>$action);

$multipart_boundary = "ABC1234";

$request_headers    = array();
$request_headers[]  = "Content-Type: multipart/form-data; boundary=". $multipart_boundary;

$curl_options = array(
CURLOPT_URL => $base_api_url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query( $payload),
CURLOPT_HTTP_VERSION  => 1.0,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true,
CURLOPT_HTTPHEADER => $request_headers
);

$curl = curl_init();
curl_setopt_array( $curl, $curl_options );
$result = curl_exec( $curl );
curl_close ($curl);

In my code i'm passing utterance as one of the multipart parameter but still i'm getting the same error. I have googled to resolve the problem but not able to fix it. Is there any problem in the above code approach, if any one have idea please share to me to fix or resolve the issue.

Please help / assist me to resolve this issue.

1 Answer 1

3

You're trying to do manually things that curl should do (build http query, generate boundary, add headers, content length). BTW, for PHP 5.5+ you should use CURLFile instead of '@file'.

Code for PHP below 5.5:

$a1614_1 = "@" .realpath("RecordFiles/1614_1.wav");

$payload = array("sessionid"=>$sessionid,"clientid"=> $clientid,"userid"=>$userid,"utterance"=> $a1614_1,"type"=>$type,"action"=>$action);

$curl_options = array(
CURLOPT_URL => $base_api_url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTP_VERSION  => 1.0,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true
);

$curl = curl_init();
curl_setopt_array( $curl, $curl_options );
$result = curl_exec( $curl );
curl_close ($curl);

Code for PHP 5.5+:

$baseApiUrl = 'http://some.net/api';
$file = new CurlFile(realpath('RecordFiles/1614_1.wav'));

$fields = array(
    'sessionid' => $sessionid,
    'clientid' => $clientid,
    'userid' => $userid,
    'utterance' => $file,
    'type' => $type,
    'action' => $action
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $baseApiUrl);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curl);

curl_close($curl);
Sign up to request clarification or add additional context in comments.

1 Comment

@user3223414 you can set my answer as accepted if that was helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.