-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch_url.php
50 lines (47 loc) · 1.14 KB
/
fetch_url.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
39
40
41
42
43
44
45
46
47
48
49
50
<?php
$url = $_POST['url'];
function file_get_contents_curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
return $data;
}
//fetching url data via curl
$html = file_get_contents_curl($url);
if($html) {
//parsing begins here
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
$images = $doc->getElementsByTagName('img');
$clean = false;
echo '{"title":{
"title":"'.$title.'"
}';
foreach($images as $img)
{
$url = $img->getAttribute('src');
$alt = $img->getAttribute('alt');
$width = $img->getAttribute('width');
$height = $img->getAttribute('height');
if($width > 200){
echo ',"images":{
"imgsrc":"'.$url.'",
"imgalt":"'.$alt.'"
}';
break;
}else{
echo ',"images":{
"imgsrc":"",
"imgalt":""
}';
}
}
echo '}';
}
?>