dmBridge PHP API
DMHTTPClient.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 #
00005 # Copyright © 2009, 2010, 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00013 class DMHTTPClient {
00014 
00016    private $curl_handle;
00018    private $follow_redirects = true;
00020    private $request;
00022    private $timeout = 10;
00023 
00027    public function __construct(DMHTTPRequest $request) {
00028       $this->setRequest($request);
00029    }
00030 
00031    private function getCURL() {
00032       if (!$this->curl_handle) {
00033          $this->curl_handle = curl_init();
00034          curl_setopt($this->curl_handle, CURLOPT_HTTP_VERSION,
00035             CURL_HTTP_VERSION_1_1);
00036          curl_setopt($this->getCURL(), CURLOPT_RETURNTRANSFER, true);
00037          curl_setopt($this->getCURL(), CURLINFO_HEADER_OUT, true);
00038       }
00039       global $cookiearr;
00040       if (!is_array($cookiearr)) {
00041          $cookiearr = array();
00042       }
00043       global $curl_handle;
00044       $curl_handle = $this->curl_handle;
00045       return $this->curl_handle;
00046    }
00047 
00051    public function isFollowingRedirects() {
00052       return $this->follow_redirects;
00053    }
00054 
00058    public function setFollowRedirects($bool) {
00059       $this->follow_redirects = (bool) $bool;
00060    }
00061 
00065    public function getRequest() {
00066       return $this->request;
00067    }
00068 
00072    public function setRequest(DMHTTPRequest $request) {
00073       $this->request = $request;
00074    }
00075 
00081    public function send() {
00082       // initialize the request
00083       curl_setopt($this->getCURL(), CURLOPT_URL,
00084             $this->getRequest()->getURI());
00085       curl_setopt($this->getCURL(), CURLOPT_TIMEOUT, $this->getTimeout());
00086       curl_setopt($this->getCURL(), CURLOPT_FOLLOWLOCATION,
00087             $this->isFollowingRedirects());
00088       global $cookiearr;
00089       curl_setopt($this->curl_handle, CURLOPT_HEADERFUNCTION, "read_header");
00090 
00091       switch ($this->getRequest()->getMethod()) {
00092       case DMHTTPMethod::POST:
00093          curl_setopt($this->getCURL(), CURLOPT_POST, true);
00094          curl_setopt($this->getCURL(), CURLOPT_POSTFIELDS,
00095                $this->getRequest()->getRepresentation()->getFormDataAsString());
00096          break;
00097       }
00098 
00099       $headers = array();
00100       foreach ($this->getRequest()->getHeaders() as $header) {
00101          $headers[] = $header['key'] . ": " . $header['value'];
00102       }
00103       curl_setopt($this->getCURL(), CURLOPT_HTTPHEADER, $headers);
00104       if ($this->getRequest()->getUsername()) {
00105          curl_setopt($this->getCURL(), CURLOPT_HTTPAUTH, CURLAUTH_ANY);
00106          curl_setopt($this->getCURL(), CURLOPT_COOKIESESSION, true);
00107          curl_setopt($this->getCURL(), CURLOPT_USERPWD,
00108                sprintf("%s:%s", $this->getRequest()->getUsername(),
00109                      $this->getRequest()->getPassword()));
00110       }
00111 
00112       // execute the request
00113       $response = new DMHTTPResponse();
00114       $rep = new DMHTTPRepresentation();
00115       $rep->setBody(curl_exec($this->getCURL()));
00116       $response->setRepresentation($rep);
00117       $response->setStatusCode(
00118             curl_getinfo($this->getCURL(), CURLINFO_HTTP_CODE));
00119       // @todo extract the response message from the response
00120       //$response->setStatusMessage();
00121       $redirected_uri = curl_getinfo($this->getCURL(), CURLINFO_EFFECTIVE_URL);
00122       if ($redirected_uri != $this->getRequest()->getURI()->__toString()) {
00123          $this->getRequest()->setRedirectedURI(new DMURI($redirected_uri));
00124       }
00125       $tmp = curl_getinfo($this->getCURL(), CURLINFO_CONTENT_TYPE);
00126       $tmp = explode(";", $tmp);
00127       $type = DMMediaType::getTypeForString($tmp[0]);
00128       if ($type) {
00129          $rep->setMediaType($type);
00130       }
00131 
00132       // extract the response headers from the "headers" string returned by
00133       // cURL, which are not actually all headers
00134       $lines = explode("\n",
00135             curl_getinfo($this->getCURL(), CURLINFO_HEADER_OUT));
00136       for ($i = 0; $i < count($lines); $i++) {
00137          $tmp = explode(":", $lines[$i]);
00138          if (count($tmp) < 2) {
00139             continue;
00140          }
00141          $key = $tmp[0];
00142          unset($tmp[0]);
00143          $value = implode(":", $tmp);
00144          $response->addHeader(trim($key), trim($value));
00145       }
00146 
00147       // curl does not include the Set-Cookie header in the return value of
00148       // CURLINFO_HEADER_OUT, so invoke elaborate hack to parse out cookies.
00149       foreach ($cookiearr as $key => $value) {
00150          $response->addCookie(trim($key), trim($value));
00151       }
00152 
00153       return $response;
00154    }
00155 
00159    public function getTimeout() {
00160       return $this->timeout;
00161    }
00162 
00166    public function setTimeout($timeout) {
00167       $this->timeout = $timeout;
00168    }
00169 
00170 }
00171 
00176 function read_header($curl_handle, $string) {
00177    global $location;
00178    global $cookiearr;
00179    global $curl_handle;
00180 
00181    if (!is_array($cookiearr)) {
00182       $cookiearr = array();
00183    }
00184 
00185    $length = strlen($string);
00186    if (!strncmp($string, "Set-Cookie:", 11)) {
00187       $cookiestr = trim(substr($string, 11, -1));
00188       $cookie = explode(';', $cookiestr);
00189       $cookie = explode('=', $cookie[0]);
00190       $cookiename = trim(array_shift($cookie));
00191       $cookiearr[$cookiename] = trim(implode('=', $cookie));
00192    }
00193    $cookie = "";
00194    if (trim($string) == "") {
00195       foreach ($cookiearr as $key => $value) {
00196          $cookie .= "$key=$value; ";
00197       }
00198       curl_setopt($curl_handle, CURLOPT_COOKIE, $cookie);
00199    }
00200    return $length;
00201 }
 All Data Structures Functions Variables