/**
* GET 請求
* @param string $url
*/
static public function http_get($url) {
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
}curl
/**
* 以post方式提交xml到對應的接口url
* @param type $url
* @param type $postdata
* @param type $second
* @return boolean
*/
static public function http_post($url, $postdata, $second = 30) {
$ch = curl_init();
curl_setopt($ch, CURLOP_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
/* post提交方式 */
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
return $data;
} else {
return false;
}
}post
static function http_ssl_post($url, $data, $ssl_cer = null, $ssl_key = null, $second = 30) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
/* 要求結果爲字符串且輸出到屏幕上 */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
/* 設置證書 */
if (!is_null($ssl_cer)) {
if (is_file($ssl_cer)) {
$_cer_path = $ssl_cer;
} else {
$_cer_path = RUNTIME_PATH . 'Data/client_cer.pem';
file_put_contents($_cer_path, $ssl_cer);
}
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, $_cer_path);
}
if (!is_null($ssl_key)) {
if (is_file($ssl_key)) {
$_key_path = $ssl_key;
} else {
$_key_path = RUNTIME_PATH . 'Data/client_key.pem';
file_put_contents($_key_path, $ssl_key);
}
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, $_key_path);
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
if ($result) {
return $result;
} else {
return false;
}
}url