• file_get_contents 与curl 的对比


    在讲区别前大家对file_get_contents 只是停留在get 方法其实file_get_contents也可以进行post请求该方法如下

    1. $content = [];
    2. $options = array(
    3. 'http' => array(
    4. 'method' => 'POST',
    5. // header 需要设置为 JSON
    6. 'header' => 'Content-type:application/json',
    7. 'content' => json_encode($content,true),
    8. // 超时时间
    9. 'timeout' => 60
    10. )
    11. );
    12. $context = stream_context_create( $options );
    13. $result = file_get_contents( $url, false, $context );*

    curl 的封装方法 post get form-post

    1. /**
    2. * @param $url
    3. * @param $headers
    4. * @param $data
    5. * @return bool|string
    6. * curl-post
    7. */
    8. function POST($url,$headers,$data)
    9. {
    10. $curl = curl_init();
    11. curl_setopt($curl, CURLOPT_URL, $url);
    12. curl_setopt($curl,CURLOPT_HEADER,0);
    13. curl_setopt($curl, CURLOPT_NOBODY, 0);
    14. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    15. curl_setopt($curl, CURLOPT_ENCODING, "gzip");
    16. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    17. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    18. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    19. curl_setopt($curl, CURLOPT_POST, 1);
    20. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    21. $html = curl_exec($curl);
    22. curl_close($curl);
    23. return $html;
    24. }
    25. /**
    26. * @param $url
    27. * @param $headers
    28. * @param $data
    29. * @return bool|string
    30. * curl-get
    31. */
    32. function GET($url,$headers,$data)
    33. {
    34. $curl = curl_init();
    35. curl_setopt($curl, CURLOPT_URL, $url);
    36. curl_setopt($curl,CURLOPT_HEADER,0);
    37. curl_setopt($curl, CURLOPT_NOBODY, 0);
    38. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    39. curl_setopt($curl, CURLOPT_ENCODING, "gzip");
    40. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    41. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    42. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    43. // curl_setopt($curl, CURLOPT_POST, 1);
    44. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    45. $html = curl_exec($curl);
    46. curl_close($curl);
    47. return $html;
    48. }
    49. protected function curlForm($url, $post = '', $header = '')
    50. {
    51. $ch = curl_init();
    52. curl_setopt($ch, CURLOPT_URL, $url);
    53. if (!empty($header)) {
    54. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
    55. }
    56. curl_setopt($ch, CURLOPT_FAILONERROR, false);
    57. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    58. //读取超时
    59. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    60. //连接超时
    61. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    62. //https 请求
    63. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
    64. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    65. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    66. }
    67. if (!empty($post))
    68. {
    69. //设置post数据
    70. curl_setopt ( $ch, CURLOPT_POST, true );
    71. curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query($post));
    72. }
    73. $response = curl_exec($ch);
    74. if (curl_errno($ch)) {
    75. throw new Exception(curl_error($ch),0);
    76. } else {
    77. $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    78. if (200 !== $httpStatusCode && 201 !== $httpStatusCode)
    79. {
    80. throw new Exception($response,$httpStatusCode);
    81. }
    82. }
    83. curl_close($ch);
    84. return $response;
    85. }
    这二者简单封装后 使用上有什么区别?

    1. file_get_contents 每次请求都会重新做DNS查询,并不对 DNS信息进行缓存。但是CURL会自动对DNS信息进行缓存。对同一域名下的网页或者图片的请求只需要一次DNS查询。这大大减少了DNS查询的次数。所以CURL的性能比fopen /file_get_contents 好很多。

    2.file_get_contents 在请求HTTP时,使用的是http_fopen_wrapper,不会keeplive。而curl却可以。这样在多次请求多个链接时,curl效率会好一些。

    3. file_get_contents 函数会受到php.ini文件中allow_url_open选项配置的影响。如果该配置关闭了,则该函数也就失效了。而curl不受该配置的影响。

    4. file_get_contents处理频繁小的时候,用它感觉挺好的。没什么异常。如果你的文件被1k+人处理。那么你的服务器cpu就等着高升吧。所以建议自己和大家在以后写php代码的时候使用curl库。

  • 相关阅读:
    Ubuntu下目标检测YOLO系列网络安装OpenCV时Darknet编译出现的问题(pjreddie版本)
    2023年G2电站锅炉司炉证考试题库及G2电站锅炉司炉试题解析
    亲测解决no module named ‘PyQt5.QtCore‘
    WebDAV之葫芦儿·派盘 + ES文件浏览器
    Oracle创建索引的LOGGING | NOLOGGING区别
    Java --- IO流
    并查集の进阶用法
    有哪些不起眼却赚钱的行业?
    Win下Eclipse安装
    memcached的大key存储与slab钙化问题踩坑
  • 原文地址:https://blog.csdn.net/qq_63530862/article/details/133840289