Laravel 框架 GuzzleHttp 使用方法

本文介绍了如何在Laravel框架中使用GuzzleHttp进行HTTP请求,包括发送POST请求、设置自定义headers以及处理cookie。参考资源包括Guzzle官方手册和GitHub仓库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

手册地址:http://docs.guzzlephp.org/en/stable/request-options.html#headers

Github:https://github.com/guzzle/guzzle 
Composer:https://packagist.org/packages/guzzlehttp/guzzle

发送请求

 
  1. use GuzzleHttp\Client;

  2.  
  3. $client = new Client([

  4. //跟域名

  5. 'base_uri' => 'http://localhost/test',

  6. // 超时

  7. 'timeout' => 2.0,

  8. ]);

  9.  
  10. $response = $client->get('/get'); //http://localhost/get

  11. $response = $client->delete('delete'); //http://localhost/get/delete

  12. $response = $client->head('http://localhost/get');

  13. $response = $client->options('http://localhost/get');

  14. $response = $client->patch('http://localhost/patch');

  15. $response = $client->post('http://localhost/post');

  16. $response = $client->put('http://localhost/put');

POST

 
  1. $response = $client->request('POST', 'http://localhost/post', [

  2. 'form_params' => [

  3. 'username' => 'webben',

  4. 'password' => '123456',

  5. 'multiple' => [

  6. 'row1' => 'hello'

  7. ]

  8. ]

  9. ]);

  •  
    1. //传json格式参数

    2. $response = $client->post('/xxx', [

    3.  
    4.             'headers' => ['Content-Type' => 'application/json'],//设置请求头为json

    5.  
    6.             'json' => [

    7.  
    8.                     'network_id' => 8,

    9.  
    10.                     'state' => 0,

    11.  
    12.                     'terminal_name' => ''

    13.  
    14.             ]

    15.  
    16.         ]);

    17.  
    18.         $body = $response->getBody()->getContents();

    19.  
    响应
 
  1. # 状态码

  2. $code = $response->getStatusCode(); // 200

  3. $reason = $response->getReasonPhrase(); // OK

  4.  
  5. # header

  6. // Check if a header exists.

  7. if ($response->hasHeader('Content-Length')) {

  8. echo "It exists";

  9. }

  10.  
  11. // Get a header from the response.

  12. echo $response->getHeader('Content-Length');

  13.  
  14. // Get all of the response headers.

  15. foreach ($response->getHeaders() as $name => $values) {

  16. echo $name . ': ' . implode(', ', $values) . "\r\n";

  17. }

  18.  
  19. # 响应体

  20. $body = $response->getBody();

  21. // Implicitly cast the body to a string and echo it

  22. echo $body;

  23. // Explicitly cast the body to a string

  24. $stringBody = (string) $body;

  25. // Read 10 bytes from the body

  26. $tenBytes = $body->read(10);

  27. // Read the remaining contents of the body as a string

  28. $remainingBytes = $body->getContents();

  • 自定义header
 
  1. // Set various headers on a request

  2. $client->request('GET', '/get', [

  3. //header

  4. 'headers' => [

  5. 'User-Agent' => 'testing/1.0',

  6. 'Accept' => 'application/json',

  7. 'X-Foo' => ['Bar', 'Baz']

  8. ],

  9. //下载

  10. 'save_to'=> $filename,

  11. //referer

  12. 'allow_redirects' => [

  13. 'referer' => '',

  14. ],

  15. ]);

cookie 访问

 
  1. $client = new \GuzzleHttp\Client();

  2.  
  3. $url = 'https://www.baidu.com/getUserInfo';

  4.  
  5. $jar = new \GuzzleHttp\Cookie\CookieJar();

  6. $cookie_domain = 'www.baidu.com';

  7. $cookies = [

  8. 'BAIDUID' => '221563C227ADC44DD942FD9E6D577EF2CD',

  9. ];

  10.  
  11. $cookieJar = $jar->fromArray($cookies, $cookie_domain);

  12.  
  13. $res = $client->request('GET', $url, [

  14. 'cookies' => $cookieJar,

  15. // 'debug' => true,

  16. ]);

  17. $body = $res->getBody();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值