PHP cURL 与 file_get_contents

标签 php curl file-get-contents

这两段代码在访问 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);

它们都产生相同的结果,判断为

print_r(json_decode($result))

最佳答案

file_get_contents() 是一个简单的 Screwdriver 。非常适合 header 、HTTP 请求方法、超时、cookiejar、重定向和其他重要事项无关紧要的简单 GET 请求。

fopen() 带有 stream context或带有 setopt 的 cURL是你能想到的每一个细节和选项的电钻。

关于PHP cURL 与 file_get_contents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11064980/

相关文章:

curl - Slack API - 使用 curl 创建帖子

php - 使用 Curl 和 PHP 保持 session 活跃

php - urlencode 和 file_get_contents

php - 保护 PHP 文件

PHP Paypal API : How to Capture Authorized Payment Partially?

php - 如何在 PHP 中的 GET 中传递一个数组?

php - jQuery 使用 AJAX 显示从 PHP 文件获取的 PDF 数据

php - 输出缓冲区与 PHP 中的 file_get_contents

php - 选择多个不起作用

javascript - 如何在 Node.js 中使用 MySQL 查询更新 MySQL 数据库中的 JSON 记录?