介紹:
? ?Guzzle是一個PHP的HTTP客戶端,用來輕而易舉地發(fā)送請求,并集成到我們的WEB服務上.
安裝:
????composer require guzzlehttp/guzzle
基礎:
? ??use GuzzleHttp\Client;
? ??$client = new Client();
? ? $request =?$client->request('GET', 'https://www.google.com/');
? ? $headers = $request->getHeaders();
? ? $contents = $request->getBody()->getContents();
進階:
? ? 模擬POST提交表單
????$options = [
? ? ????'form_params' => [
? ? ? ? ????'username' => 'zhangsan',
? ? ? ? ????'password' => '123456'
????????]
????];
? ??$client->request('POST', 'https://www.google.com/', $options);
? ? 自定義headers進行請求
? ??$options = [
? ? ? ? 'headers' => [
? ??????????'Authorization' => 'access_token',
? ? ? ? ? ??'user-agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
????????????'x-requested-with' => 'XMLHttpRequest'
????????];
????];
????$client->request('POST', 'https://www.google.com/', $options);
? ? 攜帶cookie進行請求:
? ??use GuzzleHttp\Cookie\CookieJar;
? ??$jar = new CookieJar();
? ? $cookies = [
? ? ? ? 'sessionid' => 'sessionid',
? ? ? ? 'user_id=' => 'user_id'
????];
? ??$jar->fromArray($cookies, 'https://www.google.com/');
? ??$options = [
? ? ? ? 'cookies' => $jar;
????];
? ??$client->request('POST', 'https://www.google.com/', $options);
文檔地址:https://docs.guzzlephp.org/en/stable/index.html