使用cURL将$ _POST值传递给外部服务器

时间:2012-12-13 07:38:33

标签: php post curl

  

可能重复:
  Passing $_POST values with cURL

我正在尝试使用curl将表单发送到itermediary验证页面的$ _POST值传递到另一台服务器进行处理......然后返回我们的服务器以显示成功页面。

这就是我所拥有的:

    unset($_POST['recaptcha_challenge_field']);
    unset($_POST['recaptcha_response_field']);
    $params = $_POST;
    foreach ( $params as $key => $value) {
      $post_items[] = $key . '=' . $value;
    }
    $post_string = implode ('&', $post_items);
    $curl_connection =
    curl_init('http://server.somedomain.net/about/spit.php');
    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, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
    $result = curl_exec($curl_connection);
    print_r(curl_getinfo($curl_connection));
    echo curl_errno($curl_connection) . '-' .
    curl_error($curl_connection);
    //close the connection
    curl_close($curl_connection);

数据似乎存在且格式正确,但据我所知,它不会发送到服务器。知道这有什么问题吗?我有点困惑。

戴夫

1 个答案:

答案 0 :(得分:1)

你忘了:

curl_setopt($curl_connection, CURLOPT_POST, 1);

并改变:

curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);

curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, false);

  

在PHP 5.2+ POSTFIELDS必须是一个数组!

http://php.net/manual/en/function.curl-setopt.php#example-4025