2011-06-22 72 views
2

好吧,我有一個捲曲請求,我正在努力。如何將數組添加到URL或發送數組?

$ch = curl_init(); 
// set URL and other appropriate options 
curl_setopt($ch, CURLOPT_URL, "http://someurl.com/shop_pos/index.php?route=module/cart/ajax_get_all_products"); 

// Do a POST 
$items = 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $_SESSION['cart']); 
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
$result = curl_exec($ch); 
curl_close($ch); 
echo $result; 

內容或$_SESSION['cart']

Array 
(
    [155] => 1 
    [78] => 1 
) 

我需要發送這個數據無論是作爲發佈數據或在捕捉功能得到變量...任何建議

public function ajax_get_all_products(){ 
    $this->language->load('module/cart'); 
    $all_products = $this->cart->getProducts(); 
    $data_returned = json_encode($all_products); 
    echo "<pre>".print_r($_REQUEST, true)."</pre>"; 
    echo "<pre>".print_r($_POST, true)."</pre>"; 
    $this->response->setOutput($data_returned, $this->config->get('config_compression')); 
} 

我沒有得到我設置的數組。這兩個文件是在同一臺服務器上BTW

+0

你有cURL腳本文件中的session_start()嗎? – kaese

+0

是的,我做session_set_cookie_params(0,'/'); session_start(); – Trace

回答

3

嘗試:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('items' => $_SESSION['cart'])); 

試試這個:

$post_data = http_build_query(array('items' => $_SESSION['cart'])); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
+0

現在我得到陣列 ( [項目] =>數組 ) – Trace

+0

所以我嘗試curl_setopt($ CH,CURLOPT_POSTFIELDS,陣列( '項目'=>內爆( 「」,$ _SESSION [ '購物']))) ; 但它只返回[項目] => 1,1這是我需要的一半 – Trace

+0

@Alex更新了答案請檢查 –

0

試試這個

$post = ""; 
    $amp = ""; 
    foreach($_SESSION['cart'] as $key => $value) { 
     $post .= $amp . $key . "=" . urlencode($value); 
     $amp = "&";       
    } 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
+0

這也很好 – Trace