Началр работы с переработкой
This commit is contained in:
109
sources/Browsers/Curl.php
Normal file
109
sources/Browsers/Curl.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace VK\Browsers;
|
||||
|
||||
use VK\Core;
|
||||
use Exception;
|
||||
|
||||
class Curl extends BrowserAbstract
|
||||
{
|
||||
/**
|
||||
* SSL шифрование
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private static bool $ssl = false;
|
||||
|
||||
public static function post($url, $params = [])
|
||||
{
|
||||
$c = curl_init();
|
||||
curl_setopt($c, CURLOPT_HTTPHEADER, [
|
||||
"Content-Type:multipart/form-data"
|
||||
]);
|
||||
curl_setopt($c, CURLOPT_URL, $url);
|
||||
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($c, CURLOPT_POSTFIELDS, $params);
|
||||
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
|
||||
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
|
||||
$result = json_decode(curl_exec($c), True);
|
||||
curl_close($c);
|
||||
|
||||
if (isset($result['response']))
|
||||
return $result['response'];
|
||||
else if (isset($result['error']))
|
||||
throw new Exception(json_encode($result), $result['error']['error_code']);
|
||||
else if (!isset($result))
|
||||
self::post($url, $params);
|
||||
else
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function getToken($url)
|
||||
{
|
||||
if (!self::checkSSL('localhost')) {
|
||||
$core = Core::init();
|
||||
$core->logger->notice('Соединение не защищено. Необходимо включить SSL шифрование');
|
||||
}
|
||||
|
||||
if ($curl = curl_init()) {
|
||||
curl_setopt($curl, CURLOPT_URL, $url);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
||||
|
||||
curl_setopt($curl, CURLOPT_USERAGENT, $_ENV['USERAGENT']);
|
||||
if (isset($post_values)) {
|
||||
curl_setopt($curl, CURLOPT_POST, 1);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_values);
|
||||
}
|
||||
|
||||
if ($cookie and isset(self::$cookie)) {
|
||||
$send_cookie = [];
|
||||
foreach (self::$cookie as $cookie_name => $cookie_val) {
|
||||
$send_cookie[] = "$cookie_name=$cookie_val";
|
||||
}
|
||||
curl_setopt($curl, CURLOPT_COOKIE, join('; ', $send_cookie));
|
||||
}
|
||||
|
||||
curl_setopt(
|
||||
$curl,
|
||||
CURLOPT_HEADERFUNCTION,
|
||||
function ($curl, $header) use (&$headers) {
|
||||
$len = strlen($header);
|
||||
$header = explode(':', $header, 2);
|
||||
if (count($header) < 2) // ignore invalid headers
|
||||
return $len;
|
||||
|
||||
$name = strtolower(trim($header[0]));
|
||||
if (isset($headers) and !array_key_exists($name, $headers))
|
||||
$headers[$name] = [trim($header[1])];
|
||||
else
|
||||
$headers[$name][] = trim($header[1]);
|
||||
|
||||
return $len;
|
||||
}
|
||||
);
|
||||
|
||||
$out = curl_exec($curl);
|
||||
|
||||
curl_close($curl);
|
||||
//if (isset($headers['set-cookie']))
|
||||
// $this->parseCookie($headers['set-cookie']);
|
||||
return ['header' => $headers, 'body' => $out];
|
||||
}
|
||||
}
|
||||
|
||||
protected static function checkSSL($domain)
|
||||
{
|
||||
$ssl_check = @fsockopen('ssl://' . $domain, 443, $errno, $errstr, 30);
|
||||
$res = !!$ssl_check;
|
||||
|
||||
if ($ssl_check) {
|
||||
fclose($ssl_check);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user