-
Notifications
You must be signed in to change notification settings - Fork 1
/
curl-Image.php
38 lines (33 loc) · 955 Bytes
/
curl-Image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
function curlGet($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$results = curl_exec($ch);
curl_close($ch);
return $results;
}
function returnXPathObj($item)
{
$xmlPageDom = new DomDocument();
@$xmlPageDom->loadHTML($item);
$xmlPageXPath = new DOMXPath($xmlPageDom);
return $xmlPageXPath;
}
$webPage = curlGet('https://www.balyan.ir/');
$webPageXpath = returnXPathObj($webPage);
$coverImage = $webPageXpath->query('//img[@class="attachment-full size-full"]/@src');
if ($coverImage->length > 0)
{
$imageUrl = $coverImage->item(0)->nodeValue;
$imageName = end(explode('/', $imageUrl));
if (getimagesize($imageUrl))
{
$imageFile = curlGet($imageUrl);
$file = fopen($imageName, 'w');
fwrite($file, $imageFile);
fclose($file);
}
}