PHP cURL和file_get_contents [英] PHP cURL vs file_get_contents

查看:141
本文介绍了PHP cURL和file_get_contents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

访问REST API时,这两个代码如何区别?

How do these two pieces of code differ when accessing a REST API?

$result = file_get_contents('http://api.bitly.com/v3/shorten?login=user&apiKey=key&longUrl=url');

$ch = curl_init('http://api.bitly.com/v3/shorten?login=user&apiKey=key&longUrl=url');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

他们都产生相同的结果,根据

They both produce the same result, judging by

print_r(json_decode($result))


推荐答案

file_get_contents()是一个简单的螺丝刀。非常适合简单的GET请求,其中头,HTTP请求方法,超时,cookiejar,重定向和其他重要的事情都无所谓。

file_get_contents() is a simple screwdriver. Great for simple GET requests where the header, HTTP request method, timeout, cookiejar, redirects, and other important things do not matter.

fopen )流上下文或cURL与< a href =http://us2.php.net/manual/en/function.curl-setopt.php> setopt 是你可以想到的每一个位和选项的动力练习。

fopen() with a stream context or cURL with setopt are powerdrills with every bit and option you can think of.

这篇关于PHP cURL和file_get_contents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆