【问题标题】:php curl POST cookie complicationsphp curl POST cookie并发症
【发布时间】:2013-12-27 17:30:57
【问题描述】:

我正在使用 php cURL 来处理登录网站的 POST 请求。有没有办法确定登录失败的时间?我通过使用正确的凭据成功登录进行了实验,$ch 返回 true,但当我使用不正确的凭据时,$ch 也返回 true。我知道当凭据不正确时(通过浏览器),它会将我重定向回主登录页面。

有什么想法吗?

     function httpPost($url, $params)

   {
       try {
           //open connection
           $ch = curl_init($url);

           //set the url, number of POST vars, POST data
           // curl_setopt($ch, CURLOPT_COOKIEJAR, "cookieFileName");
           curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
           curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
           curl_setopt($ch, CURLOPT_HEADER, true);
           curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, false); 
           curl_setopt($ch, CURLOPT_POST, 1);
           curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
           curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

           //execute post
           $response = curl_exec($ch);

           extract($_POST);
           echo $response;

           //close connection
           curl_close($ch);

           if (FALSE === $ch)
               throw new Exception(curl_error($ch), curl_errno($ch));

           // ...process $ch now
       }
       catch(Exception $e) {

           trigger_error(sprintf(
               'Curl failed with error #%d: %s',
               $e->getCode(), $e->getMessage()),
               E_USER_ERROR);

       }
   }

【问题讨论】:

    标签: php visual-studio-2012 curl http-post


    【解决方案1】:

    登录后检查页面中的注销内容。大多数情况下,页面会显示注销选项或类似的东西。因此,在 html 源代码中可能是这样的:

    >Logout<
    >Signout<
    <a href="http://example.com/logout"
    

    【讨论】:

      【解决方案2】:

      不要在你的实际 curl 连接中搜索真/假,而是在结果中搜索一个你知道如果成功就会出现的字符串:

      if (strpos($result,'Welcome') ) {
       ...
      }
      

      【讨论】:

      • 这是我的第一个想法,但据我所知,只有 GET 请求返回一些我实际上可以询问的内容。 $response 只是有一个 true 或 false 值。我还有其他方法可以从我的帖子请求中访问实际的 html 响应吗?
      • post 仍应为您提供 $result 中的页面结果...如果网络服务器不允许您作为蜘蛛,您可能需要设置用户代理等。
      【解决方案3】:

      原来我没有收到回复,因为我不小心输入了 $curl 而不是 $ch。 去看看。

      谢谢大家,离开然后回来就成功了。

      【讨论】:

        猜你喜欢
        • 2012-01-18
        • 2013-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-05
        • 1970-01-01
        • 1970-01-01
        • 2011-04-12
        相关资源
        最近更新 更多