Last active
November 14, 2017 12:44
-
-
Save surferxo3/b62687465f4eac197fb71c384102944d to your computer and use it in GitHub Desktop.
Script to fetch Merchant Gateways from Konnektive Crm. Demonstrates data automation and scrapping from a specific source.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /*############################# | |
| * Developer: Mohammad Sharaf Ali | |
| * Designation: Web Developer | |
| * Version: 1.0 | |
| */############################# | |
| ini_set('max_execution_time', 0); | |
| ini_set('memory_limit', '1G'); | |
| error_reporting(E_ERROR); | |
| $cookie = ''; | |
| function doParse($data) { | |
| $data = json_decode($data, true); | |
| $data = $data['message']; | |
| $dom = new DOMDocument(); | |
| $dom->loadHTML($data); | |
| $xpath = new DOMXPath($dom); | |
| $billerId = $xpath->query("//td[contains(@class, 'col_billerId')]"); | |
| $title = $xpath->query("//td[contains(@class, 'col_title')]"); | |
| $apiName = $xpath->query("//td[contains(@class, 'col_apiName')]"); | |
| $items = $billerId->length; | |
| for ($i=0; $i<$items; $i++) { | |
| echo $billerId->item($i)->nodeValue . ' :: ' . $title->item($i)->textContent . ' :: ' . $apiName->item($i)->textContent; | |
| echo '<br /><br />'; | |
| } | |
| } | |
| function initCurlRequest($reqType, $reqURL, $reqBody = '', $headers = []) { | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_URL, $reqURL); | |
| curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
| curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $reqType); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, $reqBody); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
| curl_setopt($ch, CURLOPT_HEADER, true); | |
| $body = curl_exec($ch); | |
| // extract header | |
| $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
| $header = substr($body, 0, $headerSize); | |
| $header = getHeaders($header); | |
| // extract body | |
| $body = substr($body, $headerSize); | |
| curl_close($ch); | |
| return [$header, $body]; | |
| } | |
| function getHeaders($respHeaders) { | |
| $headers = []; | |
| $headerText = substr($respHeaders, 0, strpos($respHeaders, "\r\n\r\n")); | |
| foreach (explode("\r\n", $headerText) as $i => $line) { | |
| if ($i === 0) { | |
| $headers['http_code'] = $line; | |
| } else { | |
| list ($key, $value) = explode(': ', $line); | |
| $headers[$key] = $value; | |
| } | |
| } | |
| return $headers; | |
| } | |
| function doLogin() { | |
| global $cookie; | |
| $reqBody = 'userName=XXX&password=XXX&processLogin=1'; // replace 'XXX' with your crm username & password | |
| $headers = ["Content-Type: application/x-www-form-urlencoded"]; | |
| list($header, $body) = initCurlRequest('POST', 'https://crm.konnektive.com/', $reqBody, $headers); | |
| $cookie = explode(';', trim($header['Set-Cookie']))[0]; | |
| } | |
| function doLogout() { | |
| global $cookie; | |
| $reqBody = ''; | |
| $headers = ['Cookie: ' . $cookie]; | |
| initCurlRequest('POST', 'https://crm.konnektive.com/account/logout/', $reqBody, $headers); | |
| } | |
| function doFetch() { | |
| global $cookie; | |
| $reqBody = ''; | |
| $headers = ['Cookie: ' . $cookie]; | |
| list($header, $body) = initCurlRequest('POST', 'https://crm.konnektive.com/merchants/mids/', $reqBody, $headers); | |
| $reqBody = 'query=1&page=1&resultsPerPage=1000&sortBy=title&sortDir=1&displayType=VISIBLE'; | |
| preg_match("/'csrfToken':'([0-9a-z]+)',\s'currentCompanyId':'([0-9]+)'/", $body, $match); | |
| $headers = [ | |
| 'Content-Type: application/x-www-form-urlencoded', | |
| 'Cookie: ' . $cookie, | |
| 'X-CSRF-Token: ' . $match[1] | |
| ]; | |
| list($header, $body) = initCurlRequest('POST', 'https://crm.konnektive.com/merchants/mids/getTable.ajax.php', $reqBody, $headers); | |
| return $body; | |
| } | |
| ### MAIN ### | |
| doLogin(); | |
| $data = doFetch(); | |
| doParse($data); | |
| doLogout(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment