2012-12-17 34 views
0

我想調試這個,但我沒有運氣。我是否正確發送POST數據?cURL不能使用POST數據

if (isset($_POST['chrisBox'])) { 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, "http://www.associates.com/send-email-orders.php"); 
curl_setopt($curl, CURLOPT_POST, TRUE); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $_POST['chrisBox']); 
curl_setopt($curl, CURLOPT_HEADER, FALSE); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, FALSE); 
curl_setopt($curl, CURLOPT_VERBOSE, TRUE); 
$ex = curl_exec($curl); 
echo 'email'; 
$email = true; 

} 

回答

2

CURLOPT_POSTFILEDS需要urlencoded字符串或數組作爲參數。閱讀PHP Manual curl_setopt。改變了你的例子,現在它使用了urlencoded字符串。

if (isset($_POST['chrisBox'])) { 

    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, "http://www.associates.com/send-email-orders.php"); 
    curl_setopt($curl, CURLOPT_POST, TRUE); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, 'chrisBox=' . urlencode($_POST['chrisBox'])); 
    curl_setopt($curl, CURLOPT_HEADER, FALSE); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, FALSE); 
    curl_setopt($curl, CURLOPT_VERBOSE, TRUE); 
    $ex = curl_exec($curl); 
    echo 'email'; 
    $email = true; 
} 
+0

這也會起作用。謝謝。 – wowzuzz

0
$ex = curl_exec($process); 
if ($ex === false) 
{ 
    // throw new Exception('Curl error: ' . @curl_error($process)); 
    // this will give you more info 
    var_dump(curl_error($process)); 
} 
7

$_POST請求一起發送的參數需要在形式 -

key=value&foo=bar 

您可以使用PHP的http-build-query功能這一點。它將從數組中創建一個查詢字符串。

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($_POST)); 

如果您只想傳遞一個參數,您仍然需要將其包裝在數組或對象中。

$params = array(
    'stack'=>'overflow' 
); 

http_build_query($params);  // stack=overflow 
+0

我在想,因爲它是一個表格的一部分,它已經被編碼了。上面的這個函數編碼了鍵 - >值關係。對? – wowzuzz

+0

@哇 - 是的。它創建一個* URL編碼*查詢字符串。任何非法的URL字符都將被轉義。 – Lix