1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
| const METHOD_GET = 'GET'; const METHOD_POST = 'POST'; const METHOD_PUT = 'PUT'; const METHOD_DELETE = 'DELETE'; const CURL_TIMEOUT_IN_SECS = 15;
public static $successFullHttpCodes = array(200, 201, 204);
private function makeCurlCall($url, $postParams = array(), $method = self::METHOD_GET, $headers = array()) { $curl = curl_init(); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_VERBOSE, 0); curl_setopt($curl, CURLOPT_TIMEOUT, self::CURL_TIMEOUT_IN_SECS); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
if ($headers) { $usableHeaders = array(); foreach ($headers as $name => $value) { $usableHeaders[] = sprintf('%s: %s', $name, $value); }
curl_setopt($curl, CURLOPT_HTTPHEADER, $usableHeaders); }
if ($postParams) { curl_setopt($curl, CURLOPT_POSTFIELDS, $postParams); }
$data = curl_exec($curl); $this->checkForError($curl, $data);
return $data; }
private function checkForError($curl, $data) { $curlInfo = curl_getinfo($curl); if (isset($curlInfo['http_code']) && !in_array($curlInfo['http_code'], self::$successFullHttpCodes)) { throw new Exception($curlInfo['http_code'] ? $data : 'could not get a response from the service', $curlInfo['http_code'] ? $curlInfo['http_code'] : 500); } } |