如何发布到表单并填充选定的多个内容?
我在这里做了一些奇怪的事情,我从本地数据库中查询数据并使用 cURL 将其发送到 Salesforce 表单。数据正确发布到 Salesforce。但是,选择倍数没有选择正确的值。请参阅下面我的代码中的 $sd["location"]:
//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);
选择多个已在 Salesforce 中设置为具有 30 个不同的位置。我试图通过应该选择的城市(芝加哥、纽约、加利福尼亚)。您能帮我修复我的代码以使其正常工作吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您试图实现/复制的是非标准(或“实验”,如果您愿意)application/x-www-form-urlencoded 格式。 mime 类型中的“x-”表示它尚未正式标准化。
我遇到的最常见的 application/x-www-form-urlencoded 格式是在HTML 4(可能还有 HTML 5)规范此处。简化后,说明 每个成功的控件已配对包含其名称和当前值,按照其在表单中出现的顺序排列,并用与号“&”分隔。这将转换为包含以下内容的 HTTP GET 或 POST 方法:
You should also use PHP's urlencode function on form名称和值,然后将其传递到 HTTP 请求的适当位置,以符合 RFC1738。
顺便说一句,PHP 虽然可能不是唯一的,但它在处理单个 post var 上的多个值的方式上是一个奇怪的球。它遵循 HTML 建议的规范,但将其扩展为仅将 var 视为多个,如果其名称包含 PHP 数组附加运算符,例如,
formname[]
。如果您尝试在 PHP 脚本上遵循 HTML 建议(见下文),则在通过$_POST
或$_GET
超全局访问时,它将覆盖名称上的先前值。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:
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.我想通了。最终结果是这样:
作为“value1;value2”发送不起作用。即 implode(";", $array) 或 join() 不起作用。
奇怪的是,必须在 POST 字符串中拆分这样的值:
$post_string .= "&the_field=value1
$the_field=值2
$the_field=value3";
现在工作正常。
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.
$sd['location']
如果是倍数,则应该是一个数组。例如:但是您需要确保没有将值与标签混淆。 Salesforce 将期望值而不是标签,因此如果 salesforce 是使用以下内容构建的:
那么您需要传递键值而不是实际的城市名称。我不知道你的销售人员是什么样子,所以你必须弄清楚:-)
$sd['location']
should be an array if its a multiple. For example: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:
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 :-)
我还尝试将多个选择值发送到 Salesforce,但我的代码看起来略有不同,我不确定如何实施您的解决方案。这实际上是我在网上能找到的唯一解决方案,所以我希望你能帮助我。这就是我的字段数组的样子:
$Personal_Challenges
是具有多个值的选择框中的字段。老实说,我不理解所有代码,但如下:我很乐意您能给我任何帮助!很抱歉在回答中提出这个问题,但显然我无法直接在 StackOverflow 上发消息。
干杯!
I'm also trying to send multiple select values to Salesforce, but my code looks slightly different and I'm not sure how to implement your solution. It's literally the only solution I can find on the net, so I'm hoping you can help me out. This is what my fields array looks like:
$Personal_Challenges
is the field from the select box with the multiple values. I honestly don't understand all of the code, but this is what follows:I'd love any help you can give me! Sorry to ask this question in an answer, but apparently I can't directly message on StackOverflow.
Cheers!
我看到这篇文章太旧了,但以防万一:
我们必须为其准备字符串
我们可以准备数组,例如:
它给出:f1=v1&f2%5B0%5D=v21&f2%5B1%5D=v22&f2 %5B2%5D=v23&f3=v3
我们需要清除像“%5B0%5D”这样的字符
我们得到f1=v1&f2=v21&f2=v22&f2=v23&f3=v3
就这样。
I see this post is too old, but just in case:
we have to prepare string for it
We can prepare array like:
it gives : f1=v1&f2%5B0%5D=v21&f2%5B1%5D=v22&f2%5B2%5D=v23&f3=v3
we need to clear chars like "%5B0%5D"
We get f1=v1&f2=v21&f2=v22&f2=v23&f3=v3
Thats all.