PHP Sample & Campaign.Create problems solved..

The sample has some inconsistencies in the actual CMBase.php specifically in the array2xml function which doesn't close it's xml tags, replace your current array2xml function with this

/**
* Converts an array to XML. This is the inverse to xml2array(). Values
* are automatically escaped with htmlentities(), so you don't need to escape 
* values ahead of time. If you have, just set the third parameter to false.
* This is an all-or-nothing deal.
*
* @param mixed $arr The associative to convert to an XML fragment
* @param string $indent (Optional) Starting identation of each element
* @param string $escape (Optional) Determines whether or not to escape a text node.
* @return string An XML fragment.
*/
function array2xml( $arr, $indent = '', $escape = true )
{
    $buff = '';
    
    foreach ( $arr as $k => $v )
    {
        if ( !is_array( $v ) )
            $buff .= "$indent<$k>" . ($escape ? utf8_encode( $v ) : $v ) . "</$k>\n";
        else
        {
            /*
            Encountered a list. The primary difference between the two branches is that
            in the 'if' branch, a $k element is generated for each item in $v, whereas
            in the 'else' branch, a single $k element encapsulates $v.
            */
            
            if ( isset( $v[0] ) )
            {
                foreach ( $v as $_k => $_v )
                {
                    if ( is_array( $_v ) )
                         $buff .= "$indent<$k>\n" . array2xml( $_v, $indent . "\t", $escape ) . "$indent</$k>\n";
                    else
                        $buff .= "$indent<$k>" . ($escape ? utf8_encode( $_v ) : $_v ) . "</$k>\n";
                }
            }
            else
                $buff .= "$indent<$k>\n" . array2xml( $v, $indent . "\t", $escape ) . "$indent</$k>\n";
        }
    }
    
    return $buff;
}

Also I found that when putting the parameters through to the Campaign.Create which keep getting the

315: Lists or Segments Required
You must provide either List ID’s or Segments to send to.

If you are using the makeCall function this also references the array2xml function which literally takes the keys and uses the parent key if you don't assign one, so to get around this, you have to scrutinize the xml e.g.

<SubscriberListIDs>
        <string>string</string>
        <string>string</string>
      </SubscriberListIDs>

So to mirror this you need to structure your parameters as follows...

$params = array(
        "ApiKey" => $apikey,
         "ClientID" => $clientid,
        "SubscriberListIDs"=>array("string" => "xxxxx")...

This is obviously assuming that there is only one list id, so completely theroetically I think this will work for multiples

$params = array(
        "ApiKey" => $apikey,
         "ClientID" => $clientid,
        "SubscriberListIDs"=>array("string" => array("xxx", "xxxx"))...

Hope this helps everyone, took me a good couple hours to figure this out as using campaignCreate() didn't work for me either :)

jerrygarciuh, 7 years ago

Hi Ian,

I had a thread on this a while back where CM support staff kept telling me I was wrong and that there was no issue with the code.

I had posted a patch for array2xml  as well.

All of this to say I hope you have better luck than I did with them.

David

jamesd jamesd, 7 years ago

Hi Ian/David,

IanCulshaw :

The sample has some inconsistencies in the actual CMBase.php specifically in the array2xml function which doesn't close it's xml tags, replace your current array2xml function with this...

I'm interested in this, as it's been brought up before but I've never been shown the actual problem in the source. Could you let me know which version of the PHP wrapper you were using when you encountered this problem.

David, you alleged this same problem in this thread but never responded to my final post where I linked to the source in the Google Code repository showing that the array2xml function was correct.

I link to it here again: CMBase.php, revision 22, line 907

Thanks,
James

jamesd jamesd, 6 years ago

Just thought I'd update this topic with a link to the topic which identified the problem alleged above as a bug in Dreamweaver, rather than a bug in the PHP wrapper.

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