2012-08-13 2 views
-3

일부 웹 페이지에서 내용을 가져 오려면 CURL을 사용하고 있습니다. 그리고 콘텐츠에서 미디어 태그를 추출해야합니다.HTML 콘텐츠에서 MEDIA 태그를 추출하십시오.

해당 라이브러리가 있습니까? 또는 그 아이디어를 만드는 것에 대한 아이디어는 아주 훌륭합니다.

+1

[*** 한숨 ***] (http://stackoverflow.com/search?q= [PHP] + 구문 분석 + html) –

+1

너 자신을 알아내는 데 어떤 노력을 했습니까? 당신이 이것에 대해 인터넷 검색을 시도 할 수 없다면, 당신도이 사이트에 들러서는 안됩니다. –

답변

1

이 정보가 도움이 될까요?

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_FOLLOWLOCATION, 1); 

    $data = curl_exec($ch); 
    curl_close($ch); 

    return $data; 
} 

$html = file_get_contents_curl("http://example.com/"); 

//parsing begins here: 
$doc = new DOMDocument(); 
@$doc->loadHTML($html); 
$nodes = $doc->getElementsByTagName('title'); 

//get and display what you need: 
$title = $nodes->item(0)->nodeValue; 

$metas = $doc->getElementsByTagName('meta'); 

for ($i = 0; $i < $metas->length; $i++) 
{ 
    $meta = $metas->item($i); 
    if($meta->getAttribute('name') == 'description') 
     $description = $meta->getAttribute('content'); 
    if($meta->getAttribute('name') == 'keywords') 
     $keywords = $meta->getAttribute('content'); 
} 

echo "Title: $title". '<br/><br/>'; 
echo "Description: $description". '<br/><br/>'; 
echo "Keywords: $keywords"; 

아니면 .. 이미지를 저장해야하는 경우

$remote_img = 'http://www.example.com/images/image.jpg '; 
$img = imagecreatefromjpeg($remote_img); 
$path = 'images/'; 
imagejpeg($img, $path); 

function save_image($img,$fullpath){ 
    $ch = curl_init ($img); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
    $rawdata=curl_exec($ch); 
    curl_close ($ch); 
    if(file_exists($fullpath)){ 
     unlink($fullpath); 
    } 
    $fp = fopen($fullpath,'x'); 
    fwrite($fp, $rawdata); 
    fclose($fp); 
} 
+0

데이터를 추출하기 위해 TagName을 원하는 것으로 변경할 수 있습니다. – themis

관련 문제