Source: gosi-api.phps - download
<?php
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gosi-api.phps 0.1
* by daif alotaibi (http://daif.net)
* daif@daif.net
*
* Link:
* http://daif.net/script/gosi-api.phps
*
* Examples:
* $UserName = 'user123';
* $Password = 'pass123';
* // list employees
* $gosi_employees = gosi_employees($UserName, $Password);
* print_r($gosi_employees);
*
*/
$UserName = 'user123';
$Password = 'user123';
// list employees
$gosi_employees = gosi_employees($UserName, $Password);
print_r($gosi_employees);
function gosi_employees($UserName, $Password) {
$gosi_login = gosi_login($UserName, $Password);
if(!$gosi_login) {
return FALSE;
}
$gosi_info = gosi_info();
if(!$gosi_info) {
return FALSE;
}
$url = 'https://www.gosi.gov.sa/core/security/SecurityServlet?breakCache='.time().'&hidActionName=SEARCH_ALL_ENGAGEMENT&hidActionType=1';
$referer = 'https://www.gosi.gov.sa/core/security/SecurityServlet?hidActionType=2';
$post = [
'radViewType' => 'viw',
'txtRegistrationNumber' => $gosi_info['RegistrationNumber'],
'txtReportDate' => date('j/n/Y'),
'cmbNationalityCode' => '3',
'txtSocialInsuranceNumber' => '',
'txtNewNINumber' => '',
'txtCompanyWorkerNumber' => '',
'txtIqamaNumber' => '',
'txtBorderNumber' => '',
'txtPassportNumber' => '',
'hidButton' => '',
'hidButtonClicked' => '1',
'hidPrimaryKey' => '',
'hidSourceJsp' => '/userinterface/socialinsurance/registration/engagement/screens/EngagementListSearch.jsp',
'hidSourceMode' => 'DEFAULT',
'hidEstablishmentId' => '',
'hidSocialInsuranceNumber' => '',
'hidIsInternet' => 'true',
'hidSelectGroup' => '',
];
$data = OpenURL($url, $post, $referer);
$data = iconv('windows-1256', 'utf8', $data);
$regex = '<tr>\s*';
$regex .= '<td class=".*">(\\d+)</td>\s*';
$regex .= '<td class=".*">(.+)</td>\s*';
$regex .= '<td class=".*" align="center">(.*)</td>\s*';
$regex .= '<td class=".*" align="center" width="5%">(.+)</td>\s*';
$regex .= '<td class=".*" align="center" width="5%">(.+)</td>\s*';
$regex .= '<td class=".*" align="center" width="5%">(.+)</td>\s*';
$regex .= '<td class=".*" align="center" width="15%">(.+)</td>\s*';
$regex .= '<td class=".*" align="center">(\\d+</td>|)\s*';
$regex .= '<td class=".*" align="center" width="10%">(\\d+</td>|)\s*';
$regex .= '</tr>';
preg_match_all('#'.$regex.'#Uis', $data, $matches);
$employees = NULL;
if($matches) {
$employees = [];
foreach ($matches[1] as $key => $value) {
$employees[] = [
'gosi_number' => trim($matches[1][$key]),
'name' => trim($matches[2][$key]),
'emp_number' => trim($matches[3][$key]),
'country' => trim($matches[4][$key]),
'birth_date' => trim($matches[5][$key]),
'join_date' => trim($matches[6][$key]),
'status' => trim($matches[7][$key]),
'mol_office' => trim(strip_tags($matches[8][$key])),
'mol_number' => trim(strip_tags($matches[9][$key])),
];
}
}
return $employees;
}
function gosi_login($UserName, $Password) {
$url = 'https://www.gosi.gov.sa/gosionline';
$referer = 'http://www.gosi.gov.sa/portal/web/guest/home';
$post = [
'txtUser' => $UserName,
'txtPassword' => $Password,
'hidActionName' => 'CMN_INTER_LOGIN',
'hidTargetJsp' => '/Home.jsp',
'hidTaskId' => '100030',
'hidSourceJsp' => '/',
'txtSIN' => $UserName,
'txtPIN' => $Password,
'UserName' => $UserName,
'Password' => $Password,
'hidLanguage' => 'ARABIC',
];
$data = OpenURL($url, $post);
return preg_match('/FRAMESET/', $data);
}
function gosi_info() {
$url = 'https://www.gosi.gov.sa/core/security/SecurityServlet?hidActionType=2';
$referer = 'https://www.gosi.gov.sa/core/menu/screens/EoployerMenu.jsp?sourceJsp=/core/menu/screens/EoployerMain.jsp&&isValid=TRUE';
$post = [
'hidActionName' => 'VIEW_ESTABLISHMENT_INT',
'hidActionType' => '',
'hidSourceJsp' => '',
];
$data = OpenURL($url, $post);
$data = iconv('windows-1256', 'utf8', $data);
$gosi_info = NULL;
preg_match('#txtEstablishmentNameArb size=60 maxlength=60 value=\'(.+)\'#Uis', $data, $EstablishmentNameArb);
preg_match('#txtRegistrationNumber size=9 maxlength=9 readonly value=\'(\\d+)\'#Uis', $data, $RegistrationNumber);
if(isset($EstablishmentNameArb[1]) && isset($RegistrationNumber[1])) {
$gosi_info = [
'EstablishmentName' => $EstablishmentNameArb[1],
'RegistrationNumber' => $RegistrationNumber[1],
];
}
return $gosi_info;
}
function OpenURL($url, $post=FALSE, $referer=FALSE) {
$headers = [];
$headers[] = 'Host: www.gosi.gov.sa';
$headers[] = 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$headers[] = 'Accept-Language: ar,en;q=0.5';
$headers[] = 'Accept-Encoding: gzip, deflate, br';
if($referer){
$headers[] = 'Referer: '.$referer;
}
if($post){
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
}
$headers[] = 'Connection: keep-alive';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$cookie = dirname(__FILE__).'/'.parse_url($url, PHP_URL_HOST).'-cookie.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
if(is_array($post)) {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
}
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
$data = curl_exec($ch);
return($data);
}
?>