Convert a SoapClient Call to use curl Instead, but Return the Same Response

I have to deal with a SOAP API that doesn’t follow the the standard way of acting like a SOAP API. While 99 percent of the time, I can interact with it using the normal PHP SoapClient, but sometimes, I can’t do that and I have to revert to calling the SOAP method using curl.

It works, it just means I have to do more work than I care to do. One of the issues I dealt with today was that I didn’t want rewrite the method that was processing the SOAP response. The SoapClient returns a stdClass in whatever structure the SOAP API defined in its wsdl. With curl, I get raw XML and I needed to convert it to a stdClass. I did it like this.

$result = $this->curl($xml);
$soap = simplexml_load_string($result);
$response = $soap->children('')
return json_decode(json_encode($response));

The $response object is a SimpleXMLElement object. That’s super useful most of the time, but for, I didn’t want to rewrite the method that processes this response to use SimpleXMLElements. So that’s why I didn’t the json_encode then json_decode. The json_encode doesn’t carry with it specific class information, it’s just an “object.” So then json_decode decodes that into a stdClass, which is what my method was expected.

The $this->curl($xml) is just a method I created when I need to make a curl call instead of SoapClient call. It looks like this.

public function curl($xml, $timeout = 120)
    $soap = curl_init($this->_url);
    curl_setopt_array($soap, array (
        CURLOPT_CONNECTTIMEOUT => $timeout,
        CURLOPT_TIMEOUT => $timeout,

    curl_setopt_array($soap, array (
        CURLOPT_POSTFIELDS => $xml,
        CURLOPT_HTTPHEADER => array (
           'Content-Type: text/xml; charset=utf-8',
            'Content-Length: ' . strlen($xml)

   $response = curl_exec($soap);
   $error = curl_error($soap);
   if (strlen($error) > 0) {
       throw new \SoapFault('SOAP-ENV:Receiver', $error);
   return $response;
Share Button