在讲区别前大家对file_get_contents 只是停留在get 方法其实file_get_contents也可以进行post请求该方法如下
- $content = [];
- $options = array(
- 'http' => array(
- 'method' => 'POST',
- // header 需要设置为 JSON
- 'header' => 'Content-type:application/json',
- 'content' => json_encode($content,true),
- // 超时时间
- 'timeout' => 60
- )
- );
- $context = stream_context_create( $options );
- $result = file_get_contents( $url, false, $context );*
curl 的封装方法 post get form-post
- /**
- * @param $url
- * @param $headers
- * @param $data
- * @return bool|string
- * curl-post
- */
- function POST($url,$headers,$data)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl,CURLOPT_HEADER,0);
- curl_setopt($curl, CURLOPT_NOBODY, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_ENCODING, "gzip");
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- $html = curl_exec($curl);
- curl_close($curl);
- return $html;
- }
-
- /**
- * @param $url
- * @param $headers
- * @param $data
- * @return bool|string
- * curl-get
- */
- function GET($url,$headers,$data)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl,CURLOPT_HEADER,0);
- curl_setopt($curl, CURLOPT_NOBODY, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_ENCODING, "gzip");
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- // curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- $html = curl_exec($curl);
- curl_close($curl);
- return $html;
- }
-
-
- protected function curlForm($url, $post = '', $header = '')
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
-
- if (!empty($header)) {
- curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
- }
-
- curl_setopt($ch, CURLOPT_FAILONERROR, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //读取超时
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
-
- //连接超时
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
-
- //https 请求
- if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
-
- if (!empty($post))
- {
- //设置post数据
- curl_setopt ( $ch, CURLOPT_POST, true );
- curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query($post));
- }
- $response = curl_exec($ch);
-
-
- if (curl_errno($ch)) {
- throw new Exception(curl_error($ch),0);
- } else {
- $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- if (200 !== $httpStatusCode && 201 !== $httpStatusCode)
- {
- throw new Exception($response,$httpStatusCode);
- }
- }
- curl_close($ch);
- return $response;
- }
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库。