Embed XML in JSON POST to PHP-CURL -
i have requirement post rest call in following json formt. note payload variable xml. i'm using php 5 curl. looking i'm doing wrong. thanks
{ "processdefid":"testing~simpleapplication!1.0~simpleprocess", "servicename":"simpleprocess.service", "operation":"start", "payload":"<payload><formarg><p0:webform1 xmlns:p0=\"http://www.frevvo.com/schemas/_n5j6ibuoeewgf4k8gssana\"/></formarg></payload>", "action":"submit" }
constructed following not getting response nor seeing actioned on called application.
try { echo '<br>curl start<br>'; echo file_put_contents($log, "start \n", file_append); $curl = curl_init(); curl_setopt($curl, curlopt_url,"http://192.168.56.151:7003/bpm/api/3.0/processes"); curl_setopt($curl, curlopt_returntransfer, true); curl_setopt($curl, curlopt_post, true); curl_setopt($curl, curlopt_failonerror); curl_setopt($curl, curlopt_httpheader, array('content-type: application/json') ); curl_setopt($curl, curlopt_httpheader, array('accept: application/json') ); curl_setopt($curl, curlopt_userpwd, "jstein:welcome1"); $xml = '<ns1:start xmlns:ns1=\http://xmlns.oracle.com/bpm/webform/formsdata/requesttravelaccount\">'; $xml .= '<formarg xmlns:ns2=\"http://www.frevvo.com/schemas/_1g73gmt3eewsksy3n_19_q\">'; $xml .= '<ns2:requesttravelaccount>'; $xml .= '<employeeid>1771</employeeid>'; $xml .= '<firstname>aaron</firstname>'; $xml .= '<lastname>thompson</lastname>'; $xml .= '<personid>300000087953021</personid>'; $xml .= '</ns2:requesttravelaccount>'; $xml .= '</formarg>'; $xml .= '</ns1:start>'; $xml_e = htmlspecialchars($xml, ent_quotes); echo file_put_contents($log, $xml_e ."\n", file_append); $post_data = array( "processdefid"=>"testing~hcm!1.0~newemployeetravelsetupprocess", "servicename"=>"newemployeetravelsetupprocess.service", "operation"=>"start", "action"=>"submit", "payload" => $xml_e ); $curl_post_data = json_encode($post_data); echo file_put_contents($log, "$curl_post_data"."\n", file_append); curl_setopt($curl, curlopt_postfields, $curl_post_data); $curl_response = curl_exec($curl); echo '<br> curl_exec return: ' . curl_error($curl); } catch (exception $e) { echo 'caught exception" ',$e->getmessage(), "\n"; } curl_close($curl); echo '<br>curl done';
you initializing curlopt_httpheader
option twice. whereas need initialize once headers.
wrong:
curl_setopt($curl, curlopt_httpheader, array('content-type: application/json') ); curl_setopt($curl, curlopt_httpheader, array('accept: application/json') ); // resetting above 1 line.
correct:
curl_setopt($curl, curlopt_httpheader, array('content-type: application/json', 'accept: application/json') );
Comments
Post a Comment