Crizin.HTTPRequest.php

예전에 웹브라우저를 에뮬레이트 하는 간단한 PHP 클래스를 만든적이 있었는데 회사에 들어온 뒤 파파챠님이 만드신 태터툴즈용 HTTPRequest 클래스에 감명을 받아;; 전체적인 모습을 그 것과 비슷하게 발전시켜왔다. 2년넘게 이걸로 다양한 짓거리들을 하며 써오고 있는데 마지막 버전업이 작년 여름인걸 보니 이제 될건 다 되겠다 싶어 공개. Snoopy같은 라이브러리와 많이 비슷한데 필요한 기능들을 집어넣다보니 꽤 다르게 됐다. 이걸 가지고 할 수 있는 일은 A 게시판을 RSS로 만들어 구독한다던가 B 갤러리에 새 사진이 등록되면 하드에 저장한다던가 브라우저로는 접근하기 어려운 C 사이트의 뒷구멍으로 뭔가를 날리는 등등의 다양한 노가다성 작업이 가능하다.

다운로드

Download from GoogleCode

Simple tutorial

<?
    require 'Crizin.HTTPRequest.php';

    // Create instance
    $h = new HTTPRequest('www.foo.com');
    // Turn on debug mode (request/response streams will be print)
    $h->debug(true);
    // Setting 'PATH'
    $h->setPath('/');
    // Request by GET method
    $h->send();
    // Get responsed informations
    $responseText = $h->responseText;
    $contentType = $h->getResponseHeader('Content-Type');

    // Let's try to another host into 8080 port
    $h->open('www.bar.com', 8080);
    // Path and GET parameter string
    $h->setPath('/accept.php?mode=login&and=more');
    // Send around proxy server
    $h->setProxy('www.proxy.com', 8888);
    // Modify 'User-Agent' header
    $h->setRequestHeader('User-Agent', 'Gozilla/1.0');
    // Import from cookie string
    $h->importCookie('COOK1=foo; COOK2=bar;');
    // Set cookie
    $h->setCookie('cook', 'something');
    // Request by POST method
    $h->send('param1=value1&param2=value2');
    // Get cookie
    $sessionId = $h->getCookie('PHPSESSID');

    // Download image
    $h->setPath('/images/logo.gif');
    // Login with apache authorization
    $h->authorization('myId', 'myPassword');
    // Hide 'User-Agent' header
    $h->removeRequestHeader('User-Agent');
    // Response content will be saved in './save'
    $h->saveToFile('./save');

    // Upload some files
    $h->setPath('/write.php');
    // <input type="file" name="image" value="/files/blah.gif"/> and rename to 'myImage.gif'
    $h->attachFile('image', './files/blah.gif', 'myImage.gif');
    // Request
    $h->send();

    // Test some text exists in $responseText or not
    $result = $h->existText('OK') ? 'Succeed' : 'Failed';
    // Get all text fragments was surrounded by '<td>' and '</td>' (array will be return)
    $result = $h->getAllTextFragments('<td>', '</td>');
    // Get 5th text only (string will be return)
    $result = $h->getTextFragment('<td>', '</td>', 5);
?>

'컴퓨터 얘기 > 프로그래밍' 카테고리의 다른 글

TinyMCE 플러그인 - WikiExporter  (5) 2007/02/07
멜론 앨범 커버 다운로더  (20) 2007/02/04
Crizin.HTTPRequest.php  (8) 2007/02/03
preg 계열 함수들의 버그?  (10) 2006/08/21
IE 6.0의 버그들  (7) 2006/08/11
태터툴즈 1.0.5 플러그인  (58) 2006/05/09