使用curl将表单提交到外部网页

时间:2011-06-13 03:14:08

标签: php forms post curl

我需要一些帮助。

我正在尝试将$ _POST转换为外部网页的表单。 这个想法是下一个:

  • 用户进入我的网页。 www.caae.cl/encuesta
  • 用户输入用户名和密码(进入我的网页)
  • 我提交到外部网页的表单( https ://www.uc.cl),我没有控制权,用户和pw
  • 我检查组合是否正确
  • 我将用户重定向到我的网页用户主页

我有两点:首先我如何通过cUrl和第二个提交,我需要阅读外部网页的cookie以检查用户密码组合是否正常

我将不胜感激任何帮助。我正在使用php和curl。

1 个答案:

答案 0 :(得分:2)

阅读cURL here的文档。它有可用于POST数据,cookie和HTTPS的选项。

但是,为了帮助你,这是我用来发出cURL请求的函数。您需要根据自己的目的修改选项,例如您想要启用HTTPs选项,您可能不想存储cookie。请阅读文档!

function curl_request($url, $referer = false, $postdata = false, $new_session = false) //single custom cURL request.
{
    $ch = curl_init();
    if ($new_session)
    {
        curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    }
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     

    curl_setopt($ch, CURLOPT_URL, $url);

    if ($referer)
    {
        curl_setopt($ch, CURLOPT_REFERER, $referer); 
    }

    if ($postdata)
    {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);   
    }

    $response = curl_exec($ch);

    curl_close($ch);

    return $response;
}

此外,如果需要将cookie变量转换为php变量,请按照answer here进行操作。请仔细考虑检查cookie是否是检查登录的最佳方式。

相关问题