Files
portal/app/Http/Controllers/TestController.php
T
omair saleh 5fb880ef83 large commit
2020-10-22 12:46:06 +08:00

272 lines
10 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Classes\Modules\Accounts\ControllersLogic\CheckEmailLogic;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class TestController
{
public function index(){
$cookies = $this->getToken();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://portalvt.azurewebsites.net/Services/DataControllerService.asmx/GetPage');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"controller":"WarehouseList","view":"grid1","request":{"PageSize":10000000, "Filter":["ParcelDate:>%js%\"2020-01-10T00:00:00.000\"\u0000"]}}');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Connection: keep-alive';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36';
$headers[] = 'X-Requested-With: XMLHttpRequest';
$headers[] = 'Content-Type: application/json; charset=UTF-8';
$headers[] = 'Accept: */*';
$headers[] = 'Origin: http://portalvt.azurewebsites.net';
$headers[] = 'Referer: http://portalvt.azurewebsites.net/Pages/WarehouseList.aspx';
$headers[] = 'Accept-Language: en-US,en;q=0.9';
$headers[] = 'Cookie: '.implode('; ', $cookies);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
dd(json_decode($result));
}
private function getToken(){
$cookies = $this->getCookies();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://portalvt.azurewebsites.net/Services/DataControllerService.asmx/Login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"username":"CIEF","password":"0122120880","createPersistentCookie":true}');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Connection: keep-alive';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36';
$headers[] = 'X-Requested-With: XMLHttpRequest';
$headers[] = 'Content-Type: application/json; charset=UTF-8';
$headers[] = 'Accept: */*';
$headers[] = 'Origin: http://portalvt.azurewebsites.net';
$headers[] = 'Referer: http://portalvt.azurewebsites.net/Login.aspx?ReturnUrl=%2fPages%2fHome.aspx';
$headers[] = 'Accept-Language: en-US,en;q=0.9';
$headers[] = 'Cookie: '.implode('; ', $cookies);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$cookies[] = 'AppVTCC='.json_decode($result)->d->Token;
return $cookies;
}
private function getCookies(){
$cookies = [];
$curl_handle = curl_init('http://portalvt.azurewebsites.net/Login.aspx');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl_handle, CURLOPT_HEADER, true);
$data = curl_exec($curl_handle);
$responseHeader= substr($data,0, intval(curl_getinfo($curl_handle, CURLINFO_HEADER_SIZE)));
$endPosition = 0;
while(true) {
$startPosition = strpos($responseHeader, 'Set-Cookie: ', $endPosition);
if (!$startPosition){break;}
$startPosition += 12;
$endPosition = strpos($responseHeader, ';', $startPosition);
$cookies[] = substr($responseHeader,$startPosition,$endPosition-$startPosition);
}
return $cookies;
}
public function index2() {
// curl 'http://portalvt.azurewebsites.net/Services/DataControllerService.asmx/Login' \
// -H 'Connection: keep-alive' \
// -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36' \
// -H 'X-Requested-With: XMLHttpRequest' \
// -H 'Content-Type: application/json; charset=UTF-8' \
// -H 'Accept: */*' \
// -H 'Origin: http://portalvt.azurewebsites.net' \
// -H 'Referer: http://portalvt.azurewebsites.net/Login.aspx?ReturnUrl=%2fPages%2fHome.aspx' \
// -H 'Accept-Language: en-US,en;q=0.9' \
// -H 'Cookie: ASP.NET_SessionId=cxe2tgbsi03jtg2p0nlfdcrl; ARRAffinity=2950dd3bc3c86410bd48fe08ed3595e06670f25d93c162510d25040b02f01a67' \
// --data-binary '{"username":"CIEF","password":"0122120880","createPersistentCookie":true}' \
// --compressed \
// --insecure
header('Content-Type: text/plain; charset=utf-8');
$post = array('createPersistentCookie'=>true,
'username'=>'CIEF',
'password'=>'0122120880');
$request = array();
$request[] = 'Connection: keep-alive';
$request[] = 'Pragma: no-cache';
$request[] = 'Cache-Control: no-cache';
$request[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$request[] = 'User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36';
$request[] = 'DNT: 1';
$request[] = 'http://portalvt.azurewebsites.net/Login.aspx?ReturnUrl=%2fPages%2fHome.aspx';
$request[] = 'Accept-Encoding: gzip, deflate';
$request[] = 'Accept-Language: en-US,en;q=0.8';
$request[] = 'Content-Type:multipart/form-data';
$url = 'http://portalvt.azurewebsites.net/Login.aspx';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
if (curl_errno($ch)){
$data .= 'Retreive Base Page Error: ' . curl_error($ch);
var_dump($data);
}
else {
$info = rawurldecode(var_export(curl_getinfo($ch),true));
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$responseHeader= substr($data,0,$skip);
// The folowing line is for debug
// echo "$info\n$responseHeader\n$data\n\n";
// Get the cookies:
$e = 0;
while(true){
$s = strpos($responseHeader,'Set-Cookie: ',$e);
if (!$s){break;}
$s += 12;
$e = strpos($responseHeader,';',$s);
$cookie = substr($responseHeader,$s,$e-$s) ;
$s = strpos($cookie,'=');
$key = substr($cookie,0,$s);
$value = substr($cookie,$s);
$cookies[$key] = $value;
}
// Create cookie for subsequent Requests:
$cookie = '';
$show = '';
$head = '';
$delim = '';
foreach ($cookies as $k => $v){
$cookie .= "$delim$k$v";
$delim = '; ';
}
}
var_dump($cookies);
$request = array();
$request[] = 'Host: portalvt.azurewebsites.net';
$request[] = 'Connection: keep-alive';
$request[] = 'Pragma: no-cache';
$request[] = 'Cache-Control: no-cache';
$request[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$request[] = 'User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36';
$request[] = 'DNT: 1';
$request[] = 'Referer: http://portalvt.azurewebsites.net/Login.aspx?ReturnUrl=%2fPages%2fHome.aspx';
$request[] = 'Accept-Encoding: gzip, deflate';
$request[] = 'Accept-Language: en-US,en;q=0.8';
$url = 'http://portalvt.azurewebsites.net/Login.aspx';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION , true );
curl_setopt($ch, CURLOPT_COOKIE, $cookie );
$data = curl_exec($ch);
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
echo substr($data,$skip); //do not transmit header
// if (curl_errno($ch)){
// $data .= 'Retreive Base Page Error: ' . curl_error($ch);
// var_dump($data);
// }
// else {
// $info = rawurldecode(var_export(curl_getinfo($ch), true));
//
// // Get the cookies:
//
// $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
// $responseHeader = substr($data, 0, $skip);
//
// echo "$info\n$responseHeader\n$data\n\n";
//
// $e = 0;
// while (true) {
// $s = strpos($responseHeader, 'Set-Cookie: ', $e);
// if (!$s) {
// break;
// }
// $s += 12;
// $e = strpos($responseHeader, ';', $s);
// $cookie = substr($responseHeader, $s, $e - $s);
// $s = strpos($cookie, '=');
// $key = substr($cookie, 0, $s);
// $value = substr($cookie, $s);
// $cookies[$key] = $value;
// }
// }
}
}