Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send xml request using PHP curl

I have used PHP curl to send XML request to webservice and and get response. My code is as follows.

$url = "https://path_to_service.asp";
try{
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_POSTFIELDS,  urlencode($xmlRequest));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_VERBOSE, 0);
            $data = curl_exec($ch);

            //convert the XML result into array
            if($data === false){
                $error = curl_error($ch);
                echo $error; 
                die('error occured');
            }else{

                $data = json_decode(json_encode(simplexml_load_string($data)), true);  
            }
            curl_close($ch);

        }catch(Exception  $e){
            echo 'Message: ' .$e->getMessage();die("Error");
    }

I'm getting only this error from third party webservice.They are saying way of requesting may be invalid and XML code is ok.

"XML load failed. [Invalid at the top level of the document.]"

But my questions are;

  1. Does this code correct when requests using XML?

    Eg. curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($xmlRequest));

  2. There is no post field variable to set up when setting post fields.

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

Thanks.

like image 904
cha Avatar asked Nov 25 '13 05:11

cha


People also ask

How to send XML using Curl?

To send XML to the server using Curl, you need to pass the XML data to Curl with the -d command line option and specify the data type in the body of the POST message using the -H "Content-Type: application/xml" command-line option.

How do I send an XML request?

If you want to send XML data to the server, set the Request Header correctly to be read by the sever as XML. xmlhttp. setRequestHeader('Content-Type', 'text/xml'); Use the send() method to send the request, along with any XML data.


1 Answers

I'm sharing my solution with others that will be helpful for others.

$url = "https://path_to_service.asp";

//setting the curl headers
$headers = array(
    "Content-type: text/xml;charset=\"utf-8\"",
    "Accept: text/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    "SOAPAction: \"run\""
);

try{

    $ch = curl_init();

    //setting the curl options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS,  $xmlRequest);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $data = curl_exec($ch);

    //convert the XML result into array
    if($data === false){
        $error = curl_error($ch);
        echo $error;
        die('error occured');
    }else{
        $data = json_decode(json_encode(simplexml_load_string($data)), true);
    }

    curl_close($ch);

}catch(Exception  $e){
    echo 'Message: '.$e->getMessage();
    die("Error");
}

Thanks.

like image 192
cha Avatar answered Oct 02 '22 16:10

cha