php 使用 CURL 获取数据

function http_curl($url, $type = 'get', $data = ''){
  $cl = curl_init();
  curl_setopt($cl, CURLOPT_URL, $url);
  curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
  if($type == 'post'){
    curl_setopt($cl, CURLOPT_POST, 1);
    curl_setopt($cl, CURLOPT_POSTFIELDS, $data);
  }
  $output = curl_exec($cl);
  curl_close($cl);
  return $output;
}

参数说明:curl

  $url :要请求的url地址,若是是get方式请求,能够把参数直接加到url后面post

  $type:请求方式url

  $data:post方式请求时携带的参数spa

curl_init()  初始化一个cURL会话code

curl_setopt()  设置一个cURL传输选项blog

curl_exec()  执行一个cURL会话get

curl_close()  关闭一个cURL会话it

相关文章
相关标签/搜索