【问题标题】:submit form into external webpage with curl使用 curl 将表单提交到外部网页
【发布时间】:2011-06-13 03:14:07
【问题描述】:

我需要一些帮助。

我正在尝试将 $_POST 制作成外部网页的表单。 想法是下一个:

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

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

我将不胜感激。我正在使用 php 和 curl。

【问题讨论】:

    标签: php forms post curl


    【解决方案1】:

    阅读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 是否是检查登录的最佳方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      相关资源
      最近更新 更多