CURL 請求外部介面 GET 和 POST
阿新 • • 發佈:2019-02-10
CURL 請求外部介面
/**
* GET方式請求
* @return mixed $result
*/
protected function getData()
{
// 取消SSL證書檢驗
curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->curlHandle, CURLOPT_URL, $this ->request_url);
curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($this->curlHandle, CURLOPT_HEADER, 0);
$result = curl_exec($this->curlHandle);
if (curl_errno($this->curlHandle)) {
return false;
} else {
return $result;
}
}
/**
* POST方式請求
* @param mixed $data 需要傳送的資料
* @return mixed $result
*/
private function postData($data)
{
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
// 取消SSL證書檢驗
curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->curlHandle, CURLOPT_URL, $this->request_url);
curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curlHandle, CURLOPT_POST, 1);
curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($this->curlHandle);
if (curl_errno($this->curlHandle)) {
echo curl_errno($this->curlHandle).':'.curl_error($this->curlHandle);
return false;
} else {
return $result;
}
}