Created
April 19, 2013 18:25
-
-
Save polidog/5422205 to your computer and use it in GitHub Desktop.
普通にHttpRequestクラス書いてみた。curl必須ね。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* HttpRequestクラス | |
* @property array $CURL_OPTS default curl options* | |
* @property array $lastHttpResponse 最後のリクエスト情報 | |
* @property array $options そのたcurlの設定情報 | |
*/ | |
class HttpRequest { | |
/** | |
* Curlのデフォルトオプション | |
* @var array | |
*/ | |
public static $CURL_OPTS = array( | |
CURLOPT_CONNECTTIMEOUT => 10, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_TIMEOUT => 60, | |
CURLOPT_USERAGENT => 'polidog-client-0.1.0', | |
CURLOPT_SSLVERSION => 3, | |
CURLINFO_HEADER_OUT => true, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_MAXREDIRS => 5, | |
); | |
private $lastHttpResponse; | |
private $option = array(); | |
/** | |
* コンストラクタ | |
* @param \Polidog\Bitbucket\Log\LogInterface $log | |
* @param array $option CURL設定情報 | |
*/ | |
public function __construct($option = array()) { | |
$this->option = $option; | |
} | |
/** | |
* GETリクエストを送信する | |
* @param string $url リクエスト先URI | |
* @param string $params query string | |
* @return array | |
*/ | |
public function doGet($url, $params = array()) { | |
return $this->httpRequest('get', $url, $params); | |
} | |
/** | |
* POSTリクエストを送信する | |
* @param string $url request uri | |
* @param string $data body post data | |
* @return array | |
*/ | |
public function doPost($url, $data) { | |
return $this->httpRequest('post', $url, $data); | |
} | |
/** | |
* PUTリクエストを送る | |
* @param string $url request uri | |
* @param array $data body put data | |
* @return array | |
*/ | |
public function doPut($url, $data) { | |
return $this->httpRequest('put', $url, $data); | |
} | |
/** | |
* DELETEリクエストを送る | |
* @param string $url request uri | |
* @param array $params | |
* @return array | |
*/ | |
public function doDelete($url, $params) { | |
return $this->httpRequest('delete', $url, $params); | |
} | |
/** | |
* 最後のリクエスト情報を取得する | |
* @return array | |
*/ | |
public function getHttpResponse() { | |
return $this->lastHttpResponse; | |
} | |
/** | |
* オプションをセットする | |
* @param array|string $key | |
* @param mixed $value | |
* @return \Polidog\Bitbucket\Request | |
*/ | |
public function setOption($key, $value = null) { | |
if (is_array($key)) { | |
foreach ($key as $k => $v) { | |
$this->setOption($k, $v); | |
} | |
} else { | |
$this->option[$key] = $value; | |
; | |
} | |
return $this; | |
} | |
/** | |
* 設定情報を取得する | |
* @return type | |
*/ | |
public function getOption($key = null) { | |
if (!is_null($key)) { | |
if (isset($this->option[$key])) { | |
return $this->option[$key]; | |
} | |
return false; | |
} | |
return $this->option; | |
} | |
/** | |
* HTTPリクエストを送る | |
* @param strint $method httpMethod name | |
* @param string $url requets url | |
* @param array $data get parameter or post and put body data | |
* @return array | |
* @throws \Polidog\Bitbucket\Exception\BitbucketRequestException | |
*/ | |
public function httpRequest($method, $url, $data = null) { | |
$method = strtolower($method); | |
$curl = curl_init(); | |
$opts = static::$CURL_OPTS; | |
if (!is_null($this->option)) { | |
$opts = $this->option + $opts; | |
} | |
$opts[CURLOPT_URL] = $url; | |
switch ($method) { | |
case 'get': | |
$query = null; | |
if (!empty($data)) { | |
$query = http_build_query($data, '', '&'); | |
} | |
if (!empty($query)) { | |
$sp = "?"; | |
if (strpos($url, '?') !== false) { | |
$sp = '&'; | |
} | |
$url .= $sp . $query; | |
} | |
break; | |
case 'post': | |
$json = json_encode($data); | |
$opts[CURLOPT_HTTPHEADER] = array('Content-Type: application/json; charset=utf8', 'Content-Length: ' . strlen($json)); | |
$opts[CURLOPT_POST] = true; | |
$opts[CURLOPT_POSTFIELDS] = $json; | |
$opts[CURLOPT_SSL_VERIFYPEER] = false; | |
break; | |
case 'put': | |
$opts[CURLOPT_CUSTOMREQUEST] = 'PUT'; | |
$opts[CURLOPT_POSTFIELDS] = http_build_query($data); | |
$opts[CURLOPT_SSL_VERIFYPEER] = false; | |
break; | |
case "delete": | |
if (!empty($data)) { | |
$json = json_encode($data); | |
$opts[CURLOPT_POSTFIELDS] = $json; | |
$opts[CURLOPT_CUSTOMREQUEST] = "DELETE"; | |
$opts[CURLOPT_HTTPHEADER] = array('Content-Length: ' . strlen($json)); | |
} else { | |
$opts[CURLOPT_CUSTOMREQUEST] = "DELETE"; | |
} | |
break; | |
default: | |
$json = json_encode($data); | |
$opts[CURLOPT_POSTFIELDS] = $json; | |
$opts[CURLOPT_CUSTOMREQUEST] = $method; | |
$opts[CURLOPT_HTTPHEADER] = array('Content-Length: ' . strlen($json)); | |
break; | |
} | |
curl_setopt_array($curl, $opts); | |
$content = curl_exec($curl); | |
$headers = curl_getinfo($curl); | |
$errorMessage = curl_error($curl); | |
$errorNo = curl_errno($curl); | |
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
$this->lastHttpResponse = array( | |
'headers' => $headers, | |
'content' => $content, | |
'errorMessage' => $errorMessage, | |
'errorNo' => $errorNo, | |
); | |
if ($errorMessage) { | |
$e = new BitbucketRequestException(array( | |
'error_code' => $errorNo, | |
'error' => array( | |
'message' => $errorMessage, | |
'type' => 'CurlException' | |
), | |
), $responseCode); | |
curl_close($curl); | |
throw $e; | |
} | |
curl_close($curl); | |
return json_decode($content, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment