본문 바로가기

Computer/Programing

Parsing - PHP

 ( cURL, file_get_contents, fSockOpen, Snoopy )


cURL 이용법


Example 1.

<?php

$url = "http://www.example.com";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

$output = curl_exec($ch);

$info = curl_getinfo($ch);

curl_close($ch);

//print_r($output);

echo $output;

?>


Example 2.

<?php

error_reporting(E_ALL);

ini_set("display_errors", 1);


$url = "ADDRESS IS HERE";

$curl_handle=curl_init();

curl_setopt($curl_handle, CURLOPT_URL, $url);

curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl_handle, CURLOPT_USERAGENT, 'heeseop);

$html = curl_exec($curl_handle);

curl_close($curl_handle);

echo $html;

?>




File_Get_Contents 이용법


<?php

$homepage = file_get_contents('http://www.example.com');

echo $homepage;

?>


file_get_contents 함수가 사용안되는곳인 경우 cURL 기능을 이용하면 HTML을 불러올 수 있다.

아래는 example.com 에서 HTML을 불러들여 output 으로 출력하는 Script이다.





fSockOpen 이용법


<?php

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);

if (!$fp) {

    echo "$errstr ($errno)<br />\n";

} else {

    $out = "GET / HTTP/1.1\r\n";

    $out .= "Host: www.example.com\r\n";

    $out .= "Connection: Close\r\n\r\n";

    fwrite($fp, $out);

    while (!feof($fp)) {

        echo fgets($fp, 128);

    }

    fclose($fp);

}

?>



Snoopy 이용법


아래에서 파일을 다운받아서 압축을 풀고 Snoopy.class.php 파일만 실행될 파일과 동일한 디렉토리에 넣어주면 작동한다.

https://sourceforge.net/projects/snoopy/files/latest/download


<?php

include_once 'Snoopy.class.php';

$snoopy = new snoopy;


// 웹브라우져에서 검색하는것처럼 위장

$snoopy->agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";                                                                                                     

$snoopy->referer = "http://ya-ho.com/";   


$snoopy->fetch("http://www.example.com");

$txt = $snoopy->results;

echo $txt;

//배열함수일경우 print_r($txt);

?>





Parsing 시 활용함수


#배열(Arry)을 string 으로 "|" 구분을 통해서 나눔

$html = implode("|",$html);




# $list 에서 해당값을 찾아서 $invdate 로 저장, php parcing에서 가장 많이 

preg_match_all("'(\d{1}\_){2}\.\_.*'",$list,$invdate);