如何在php中使用curl重写file_get_contents函数

如何在php中使用curl重写file_get_contents函数?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

具体如下:

file_get_contents在连接不上的时候会提示Connection refused,有时候会带来不便;另外,curl的性能比file_get_contents高,所以用curl重写file_get_contents

function _file_get_contents($s) {
  $ret = "";
  $ch = curl_init($s);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
  curl_setopt($ch, CURLOPT_TIMEOUT, 0);
  $buffer = curl_exec($ch);
  curl_close($ch);
  if ($buffer === false || empty($buffer)) {
    $ret = "";
  } else {
    $ret = $buffer;
  }
  return $ret;
}

看完上述内容,你们掌握如何在php中使用curl重写file_get_contents函数的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!