0

I try to log in at https://ssloperator.voiceworks.nl/site/loginform with curl and cookies, however I do not see this page or an error message on it.

How to login in with Curl and SSL and cookies does not work either.

Can you help me further? The script that once worked on http, I possibly available on request per 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
   }
    }
  }

   }
    }
  }

 
//==================================================================================================================
?>

<?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);
  }

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

?>

Greeting,

Peters

Peters T.
  • 1
  • 2

1 Answers1

0

sure. ps, your code is doing a lot of bad things, like parsing HTML with regex (and parsing html with strings functions like strpos/explode), you should use a proper HTML parser instead, like DOMDocument, and you're doing manual cookie handling, imo its much better to just let curl handle cookies automatically, curl is really good at that. and you keep re-creating/deleting curl handles several times, i think it's better to just keep re-using the same curl handle, thus allowing curl to handle cookies automatically between requests.

anyway, using hhb_curl, this should get you logged in, just add the username/password on line 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 );

it currently throws:

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

meaning that the username/password ???/??? is not valid.

hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • Hi Hans, Thanks! Parse error: syntax error, unexpected ':', expecting '{' in C:\wamp\www\VW\hhb_.inc.php on line 9 Is there still a certain function that should be switched on from php? – Peters T. Jun 06 '18 at 18:29
  • @PetersT. oh, that'll happen if you try to run it in PHP5... this code is written for PHP7 (requires php 7.0.0 or higher) - there is a php5 version of hhb_curl [here](https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php) , but the code in my post is also written in PHP7, hmm – hanshenrik Jun 06 '18 at 18:35
  • Thanks, the login works, but I do not quite understand how I can now go to the next page where I want to retrieve data as it worked in my previous script? Can you help me? Perhaps we can discuss this privately? – Peters T. Jun 06 '18 at 19:52
  • @PetersT. sure, my contact info is on https://stackoverflow.com/users/1067003/hanshenrik?tab=profile – hanshenrik Jun 06 '18 at 20:42