|
|
<?php
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
use GuzzleHttp;
|
|
|
use GuzzleHttp\Cookie\CookieJar;
|
|
|
use function Zend\Diactoros\parseCookieHeader;
|
|
|
|
|
|
|
|
|
class CommonService
|
|
|
{
|
|
|
public static function downloadImage($dir, $imageUrl)
|
|
|
{
|
|
|
if (!file_exists($dir)) {
|
|
|
mkdir($dir);
|
|
|
}
|
|
|
$host = parse_url($imageUrl, PHP_URL_HOST);
|
|
|
$dirList = explode("/", $dir);
|
|
|
$albumName = array_pop($dirList);
|
|
|
$cookieStr = "__cfduid=d959b19dbaa25a5fac7c5fe1d3988d15e1610778634; frontend=36ab879e6d75ae4e19e4843a1bdf2e45; _gid=GA1.2.22159331.1610804846; __cf_bm=19f3172d7b8d3c97ce28f86ec56af4da3c3560a2-1610805781-1800-Adr3Ph9l2oeHE9Ms+YrTrKfE1uwMO7Tpcw/WxfRrPldspHy/AcwPovSpDcrz+DE3UHWhrw60voIaOsPy3VvpvTV9fjU9F1Hk3y1U5O6V1RZeclh6+YoIpZc1UfRyixPrKw==; frontend-rmu=qUl6oIQSib76RH6A4ZJGALhooK63; frontend-rmt=3d1i%2F0DU5cuBB%2BII5MzosWl0MPonPRchZyJyhL73PzaHWBzoq9RFDWJG%2FDPtCncI; _ga_170M3FX3HZ=GS1.1.1610804845.1.1.1610806191.0; _ga=GA1.2.6396742.1610804846; _gat_UA-140713725-1=1";
|
|
|
$cookieArr = parseCookieHeader($cookieStr);
|
|
|
$cookieJar = CookieJar::fromArray($cookieArr, ".v2ph.com");
|
|
|
$client = new GuzzleHttp\Client(['headers' => [
|
|
|
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
|
|
|
'Referer' => $host,
|
|
|
'cookies' => $cookieJar
|
|
|
]]);
|
|
|
$fileParts = pathinfo($imageUrl);
|
|
|
$completeFile = $dir . "/" . $albumName . "-" . $fileParts["filename"] . "." . $fileParts["extension"];
|
|
|
$client->request('GET', $imageUrl, ['sink' => $completeFile]);
|
|
|
}
|
|
|
|
|
|
}
|