/** * Set curl default settings for connections. * * @param resource $rCurl * @param string $sUrl */ private function setDefaults($rCurl, $sUrl) { \curl_setopt($rCurl, \CURLOPT_URL, $sUrl); \curl_setopt($rCurl, \CURLOPT_ENCODING, 'gzip, deflate'); \curl_setopt($rCurl, \CURLOPT_USERAGENT, self::AGENT_NAME); //Transfer settings \curl_setopt($rCurl, \CURLOPT_FAILONERROR, true); \curl_setopt($rCurl, \CURLOPT_FRESH_CONNECT, true); \curl_setopt($rCurl, \CURLOPT_RETURNTRANSFER, true); \curl_setopt($rCurl, \CURLOPT_AUTOREFERER, true); \curl_setopt($rCurl, \CURLOPT_FORBID_REUSE, true); //Connection settings \curl_setopt($rCurl, \CURLOPT_CONNECTTIMEOUT, $this->oConfig->getConnectTimeout()); \curl_setopt($rCurl, \CURLOPT_TIMEOUT, $this->oConfig->getTimeout()); //Redirection settings may not be required \curl_setopt($rCurl, \CURLOPT_FOLLOWLOCATION, true); \curl_setopt($rCurl, \CURLOPT_MAXREDIRS, 1); \curl_setopt($rCurl, \CURLOPT_VERBOSE, $this->oConfig->isVerbose()); }
/** * Performs a GET request against a given URL and port. * * @param string $sUrl * @param int $iPort - Defaults to null. * @return string */ public function get($sUrl, $iPort = null) { $errno = 0; $errstr = ''; //Unfortunately this will throw a fatal error if the hostname is invalid!!!. //I do not use error suppresion, it is not good practice. //Hopefully fixed in PHP 7, all exceptions, no fatal errors, instead runtime exceptions. $fp = \fsockopen($sUrl, $iPort, $errno, $errstr, $this->oConfig->getConnectTimeout()); if (!$fp) { $this->sError = $errstr; throw new ClientException('Cannot create connection to ' . $sUrl); } //Build the HTTP header. $sGetHeaders = 'GET ' . $this->getPath($sUrl) . ' HTTP/1.1' . self::HEADER_EOL; $sGetHeaders .= 'HOST: ' . $this->getHostname($sUrl) . self::HEADER_EOL; $sGetHeaders .= $this->setDefaults(); $sGetHeaders .= $this->setupRequestHeaders(); $sGetHeaders .= $this->setupBasicAuthHeader(); $sGetHeaders .= self::HEADER_EOL; $this->setRequestDetails($sGetHeaders); $sResponse = $this->readResponse($fp, $sGetHeaders); \fclose($fp); return $sResponse; }