set("connectionType", CONNECTION); if ($url) { $this->load($url); } } /** * Loads content from the given URL. */ public function load($url) { switch ($this->connectionType) { case "curl": $this->file = $this->getCURL($url); break; case "http": $this->file = $this->getHTTP($url); break; case "socket": $this->file = $this->getSOCKET($url); break; } if(!$this->file){ $e = new mb_exception("connector: unable to load: ".$url); return false; } return $this->file; } /** * Sets the environment variables. The following can be set: * - connectionType ("http", "curl", "socket") * - httpType ("get", "post") * - etc. */ public function set ($key, $value) { switch ($key) { case "connectionType": if ($this->isValidConnectionType($value)) { $this->connectionType = $value; } break; case "httpVersion": if (in_array($value, array("1.0", "1.1"))) { $this->httpVersion = $value; } else { $e = new mb_exception("class_connector.php: invalid http type '" . $value . "'"); } break; case "httpType": if (in_array(mb_strtoupper($value), array("POST", "GET"))) { $this->httpType = $value; } else { $e = new mb_exception("class_connector.php: invalid http type '" . $value . "'"); } break; case "httpPostData": $this->httpPostData = $value; break; case "httpContentType": if ($this->isValidHttpContentType($value)) { $this->httpContentType = $value; } break; } } private function isValidConnectionType ($value) { if (in_array(mb_strtoupper($value), array("HTTP", "CURL", "SOCKET"))) { return true; } else { $e = new mb_exception("class_connector.php: invalid connection type '" . $value . "'"); return false; } } private function isValidHttpContentType ($value) { $validHttpContentTypeArray = array("XML"); if (in_array(mb_strtoupper($value), $validHttpContentTypeArray)) { switch ($value) { case "XML": $this->httpContentType = "application/xml"; break; } return true; } else { $e = new mb_exception("class_connector.php: invalid HTTP content type '" . $value . "'"); return false; } } private function getCURL($url){ $ch = curl_init ($url); // curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); if(CONNECTION_PROXY != ""){ curl_setopt($ch, CURLOPT_PROXY,CONNECTION_PROXY.":".CONNECTION_PORT); } if(CONNECTION_PASSWORD != ""){ curl_setopt ($ch, CURLOPT_PROXYUSERPWD, CONNECTION_USER.':'.CONNECTION_PASSWORD); } curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $file = curl_exec ($ch); curl_close ($ch); return $file; } private function getHTTP($url){ if ($this->httpType == "get") { return @file_get_contents($url); } else { $urlComponentArray = parse_url($url); $host = $urlComponentArray["host"]; $port = $urlComponentArray["port"]; if ($port == "") { $port = 80; } $path = $urlComponentArray["path"]; $buf = ''; $fp = fsockopen($host, $port); fputs($fp, "POST $path HTTP/".$this->httpVersion . "\r\n"); fputs($fp, "Host: $host\r\n"); if ($this->isValidHttpContentType($this->httpContentType)) { fputs($fp,"Content-type: " . $this->httpContentType . "\r\n"); } fputs($fp, "Content-length: " . strlen($this->httpPostData) . "\r\n"); fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $this->httpPostData); $xmlstr = false; while (!feof($fp)) { $content = fgets($fp,4096); if( strpos($content, '\n"; } else { fputs ($fp, "GET ".$url." HTTP/1.0\r\n\r\n"); while (!feof($fp)) { $r .= fgets($fp,4096); } fclose($fp); return $r; } } } ?>