如何发布到表单并填充选定的多个内容?

发布于 2024-09-28 09:26:03 字数 975 浏览 3 评论 0原文

我在这里做了一些奇怪的事情,我从本地数据库中查询数据并使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

穿透光 2024-10-05 09:26:03

您试图实现/复制的是非标准(或“实验”,如果您愿意)application/x-www-form-urlencoded 格式。 mime 类型中的“x-”表示它尚未正式标准化。

我遇到的最常见的 application/x-www-form-urlencoded 格式是在HTML 4(可能还有 HTML 5)规范此处。简化后,说明 每个成功的控件已配对包含其名称和当前值,按照其在表单中出现的顺序排列,并用与号“&”分隔。这将转换为包含以下内容的 HTTP GET 或 POST 方法:

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

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:

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.

╭⌒浅淡时光〆 2024-10-05 09:26:03

我想通了。最终结果是这样:

作为“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.

遮了一弯 2024-10-05 09:26:03

$sd['location'] 如果是倍数,则应该是一个数组。例如:

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

但是您需要确保没有将值与标签混淆。 Salesforce 将期望值而不是标签,因此如果 salesforce 是使用以下内容构建的:

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

那么您需要传递键值而不是实际的城市名称。我不知道你的销售人员是什么样子,所以你必须弄清楚:-)

$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 :-)

超可爱的懒熊 2024-10-05 09:26:03

我还尝试将多个选择值发送到 Salesforce,但我的代码看起来略有不同,我不确定如何实施您的解决方案。这实际上是我在网上能找到的唯一解决方案,所以我希望你能帮助我。这就是我的字段数组的样子:

//set POST variables
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';

$fields = array(
'oid'=>urlencode($oid),
'00NE0000003yZ9B'=>urlencode($Lead_Source_Email),
'00NE0000004hd4z'=>urlencode($Personal_Challenges),
'recordType'=>urlencode($recordType),
'retURL'=>urlencode($retURL),
'first_name'=>urlencode($first_name),
'last_name'=>urlencode($last_name),
'email'=>urlencode($email),
'phone'=>urlencode($phone),
//'00NE0000003uZ4G'=>urlencode($Contact_By),
'00NE0000003uZ9X'=>urlencode($Message)
);

$Personal_Challenges 是具有多个值的选择框中的字段。老实说,我不理解所有代码,但如下:

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//print_r($fields_string);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

我很乐意您能给我任何帮助!很抱歉在回答中提出这个问题,但显然我无法直接在 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:

//set POST variables
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';

$fields = array(
'oid'=>urlencode($oid),
'00NE0000003yZ9B'=>urlencode($Lead_Source_Email),
'00NE0000004hd4z'=>urlencode($Personal_Challenges),
'recordType'=>urlencode($recordType),
'retURL'=>urlencode($retURL),
'first_name'=>urlencode($first_name),
'last_name'=>urlencode($last_name),
'email'=>urlencode($email),
'phone'=>urlencode($phone),
//'00NE0000003uZ4G'=>urlencode($Contact_By),
'00NE0000003uZ9X'=>urlencode($Message)
);

$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:

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//print_r($fields_string);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

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!

圈圈圆圆圈圈 2024-10-05 09:26:03

我看到这篇文章太旧了,但以防万一:

我们必须为其准备字符串

curl_setopt($ch, CURLOPT_POSTFIELDS, $str_fields);

我们可以准备数组,例如:

$fields = array('f1' => 'v1', f2 =>['v21', 'v22', 'v23'], 'f3' =>'v3' );
$fields = http_build_query($fields);

它给出:f1=v1&f2%5B0%5D=v21&f2%5B1%5D=v22&f2 %5B2%5D=v23&f3=v3
我们需要清除像“%5B0%5D”这样的字符

$str_fields=preg_replace("/(%5[A-F]\d{0,})/" ,"",$fields);

我们得到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

curl_setopt($ch, CURLOPT_POSTFIELDS, $str_fields);

We can prepare array like:

$fields = array('f1' => 'v1', f2 =>['v21', 'v22', 'v23'], 'f3' =>'v3' );
$fields = http_build_query($fields);

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"

$str_fields=preg_replace("/(%5[A-F]\d{0,})/" ,"",$fields);

We get f1=v1&f2=v21&f2=v22&f2=v23&f3=v3

Thats all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文