dongyuelian9602 2015-12-22 22:47
浏览 125
已采纳

PHP Curl登录https for Esse3

I'm trying to do a login in a Esse3 Platform with Curl PHP. Here the snippet:

<?
 function get_jsessionid($url){
  $ch = curl_init($url);
  print_r($ch);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  $result=curl_exec ($ch);
  preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
  $cookies = array();
  foreach($matches[1] as $item) {
   parse_str($item, $cookie);
   $cookies = array_merge($cookies, $cookie);
  }
  return $cookies['JSESSIONID'];
 }

  $username='';
  $password='';
  $baseurl='https://webstudenti.unica.it/esse3/Home.do';
  $loginurl= 'https://webstudenti.unica.it/esse3/auth/Logon.do;jsessionid=' . get_jsessionid($baseurl);

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$loginurl);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
  $result=curl_exec ($ch);
  curl_close ($ch);

  print_r($result);

Obviously now I've deleted my credentials, but the code doesn't work. I've tried a lot of time with differents snippet but login doesn't go. Do you have any solution? Ps: code works, but the remote webapp give me a login error.

  • 写回答

1条回答 默认 最新

  • doubu8643 2015-12-27 21:59
    关注
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://webstudenti.unica.it/esse3/Home.do');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    $response = curl_exec($ch);
    
    preg_match("/Set-cookie: (.*)
    /iU", $response, $matches);
    $cookie = trim(substr($matches[1], strpos($matches[1],':')));
    
    curl_setopt($ch, CURLOPT_URL, 'https://webstudenti.unica.it/esse3/auth/Logon.do');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}");
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $response = curl_exec($ch);
    

    UPDATE (extra configuration)

    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?