PHP curl post/get數據函數

  • 2019 年 10 月 6 日
  • 筆記

Post

function postData($url, $data)  {  $ch = curl_init();  $timeout = 300;  curl_setopt($ch, CURLOPT_URL, $url);  curl_setopt($ch, CURLOPT_REFERER, ""); //來路  curl_setopt($ch, CURLOPT_POST, true);  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  $handles = curl_exec($ch);  curl_close($ch);  return $handles;  }

Get

function getData($url)  {  $ch = curl_init();  $timeout = 300;  curl_setopt($ch, CURLOPT_URL, $url);  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  curl_setopt($ch, CURLOPT_HEADER, 0);  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  $handles = curl_exec($ch);  curl_close($ch);  return $handles;  }