title: Send xml request using PHP curl
body: <p>I have used PHP curl to send XML request to webservice and and get response. My code is as follows.</p>
  
  <pre><code>$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-&gt;getMessage();die("Error");
      }
  </code></pre>
  
  <p>I'm getting only this error from third party webservice.They are saying way of requesting may be invalid and XML code is ok.</p>
  
  <pre><code>"XML load failed. [Invalid at the top level of the document.]"
  </code></pre>
  
  <p>But my questions are;</p>
  
  <ol>
  <li><p>Does this code correct when requests using XML?</p>
  
  <p>Eg. <code>curl_setopt($ch, CURLOPT_POSTFIELDS,  urlencode($xmlRequest));</code></p></li>
  <li><p>There is no post field variable to set up when setting post fields.</p>
  
  <p>Eg. <code>curl_setopt($ch, CURLOPT_POSTFIELDS, "xmlRequest=" . $xmlRequest);</code></p></li>
  </ol>
  
  <p>Thanks.</p>