2012-07-01 69 views
0

我正在通過jQuery通過.val()方法收集表單帖子,驗證這些帖子並將錯誤傳遞迴表單輸入,或將true傳遞給。$ post方法,該方法調用一個PHP cURL腳本。如何通過使用PHP傳遞嵌套數組cURL

一個典型的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顯示「數組」是多字段數組接收的值,而不是數組值本身。我收集數組並將其傳遞給銷售人員的方式有問題嗎? 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 

回答

0

您可以使用數組本身(但它會將Content-Type標題更改爲multipart/form-data)。

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data); 

或者你可以使用構建字符串與http_build_query函數,看看例子#3。

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); 
+0

您好,歡迎來SO,提供答案的時候,它的偉大實踐中也有說明伴隨着它。 – Fluffeh