php curl sampleプログラム

https対応、basic認証対応

<?php
class curl 
{
    var $id = null;
    var $pw = null;
    var $url = null;
    var $fieldsdata = null;
    var $_result = array();
    var $response_code = null;
    function set_id($id) {
        $this->id = $id;
    }

    function set_pw($pw) {
        $this->pw = $pw;
    }
  
    function set_url($url) {
        $this->url = $url;
    }

    var $proxy;
    function set_proxy($proxy) {
        $this->proxy = $proxy;
    }


    var $proxyport;
    function set_proxyport($proxyport) {
        $this->proxyport = $proxyport;
    }

    function get_url() {
        return $this->url;
    }

    function set_fieldsdata($value) {
        $this->fieldsdata = $value;
    }

    function get_fieldsdata() {
        return $this->fieldsdata;
    }

    function get_result() {
        return $this->_result;
    }

    function do_connect() {
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $this->url);
        curl_setopt ($ch, CURLOPT_HEADER, 1);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,  false);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);

        if($this->proxy)
          {
            curl_setopt($ch, CURLOPT_PROXY,$this->proxy);
          }
        if($this->proxyport)
          {
            curl_setopt($ch, CURLOPT_PROXYPORT,$this->proxyport);
          }
        if ($this->fieldsdata) {
            curl_setopt ($ch, CURLOPT_POST, 1);
            curl_setopt ($ch, CURLOPT_POSTFIELDS, $this->fieldsdata); 
        }

        if (!is_null($this->id) && !is_null($this->pw)) {
            curl_setopt ($ch, CURLOPT_USERPWD, "{$this->id}:{$this->pw}");
        }

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

        list($header, $response) = split("(\r\n){2}",$result, 2);

        $this->_result = $response;
        $headerline = split("\n",$header);
        for ($i=0; $i<count($headerline); $i++) {
            if (ereg("^HTTP", $headerline[$i])) {
                $headerline[$i] = trim($headerline[$i]);
                $httpflag = split(" ",$headerline[$i],3);
                if (!$this->_is_http_200($httpflag)) {
                    return false;
                }
            }
        }
        return true;
    }

    function _is_http_200($httpflag) {
        $this->set_response_code($httpflag[1]);
        if (($httpflag[1] == "200") && ($httpflag[2] == "OK")) {
            return true;  
        }
        else {
            return false;
        }
    }

    function set_response_code($code) {
        $this->response_code = $code;
    } 

    function get_response_code() {
        return $this->response_code;
    } 
}

require_once "curl_ssl.class.php";
$o = new curl;
$o->set_url("https://www.hoge.hoge/index.html");
$o->set_id("user");
$o->set_pw("password");
$o->set_proxy("http://proxy.hoge.com:8080");
$o->set_proxyport("8080");
$o->do_connect();

print $response_code = $o->get_response_code();
print $contents = $o->get_result();


?>