Post form data to java server using php curl

Recently one of client come up with the requirements to post data from php platform to java platform. We thought it would be simpler task as we need to fetch data from mysql database and need to do http post to the java server. We can use the php curl and post the data to java server. Simple stuff !!! right ? Well not exactly. We tried to use the php curl and used array to send the data to server. To our surprise, data was not captured by java server. We were using the following code to post form data to java server:


$fields = array(

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$output = curl_exec($ch);

We tried to investigate the problem and our first suspicion was that the php curl is not working properly. We create a php script which just listens the request and print all the parameters in the log file.We used the above code and we were able to extract the parameters from the request. So it was the time we ask our friends google and stackoverflow. We read many articles and documentations but there were no solution for this problem. Later we realized that java server is not unmarshalling the post data(mutipart/form-data) which is in array format from php curl. So we decided to use other approach i.e. send the parameters in urlencoding string as we have http servlet on the java server side.

We hope this would help other developers to resolve this type of issue. Alternative solution to resolve this issue is to add support for multipart/form-data on java server using common’s ServletFileUpload
Please let us know your questions.

Related Posts

Post form data to java server using php curl was last modified: April 11th, 2016 by Kapil Jain
  • Weird your case.

    In our case we were posting a WAV file among other user information to a transcription service (audio to text).
    Never had your problem.
    The jsp response was the paragraph of text resulting from the audio.

    $audiofile = _AUDIO_FOLDER_.$queue[‘AudioFileName’];

    $postData[ ‘audioFileName’ ] = ‘MyAudio’;
    $postData[ ‘accessCode’ ] = “acrazypasshere”;
    $postData[ ‘fileContent’ ] = “@{$audiofile}”;
    $postData[ ‘profileName’ ] = $queue[‘DictationUsername’];
    $postData[ ‘accountPwd’ ] = $queue[‘DictationPassword’];

    // Initialize cURL
    $ch = curl_init();

    // Setup CURL handler
    curl_setopt($ch, CURLOPT_URL, “” );
    curl_setopt($ch, CURLOPT_PORT, 9090 );
    curl_setopt($ch, CURLOPT_HEADER, FALSE );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POST, TRUE );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData );
    $response = curl_exec( $ch );
    curl_close ($ch);

    $transc = trim($response);

    As you can see, nothing developed by the NASA. 🙂

  • Hi Fernando,

    Yes, nothing developed by NASA and we all forget that :).

    php curl sets the Content-Type header as multipart/form-data in case post data is an array and java servlet does not handling the multipart/form-data by using getParameter.I already mentioned in the above article about handling the mutipart/form-data. You are already handling the multipart form-data in your jsp file and thats the reason you hadn’t faced this issue. If there is no special case for file handling then you would face the same issue.