【问题标题】:How to pass a nested array using php cURL如何使用 php cURL 传递嵌套数组
【发布时间】:2012-07-01 01:35:22
【问题描述】:

我正在通过 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。这行得通;但是,问题在于,Sales Force 将“数组”显示为接收到的多字段数组的值,而不是数组值本身。我收集阵列并将其传递给销售人员的方式是否存在问题。如代码所示,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

【问题讨论】:

    标签: php jquery curl salesforce


    【解决方案1】:

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

    curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);
    

    或者你可以使用带有http_build_query函数的构建字符串,看例子#3。

    【讨论】:

      【解决方案2】:

      使用这个

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

      【讨论】:

      • 您好,欢迎来到 SO,在提供答案时,最好同时附上解释。
      猜你喜欢
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 2019-01-07
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      相关资源
      最近更新 更多