简体   繁体   中英

How to pass a nested array using php cURL

I am collecting form posts via jQuery via the.val() method, validating those posts and passing errors back to the form inputs, or passing true to the .$post method which invokes a PHP cURL script.

A typical var will look like this:

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

In this case it is a select multiple form field. I understand that the .val() method in jQuery passes an array, so that seems reasonable, and am I right in saying that the var will also collect the array.

I then pass var industs_servedVal to the $.post method like this ( then slide up a thank you note):

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

The file "form_sendSF_modified.php" handles those posts and sends to the Sales Force Cloud using cURL. This works; however, the problem is that Sales Force shows "array" as the values received for the multiple field array, not the array values themselves . Is there a problem in the way I am collecting the array and passing it to sales force. Is the foreach loop capable of sending the multiple field array values as well as the other values as an array as shown in the code.

$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

You can use array itself (but it will change Content-Type header to multipart/form-data).

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);

Or you can use build string with http_build_query function, look example #3.

use this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM