Question about the PHP Sample Code

For ages I've told myself I would get all SOAPy and learn how to build "real" php applications but I've never quite gotten around to it. As I started playing with your php samples, I once again gravitated back to the familiar feeling of a good old CURL request.

Within minutes I added someone to my list and was feeling pretty good about myself... until I tried to do something very simple.

Parse the return xml.

The example code you give (subscribewithcustomviapost(curl).php) is great, but I'm not sure how to correctly work with the resulting SOAP XML. How do you handle error checking? How do I get the return Code? I've tried using SimpleXMLElement but for some reason I get what appears to be an empty object.

Please put me out of my misery. I'm just looking for the correct way to take the $postResult and get the "Code" and the "Message" out. I could do an ugly hack and just use a regex, but that would be... well... ugly. There's got to be a better way.

Thanks!

Jason Jason, 9 years ago

Hi Luke,

Unfortunately, we haven't got any PHP developers in house, so I can't think of a quick and easy way to do this off hand. However, going on the tool you mentioned I was able to come up with the following which does get the Message from the XML string (and can be tweaked to get the code)

$xml = new SimpleXMLElement($postResult);


foreach($xml->xpath('//soap:Body/*/*/') as $x)
{
   echo $x->Message;
}

There is obviously a much more elegant way to do this, but I don't know what it is! Any PHP devs out there who can shed some light???

Luke Stokes, 9 years ago

Well, that would be nice... but SimpleXMLElement is returning an empty object. This is where the whole SOAP thing gets me confused.

Here's what $postResult has:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Subscriber.AddWithCustomFieldsResponse xmlns="http://app.campaignmonitor.com/api/">
<Subscriber.AddWithCustomFieldsResult>
<Code>0</Code>
<Message>Success</Message>
</Subscriber.AddWithCustomFieldsResult>
</Subscriber.AddWithCustomFieldsResponse>
</soap:Body>
</soap:Envelope>

But when I toss that to SimpleXMLElement and do a var_dump on the result, I just get an empty object:

object(SimpleXMLElement)#25 (0) {
}

Any ideas?

At this point I'm thinking I'll just regex the result which just seems wrong to me. SimpleXMLElement has worked fine for me before. Does the soap envelope change things? Is there a special way to deal with it?

BTW, you guys have a way cool company. I read through some blogs today and enjoyed it. Have fun on the ping pong table.

Luke Stokes, 9 years ago

Well, I don't like it, but it works. Until someone can teach me how to properly handle a SOAP xml result, this is what I'll use:

$postResult = curl_exec($ch);
$isError = false;
if (!curl_error($ch)) {
    if (eregi("(.*\<Code\>)(.*)(\<\/Code\>\<Message\>)(.*)(\<\/Message\>.*)",$postResult,$matches)) {
        $resultCode = $matches[2];
        $resultMessage = $matches[4];
    } else {
        $isError = true;
        $resultCode = "none";
        $resultMessage = "Result did not match our regex.";
    }
    if ($resultCode <> "0") {
        $isError = true;
    }
} else {
    $resultCode = curl_errno($ch);
    $resultMessage = curl_error($ch);
    $isError = true;
}

Hope that helps someone. If someone gets a better solution, please hit me up with it.

Thanks!

Jason Jason, 9 years ago

Nice one Luke. The snippet I posted earlier using the SimpleXMLElement does work, as I tested it in the sample we provide - however I am sure there is a better way to do it than with the foreach loop (I just hacked an example). But when use your code to dump the whole variable, I get the empty result. Beats me.

If there is anyone who can expand on either of the techniques in the thread, do post up your code.

And it's true, it is way cool working here.. :)

Join 200,000 companies around the world that use Campaign Monitor to run email marketing campaigns that deliver results for their business.

Get started for free
1-888-533-8098