Example #1
0
 public function go($get = array(), $post = array(), $postContentType = '')
 {
     if ('' == trim($this->_url)) {
         throw new Exception('Must specify a url before using the Http Client.');
     }
     $persist = $this->_connection;
     if (!$persist) {
         $this->pickup();
     }
     if (is_array($get)) {
         $query = $this->buildQuery($get);
     } else {
         if (!is_null($get) && '' != trim($get)) {
             $cleanQuery = ltrim($get, '?');
             $query = '?' . Saf_UrlRewrite::makeUrlSafe($cleanQuery);
         } else {
             $query = '';
         }
     }
     $headers = array_merge($this->_headers, $this->_tempHeaders);
     $this->clearTempHeaders();
     $fullUrl = $this->_url . $this->_actionUrl . $query;
     $options = array();
     $options[CURLOPT_URL] = $fullUrl;
     $debugPost = '';
     if (is_array($post) && count($post) > 0) {
         $debugPost = json_encode($debugPost, JSON_FORCE_OBJECT);
         $options[CURLOPT_POSTFIELDS] = $post;
         //#TODO #2.0.0 the path reported by this client for any sent files will be fully qualified. if the server is too stupid to handle this, a work around will be needed, possibly chdir...
     } else {
         if (!is_array($post) && '' != trim($post)) {
             $debugPost = $post;
             $options[CURLOPT_POST] = TRUE;
             if ($postContentType === self::UNENCODED_POST_DATA) {
                 $post = urlencode($post);
             }
             $options[CURLOPT_POSTFIELDS] = $post;
             if ('' != $postContentType && $postContentType !== self::UNENCODED_POST_DATA) {
                 $headers[] = 'Content-type: ' . $postContentType;
             }
         } else {
             #TODO #2.0.0 make sure switching back to GET mode when persisting works properly
             if (array_key_exists(CURLOPT_POST, $options)) {
                 $options[CURLOPT_POST] = FALSE;
                 //or unset?
             }
         }
     }
     if ($this->_authenticate) {
         $username = $this->_user;
         $password = $this->_password;
         $options[CURLOPT_USERPWD] = "{$username}:{$password}";
     }
     $options[CURLOPT_HTTPHEADER] = $headers;
     if ($this->_antiqueServerMode) {
         $options[CURLOPT_HTTPHEADER] = array('Expect:');
     }
     curl_setopt_array($this->_connection, $options);
     try {
         $result = curl_exec($this->_connection);
         $resultHead = '';
         $resultRest = $result;
         $resultHeadEnd = strpos($resultRest, "\r\n\r\n");
         //			$count = 0;
         //			$head = str_replace("\r\n", "\\r\\n<br/>", $resultHead);
         //			$body = str_replace("\r\n", "\\r\\n<br/>", $resultRest);
         while ($resultHeadEnd !== FALSE) {
             //				$count++;
             $resultHead .= substr($resultRest, 0, $resultHeadEnd + 4);
             $resultRest = substr($resultRest, $resultHeadEnd + 4);
             $resultHeadEnd = strpos($resultRest, "\r\n\r\n");
             // 				if (
             // 					strpos($resultRest, 'HTTP') === 0
             // 					|| strpos($resultRest, '\r\n\r\n') !== FALSE
             // 				) {
             // 				} else {
             // 					$resultHeadEnd = FALSE;
             // 				}
             // 				$resultHeadEnd =
             // 					(
             // 						strpos($resultRest,'HTTP') !== 0
             // 					//	&& strpos($resultRest,'Content-Length:') !== 0
             // 					) ? FALSE
             // 					: strpos($resultRest,"\r\n\r\n");
             // 				if (strpos($resultRest,'\r\n') === 0) {
             // 					$resultHead .= (
             // 							substr($result, 0, 2)
             // 					);
             // 					$resultRest = substr($result, 2);
             // 					$resultHeadEnd +=2;
             // 				}
             $head = str_replace("\r\n", "\\r\\n<br/>", $resultHead);
             $body = str_replace("\r\n", "\\r\\n<br/>", $resultRest);
             //Saf_Debug::outData(array($resultHeadEnd,$head,$body));
         }
         //			if ($count > 2) {
         //				die('server sent too many continues'); //#TODO #1.1.0
         //			}
         if ($this->_debugEnabled) {
             Saf_Debug::outData(array($fullUrl, htmlentities($debugPost), htmlentities($head), htmlentities($body)));
         }
         $resultBody = $resultRest;
         $this->_lastError = curl_error($this->_connection);
         $resultInfo = curl_getinfo($this->_connection);
         $this->_lastResult = array('response' => $result, 'status' => $resultInfo, 'error' => $this->_lastError);
         $this->_lastStatus = $resultInfo['http_code'];
     } catch (Exception $e) {
         $this->_lastError = $e->getMessage();
         $this->_lastStatus = 'EXCEPTION';
         $return = array('url' => $fullUrl, 'status' => 500, 'error' => $this->_lastError, 'raw' => '', 'length' => 0, 'type' => '');
         if (Saf_Debug::isEnabled()) {
             $return['stack'] = $e->getTrace();
         }
         $this->_lastStatus = $return['status'];
         return $return;
     }
     if (!$persist) {
         $this->putdown();
     }
     $status = (int) $resultInfo['http_code'];
     $return = array('url' => $fullUrl, 'status' => $status, 'status_label' => array_key_exists($status, self::$_httpCodes) ? self::$_httpCodes[$status] : 'UNKNOWN', 'length' => $resultInfo['download_content_length'], 'type' => $resultInfo['content_type'], 'redirectCount' => $resultInfo['redirect_count'], 'sentHeaders' => array_key_exists('request_header', $resultInfo) ? $resultInfo['request_header'] : '', 'receivedHeaders' => $resultHead, 'raw' => $resultBody);
     if ($resultInfo['size_upload'] < $resultInfo['upload_content_length']) {
         $return['up'] = floor($resultInfo['size_upload'] / $resultInfo['upload_content_length'] * 100);
     }
     if ($resultInfo['size_download'] < $resultInfo['download_content_length']) {
         $return['down'] = floor($resultInfo['size_download'] / $resultInfo['download_content_length'] * 100);
     }
     if ($fullUrl != $resultInfo['url']) {
         $return['effectiveUrl'] = $resultInfo['url'];
     }
     if (array_key_exists('ssl_verify_result', $resultInfo) && 0 != $resultInfo['ssl_verify_result']) {
         $return['ssl_error_code'] = $resultInfo['ssl_verify_result'] . (array_key_exists($resultInfo['ssl_verify_result'], self::$_sslCodes) ? ' ' . self::$_sslCodes[$resultInfo['ssl_verify_result']] : ' unknown SSL connection error');
     }
     if ($status < 200 || $status >= 300) {
         $return['failedConnectionInfo'] = curl_getinfo($this->_connection);
         if ($post && Saf_Debug::isEnabled()) {
             if (is_array($post) && count($post) > 0) {
                 ob_start();
                 print_r($post);
                 $rawRequest = ob_get_contents();
                 ob_end_clean();
                 $return['request'] = $rawRequest;
             } else {
                 $return['request'] = $post;
             }
         }
     }
     return $return;
 }