cURL不使用POST数据

时间:2012-12-17 20:42:49

标签: php debugging 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;

}

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:3)

CURLOPT_POSTFILEDS需要urlencoded字符串或数组作为param。阅读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;
}

答案 2 :(得分: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));
}