views:

139

answers:

3

I'm doing something a bit strange here, I'm querying data out of my local database and posintg it to Salesforce form using cURL. The data posts correctly to Salesforce. However, the select multiple is not getting the correct values selected. See $sd["location"] below in my code:

    //init curl
    $ch = curl_init();

    //setup the params
    $url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
    $oid = "HIDDEN";

    //setup the superdelegate array
    $sd = array();
    $sd["oid"] = $oid;
    $sd["retURL"] = "";  
    $sd["first_name"] = "1144asdfsadf4"; 
    $sd["last_name"] = "SDFSD1111";
    $sd["state"] = "IL";
    $sd["location"] = '"Chicago","New York","California"'; //this is the value that submits to the select multiple

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($sd));

    //post to Salesforce and then close the connection 
    curl_exec($ch); 
    curl_close($ch);

The select multiple is already setup in Salesforce with a 30 different locations. I'm trying to pass the cities that should be selected (Chicago, New York, California). Can you help me fix my code to get this to work correctly?

A: 

$sd['location'] should be an array if its a multiple. For example:

$location = str_replace('"', '', '"Chicago","New York","California"');
$sd['location'] = explode(',', $location);

However you need to make sure you are not confusing the values with the labels. Salesforce will be expecting the value not the label so if salesforce is built with something like:

1 = Antwerp
2 = California
...
29 = Whatever

Then you need to pass the key values not the actual city names. I dunno what your salesforce stuff looks like so youll have to figure that out :-)

prodigitalson
Well, I gave that a shot and none of the locations were selected in Salesforce. The labels and values are exactly the same, so that shouldn't be the issue. Hmmm.... Any other ideas?
mike
try using a hash instead of a numeric array maybe so with the above code you could use `$sd['location'] = array_combine($sd['location'], $sd['location']);` at the end.
prodigitalson
+1  A: 

I figured it out. This ended up doing it:

Sending as "value1;value2" doesn't work. i.e. implode(";", $array) or join() doesn't work.

Had to split the values like this in the POST string, oddly:

$post_string .= "&the_field=value1 $the_field=value2 $the_field=value3";

Now works fine.

mike
+6  A: 

What you are trying to implement/duplicate is the non-standard (or "experimental" if you prefer) application/x-www-form-urlencoded format. The "x-" in the mime type denotes the fact that it is has not been officially standardized.

The most common application/x-www-form-urlencoded format I've encountered is as specified in the HTML 4 (and probably HTML 5) specification here. Which, when simplified, states that each successful control is paired with it's name and current value, in the order it appears in the form, and separated by the ampersand, "&". This would translate into a HTTP GET or POST method containing something like:

name=test&desc=some%20description&option=1&option=2&option=3

You should also use PHP's urlencode function on form names and values before passing it along in the appropriate location of the HTTP request in order to conform with RFC1738.

As a side note, PHP, though probably not alone, is an odd-ball in how it handles multiple values on a single post var. It follows the HTML suggested specification, but extends it to only consider a var as multiple if its name contains the PHP array append operator, e.g., formname[]. If you try to follow the HTML suggestion (see below) on a PHP script, it will overwrite the previous value on the name when accessing via the $_POST or $_GET superglobals.

Kevin Peno