Satu Sultana
Satu Sultana

Reputation: 537

CURL command for xml unable to send via php script

I have used this linux command to send xml CURL request

curl -f -d 
           '<REQ>
               <FEATURE>COMMON</FEATURE>
               <TIME-STAMP>ddmmyyyy HH:MM:SS</TIME-STAMP>
               <OPERATION-CODE>DEACTIVATE</OPERATION-CODE>
               <CLIENT-TRANS-ID>3256778901</CLIENT-TRANS-ID>
               <BODY>
                  <DATASET>
                     <PARAM>
                        <NAME>PRODUCT-CODE</NAME>
                        <VALUE>T-1234</VALUE>
                     </PARAM>
                     <PARAM>
                        <NAME>CP-ID</NAME>
                        <VALUE>111</VALUE>
                     </PARAM>
                     <PARAM>
                        <NAME>CHANNEL-ID</NAME>
                        <VALUE>WAP</VALUE>
                     </PARAM>
                     <ADDITIONAL-PARAM>
                        <PARAM>
                           <NAME>LANGUAGE</NAME>
                           <VALUE>1</VALUE>
                        </PARAM>
                        <PARAM>
                           <NAME>KEYWORD</NAME>
                           <VALUE>LS</VALUE>
                        </PARAM>
                     </ADDITIONAL-PARAM>
                  </DATASET>
               </BODY>
            </REQ>' 
           'http://192.168.xx.xxx:8088/HttpAdapter'

It's working fine.

But when I have tried using PHPI am getting Error

$input_xml = "<REQ>
               <FEATURE>COMMON</FEATURE>
               <TIME-STAMP>ddmmyyyy HH:MM:SS</TIME-STAMP>
               <OPERATION-CODE>DEACTIVATE</OPERATION-CODE>
               <CLIENT-TRANS-ID>3256778901</CLIENT-TRANS-ID>
               <BODY>
                  <DATASET>
                     <PARAM>
                        <NAME>PRODUCT-CODE</NAME>
                        <VALUE>T-1234</VALUE>
                     </PARAM>
                     <PARAM>
                        <NAME>CP-ID</NAME>
                        <VALUE>111</VALUE>
                     </PARAM>
                     <PARAM>
                        <NAME>CHANNEL-ID</NAME>
                        <VALUE>WAP</VALUE>
                     </PARAM>
                     <ADDITIONAL-PARAM>
                        <PARAM>
                           <NAME>LANGUAGE</NAME>
                           <VALUE>1</VALUE>
                        </PARAM>
                        <PARAM>
                           <NAME>KEYWORD</NAME>
                           <VALUE>ls</VALUE>
                        </PARAM>
                     </ADDITIONAL-PARAM>
                  </DATASET>
               </BODY>
            </REQ>
            ";

            $url = "http://192.168.xx.xxx:8088/HttpAdapter";

            //setting the curl parameters.
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: application/xml',
                                            'Connection: Keep-Alive'
                                            ));
            curl_setopt($ch, CURLOPT_URL, $url);
            // Following line is compulsary to add as it is:
            curl_setopt($ch, CURLOPT_POSTFIELDS,
                        "xmlRequest=" . $input_xml);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
            $data = curl_exec($ch);
            curl_close($ch);

            //convert the XML result into array
            $array_data = json_decode(json_encode(simplexml_load_string($data)), true);

            print_r('<pre>');
            print_r($array_data);
            print_r('</pre>');

In this time I am getting "5689 featureID not sent with the request". Fault code is 1003. I am not getting any error in my PHP code. How can I send this xml request using php CURL.

Upvotes: 0

Views: 38

Answers (1)

hanshenrik
hanshenrik

Reputation: 21463

your php code prepends xmlRequest= to the POST body, your curl code doesn't. just replace

curl_setopt($ch, CURLOPT_POSTFIELDS, "xmlRequest=" . $input_xml);

with

curl_setopt($ch, CURLOPT_POSTFIELDS, $input_xml);

and the requests should be roughly similar (not exactly similar tho, for example, the commandline curl has a default User-Agent looking like User-Agent: curl/7.63.0, while libcurl, and by extension, php's curl_* api, does not have a default user-agent, but you can set one with CURLOPT_USERAGENT)

.. also neither of these examples should work, because you're sending the wrong Content-Type header in both cases, you're sending

Content-Type: application/x-www-form-urlencoded

but the correct type should be:

Content-Type: application/xml

.. i guess the server just ignores the Content-Type header, since your curl invocation actually works. unless explicitly documented, that's probably a bug, you should probably let the server developer know.

Upvotes: 2

Related Questions