0

我正在通过 jQuery 通过 .val() 方法收集表单帖子,验证这些帖子并将错误传递回表单输入,或者将 true 传递给调用 PHP cURL 脚本的 .$post 方法。

典型的 var 如下所示:

var industs_servedVal = $('#industs_served').val();

在这种情况下,它是一个选择多个表单字段。我知道 jQuery 中的 .val() 方法传递了一个数组,所以这看起来很合理,我说 var 也会收集数组是对的。

然后我像这样将 var industs_servedVal 传递给 $.post 方法(然后向上滑动感谢信):

$.post('../inc/form_sendsf_modified.php', {
            othervars: othervarsVal;
            industs_served: industs_servedVal,              

        }, function(data) {
            $('#sendEmail').slideUp('slow', 'swing', function() {
                $('#sendEmail').replaceWith('<h3>Thank you!</h3><p>Your message was sent to us. We\'ll get back to you as soon as we can.</p>');
            });
        });
    }
    return false;
});

文件“form_sendSF_modified.php”处理这些帖子并使用 cURL 发送到 Sales Force Cloud。这行得通;但是,问题在于销售人员将“数组”显示为接收到的多字段数组的值,而不是数组值本身。我收集阵列并将其传递给销售人员的方式是否存在问题。如代码所示,foreach 循环是否能够将多个字段数组值以及其他值作为数组发送。

$post_data['00N70000002U2fA'] = $_POST['industs_served']; //Array

//$otherpost data



//cURL CODE for post
//traverse array and prepare data for posting (key1=value1) 
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection to SFDC
$curl_connection = curl_init('https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
//print_r(curl_getinfo($curl_connection));
//echo curl_errno($curl_connection) . '-' .
curl_error($curl_connection);
//close the connection
curl_close($curl_connection);
//End cURL
4

2 回答 2

0

用这个

$post = "ac=on&p=1&pr[]=0&pr[]=1&a[]=3&a[]=4&pl=on&sp[]=3&ct[]=3&s=1&o=0&pp=3&sortBy=date";
parse_str($post,$fields); 

$url = 'http://example.com/';


//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, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

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

//close connection
curl_close($ch);
于 2013-09-10T11:18:34.057 回答
0

您可以使用数组本身(但它会将 Content-Type 标头更改为 multipart/form-data)。

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);

或者您可以使用带有http_build_query函数的构建字符串,参见示例 #3。

于 2013-02-28T05:42:01.537 回答