Collection.php 1.54 KB
<?php

namespace app\components;

use Yii;
use yii\base\Component;

class Collection extends Component
{
    public static function getHostsByCsv($csvFile, $separator = ";", $seporatorHosts = '|', $pos = 1, $unique = true)
    {
        $hosts = [];
        $row = 1;
        if (($handle = fopen($csvFile, "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, $separator)) !== FALSE) {
                if ($row > 1) {
                    $hostsString = $data[$pos] ?? '';
                    if ($hostsString) {
                        //$arr = array_map('trim', explode($seporatorHosts, $hostsString));
                        //$hosts = array_merge($hosts, $arr);
                        $hosts[] = str_replace('*.', '', $hostsString);
                    }
                }
                $row++;
            }
            fclose($handle);
        }
        if ($unique && $hosts) {
            $hosts = array_unique($hosts);
        }

        return $hosts;
    }


    public static function getRawData($url, $domain)
    {
        $post = [
            'a' => 'act',
            'ip' => $domain,
        ];
        $ch = curl_init('https://2ip.ua/ru/services/information-service/domain-information');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

        $response = curl_exec($ch);
        $errorNum = curl_errno($ch);
        $errorMsg = curl_error($ch);

        curl_close($ch);

        return ['data' => $response, 'errorno' => $errorNum, 'errormessage' => $errorMsg];
    }

}