curl get或post提交请求

/**请求接口get和post方式
 * @param $url
 * @param string $method
 * @param null $data
 * @return bool|mixed
 */
function api_request($url,$method = 'get',$data = null) {
   if(empty($url)) {
      return false;
   }
   $ch = curl_init();
   if('post' == strtolower($method) && !empty($data) && is_array($data)) {
      curl_setopt($ch,CURLOPT_POST,1);
      curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
   }elseif(!empty($data) && is_array($data)) {
      $params = http_build_query($data);
      $url .= "?$params";
   }
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

   $info = curl_getinfo($ch);//获取请求信息,调试查看信息使用
   $output = curl_exec($ch);
   curl_close($ch);
   return $output;
}
相关文章
相关标签/搜索