Laravel 7.X PHP使用cURL获取json数据

$url = 'http://api.qq.jsososo.com/new/mv';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
$info = json_decode($result, true);
dd($info);

Laravel 7.X的新写法,刚才看了一下新的版本文档已经集成Guzzie

composer require guzzlehttp/guzzle

创建请求

use Illuminate\Support\Facades\Http;

$response = Http::get(‘http://test.com’);

get 方法返回一个 Illuminate\Http\Client\Response 的实例,该实例提供了大量的方法来检查请求的响应:

$response->body() : string;
$response->json() : array;
$response->status() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->header($header) : string;
$response->headers() : array;