PHP cURL 与 file_get_contents [英] PHP cURL vs file_get_contents

查看:55
本文介绍了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() 带有 流上下文 或带有 setopt 的 cURL 是具有您能想到的所有功能和选项的动力练习.

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天全站免登陆