使用Curl,SSL和cookie登录

时间:2018-06-06 14:32:07

标签: ssl curl cookies

我尝试使用curl和Cookie登录https://ssloperator.voiceworks.nl/site/loginform,但是我没有看到此页面或错误消息。

How to login in with Curl and SSL and cookies也不起作用。

你能进一步帮助我吗?曾经在http上运行的脚本,我可能会根据请求提供每个PM。



<?php
//=============================================================================
//===   Source code by ############ 2018   ====================================
//=============================================================================

//=============================================================================
//===   Scan voor nieuwe bundels en plaats deze in de database   ==============
//=============================================================================


set_time_limit (0);
include ("./functions/www_functions.php");
//include ("./functions/db_functions.php");

if (true)
  {
  $cookie= Get_Page_Cookie ("https://ssloperator.voiceworks.nl/site/dologin?form[username]=".$usern."&form[password]=".base64_decode ($passw));
  $webpage= Get_Page ("https://ssloperator.voiceworks.nl/account/overview?type=wholesale&id=45069");

  $pages= stripparts ($webpage, "rel=\"navlink\">", "</a", 0);
  $lastpage= "0";
  foreach ($pages as $page)
    {
    if ($page== "volgende")
      { break; }
    else
      { $lastpage= $page; }
    }

  $totaal= 1;
  
//  for ($page= 1; $page<= $lastpage; $page++)
  for ($page= 11; $page<= 11; $page++)
    {
    $tel= 1;
    $webpage= Get_Page ("https://ssloperator.voiceworks.nl/account/overview?type=wholesale&id=5321&overviewpage={$page}");
    $webpagex= strippart ($webpage, "<tbody>", "</tbody>");
    $pages= stripparts ($webpagex, "<a onclick=\"javascript:viewAccount(", ");return false;\" parsetex", 0);

    $subStr= array();
    foreach ($pages as $nr)
      {
      if (is_numeric ($nr))
        { 
        $subStr[]= $nr;
		}
      }

    foreach ($subStr as $KlantID)
	  {
      $webpage= Get_Page ("https://ssloperator.voiceworks.nl/mobile/order/overview/accountId/".$KlantID);

      $webpagex= strippart ($webpage, "Bedrijfsbundels", "</tbody>");
      $pages= stripparts ($webpagex, "<a href=\"", "\">", 0);

      if ($pages!= array())
	    {
        $webpage= Get_Page ("https://ssloperator.voiceworks.nl/account/setaccount?accountId=".$KlantID);
        $KlantNaam= ucfirst (strtolower (html_entity_decode (strippart ($webpage, "<h3 class=\"company-name\">", " #", 0))));
        $KlantEmail=  strtolower (html_entity_decode (strippart ($webpage, "id=\"value-adminEmail\"><a href='mailto:", "'>", 0)));
        $webpage= Get_Page ("https://ssloperator.voiceworks.nl/mobile/order/overview/accountId/".$KlantID);
		
//        db_write_new_klant ($mysql_conn, $KlantID, $KlantNaam, $KlantEmail);
        foreach ($pages as $pag)
	      {
          $webpage= Get_Page ("https://ssloperator.voiceworks.nl". $pag);
          $Bundel= intval (strippart ($webpage, "<h1>Orderdetails #", "</h1>", 0));

          $webpagex= strippart ($webpage, "overviewContainer_companyBundleUsageOverview", "</table>");
          $parts= stripparts ($webpagex, "<td>", "</td>", 0);

          $Aansluiting= $parts[0];
		  $partx= explode (" / ", $parts[1]);
		  $party= explode (" ", $partx[1]);
		  $MaxVerbruik= $party [0];

          db_write_new_bundel ($mysql_conn, $Bundel, $Aansluiting, $MaxVerbruik, $KlantID);
          echo $Bundel." - ".$KlantID." - ".$KlantNaam."<br>\n";
		  
          $Volgende= 1;
		  while ($Volgende== 1)
		    {
            $webpagex= strippart ($webpage, "<span id=\"overviewContainer_companyBundleOverview\">", "</table>");
            $webpagey= strippart ($webpagex, "<tbody>", "</tbody>", 0);
            $parts= stripparts ($webpagey, "<tr>", "</tr>", 0);
			if (count ($parts)> 0)
			  {	
		      if (strpos ($parts [0], "Geen orders gevonden")=== false)
			    {	
                foreach ($parts as $part)
		          {
                  $partsx= stripparts ($part, "<td>", "</td>", 0);
                  //  <a href="/mobile/order/detail/id/157503">00157503</a>
		          //  0633039968
                  //  NuMobile
                  //  <span class="badge badge-info">Data Pack 1000 (Blue) BB (4G)</span>
                  //  62.23 MB
                  //  24-11-2016
                  //  <span class="badge badge-success">Actief</span>
                  $Abonnement= strippart ($partsx[0], "\">", "</");
                  $GSMnummer= $partsx[1];
		          $Portfolio= $partsx[2];
                  $Aansluiting= strippart ($partsx[3], "\">", "</");
				  $MaxV= explode (" ", $Aansluiting);
                  $MaxVerbruik= $MaxV[2];
              
//                  db_write_new_abonnement ($mysql_conn, $Abonnement, $GSMnummer, $Portfolio, $Aansluiting, $MaxVerbruik, $Bundel, $KlantID);
		          }
				}
		      }
		    //  <span class="actions pagenav">	</span>
            //  &nbsp;<a href="		" rel="navlink">volgende</a>
            $webpagex= strippart ($webpage, "<span class=\"actions pagenav\">", "</span>");
            if ($webpagex!= "")
		      {	  
              $webpagey= strippart ($webpagex, "&nbsp;<a href=\"", "\" rel=\"navlink\">volgende</a>", 0);
              if ($webpagey!= "")
			    {
                $webpage= Get_Page ($webpagey);
			    $Volgende= 1;
				}
			  else
		        { $Volgende= 0; }
              }
		    else
		      { $Volgende= 0; }
            // While next page, keep repeating
			}
		  }
		}

	  }
    }
  }

 
//==================================================================================================================
?>
&#13;
&#13;
&#13;

&#13;
&#13;
<?php
//==================================================================================================================
//===   Source code by ############ 2018  ==========================================================================
//==================================================================================================================


$usern= "xxxxx";
$passw= "xxxxx";

//==================================================================================================================
function Get_Page ($url)
  {
  $stukjes= array();
  $url= html_entity_decode ($url);
  if (strpos ($url, "?")!== false)
    {
	$stukken= explode ("?", $url);
    $url= $stukken [0];
    $parts= explode ("&", $stukken[1]);
    foreach ($parts as $part)
	  {
	  $s= explode ("=", $part);
	  $stukjes[$s[0]]= $s[1]; 
	  }
    }
  global $cookie;
  $ch= curl_init ();

  curl_setopt ($ch, CURLOPT_HEADER, 0);
  curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt ($ch, CURLOPT_URL, $url); 
  if ($stukjes!= array())
    { curl_setopt ($ch, CURLOPT_POSTFIELDS, $stukjes); }
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_COOKIE, $cookie);
  $webpage= curl_exec ($ch);

  return ($webpage);
  }

//==================================================================================================================
function Get_Page_Cookie ($url)
  {
  $ch= curl_init ();

  curl_setopt ($ch, CURLOPT_HEADER, 1);
  curl_setopt ($ch, CURLOPT_URL, $url); 
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  $webpage= curl_exec ($ch);

  preg_match_all ('|Set-Cookie: (.*);|U', $webpage, $results);
  $cookie= implode (';', $results [1]);

  return ($cookie);
  }

//==================================================================================================================
function Break_Down_URL ($url)
  {
  // Strip off the HTTP://
  $http= "http://";
  if (strpos ($url, $http)!== False)
    {
	if (strpos ($url, $http)== 0)
	  { $url= substr ($url, strlen ($http), strlen ($url)- strlen ($http)); }
	}

  // Strip server from the url
  $beginStr= "/";
  $beginPos= strpos ($url, $beginStr, 0);
  if ($beginPos!== false)
    {
	$server= substr ($url, 0, $beginPos);
	$pageurl= substr ($url, $beginPos, strlen ($url)- $beginPos);
	}

  // Strip webpage and params from url
  $beginStr= "?";
  $beginPos= strpos ($pageurl, $beginStr, 0);
  if ($beginPos!== false)
    {
	$page= substr ($pageurl, 0, $beginPos);
	$params= substr ($pageurl, $beginPos+ strlen ($beginStr), strlen ($pageurl)- $beginPos- strlen ($beginStr));
	}
  else
    {
	$page= trim ($pageurl);
	$params= "";
	}
  
  // Get params into array
  $urlparams= array ();
  $beginStr= "&";
  $beginPos= strpos ($pageurl, $beginStr, 0);
  if ($beginPos!== false)
    {
    $paramparts= explode ($beginStr, $params);
//    $urlparams= array ();
	foreach ($paramparts as $param)
	  {
      if (strpos ($pageurl, "=", 0)!== false)
        {
        $p= explode ("=", $param);
		$parameter= $p[0];
		$data= $p[1];
		}
	  else
	    {
		$parameter= $param;
		$data= "";
		}
      $urlparams [$parameter]= $data;
	  }
	}
  else
    {
    if (strpos ($pageurl, "=", 0)!== false)
      {
      $p= explode ("=", $params);
      $parameter= $p[0];
	  $data= $p[1];
      $urlparams [$parameter]= $data;
	  }
	}  
  return (array ($server, $page, $urlparams));
  }
  
//==================================================================================================================
function strippart ($content, $beginStr, $endStr, $start=0)
  {
  $subStr= "";
  $beginPos= strpos ($content, $beginStr, $start);
  if ($beginPos!== false)
    {
    $beginPos= $beginPos+ strlen ($beginStr);
	$endPos= strpos ($content, $endStr, $beginPos);
	if ($beginPos!== false)
      {
      $length= $endPos- $beginPos;
      $subStr= substr ($content, $beginPos, $length);
      }
	}
  return ($subStr);
  }

//==================================================================================================================
function stripparts ($content, $beginStr, $endStr, $start= 0)
  {
  $subStr= array();
  while (strpos ($content, $beginStr, $start)!== false)
    {
    $beginPos= strpos ($content, $beginStr, $start);
    $beginPosA= $beginPos+ strlen ($beginStr);
	$endPos= strpos ($content, $endStr, $beginPosA);
	if ($endPos!== false)
      {
      $length= $endPos- $beginPosA;
      $a= substr ($content, $beginPosA, $length);
      if ($a!= "wijzig")
	    { $subStr[]= $a; }
      $start= $endPos+ 1;
	  if ($start> strlen ($content))
	    { $start= strlen ($content); }
      }
	}

  return ($subStr);
  }

//==================================================================================================================

?>
&#13;
&#13;
&#13;

问候语,

彼得斯

1 个答案:

答案 0 :(得分:0)

确定。 ps,你的代码做了很多坏事,like parsing HTML with regex(并用strpos / explode这样的字符串函数解析html),你应该使用正确的HTML解析器,比如DOMDocument,你就是在做手册cookie处理,imo更好的让curl自动处理cookie,curl真的很擅长。并且你不断重复创建/删除卷曲手柄几次,我认为继续重复使用相同的卷曲手柄会更好,从而允许curl在请求之间自动处理cookie。

无论如何,使用hhb_curl,这应该会让您登录,只需在第12行和第12行添加用户名/密码。 13:

<?php
declare(strict_types = 1);
require_once ('hhb_.inc.php');
$hc = new hhb_curl ( '', true );
$html = $hc->exec ( 'https://ssloperator.voiceworks.nl/site/dologin' )->getStdOut ();
$domd = @DOMDocument::loadHTML ( $html );
$inputs = array ();
foreach ( $domd->getElementsByTagName ( "input" ) as $input ) {
    $inputs [$input->getAttribute ( "name" )] = $input->getAttribute ( "value" );
}
assert ( isset ( $inputs ['form[username]'], $inputs ['form[password]'] ) );
$inputs ['form[username]'] = "???";
$inputs ['form[password]'] = "???";
$html = $hc->setopt_array ( array (
        CURLOPT_URL => $domd->getElementsByTagName ( "form" )->item ( 0 )->getAttribute ( "action" ),
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => $inputs 
) )->exec ()->getStdOut ();
$domd = @DOMDocument::loadHTML ( $html );
$xp = new DOMXPath ( $domd );
$loginErrors = "";
foreach ( $xp->query ( "//*[contains(@class,'form_error')]" ) as $loginError ) {
    $tc = trim ( $loginError->textContent );
    if (! empty ( $tc )) {
        $loginErrors .= $tc . " - ";
    }
}
if (! empty ( $loginErrors )) {
    throw new \RuntimeException ( 'failed to login: ' . $loginErrors );
}
var_dump ( $html );

它目前抛出:

PHP Fatal error:  Uncaught RuntimeException: failed to login: gebruikersnaam of wachtwoord onjuist -  in /home/hanshenrik/projects/phptests3/foo6.php:26
Stack trace:
#0 {main}
  thrown in /home/hanshenrik/projects/phptests3/foo6.php on line 26

意思是用户名/密码??? / ???无效。