Lumen 使用 Guzzle 替代 curl

Step1. 创建lumen项目

composer create-project laravel/lumen guzzle-demo

Step2. 使用composer引入guzzle包

composer require guzzlehttp/guzzle
image

Step3. 使用

<?php

namespace App;
/**
 * Created by PhpStorm.
 * User: summer
 * Date: 18-12-20
 * Time: 下午2:54
 */

use GuzzleHttp\Psr7;

class Guzzle
{
    public static  function get()
    {
        $client = new \GuzzleHttp\Client();

        $url = 'api-chr.com/admin/users/nationalities';
        $array = [
            'headers' => [],
            'query' => [
                'search_name'=>'中'
            ],
            'http_errors' => false   #支持错误输出
        ];
        $response = $client->request('GET', $url, $array);
        dump($response->getStatusCode());   #打印响应信息
        dump(json_decode($response->getBody()->getContents()));   #输出结果
    }

    static public function post()
    {
        $client = new \GuzzleHttp\Client();

        $url = 'api-chr.com/admin/users/nationalities';
        $array = [
            'json' => [
                'name'=>'意大利',
                'en_name'=>'Italy',
                'kyc_type'=>1,
                'order'=>0,
                'is_forbid'=>0,
            ],
            'query' => [],
            'http_errors' => false
        ];
        $response = $client->request('post', $url, $array);
        dump($response->getStatusCode());   #打印响应信息
        dump(json_decode($response->getBody()->getContents()));   #输出结果
    }

    public static  function put()
    {
        $client = new \GuzzleHttp\Client();

        $url = 'api-chr.com/admin/users/nationalities/6';
        $array = [
            'json' => [
                'name'=>'意大利1',
                'en_name'=>'Italy1',
                'kyc_type'=>1,
                'order'=>0,
                'is_forbid'=>0,
            ],
            'query' => [],
            'http_errors' => false
        ];
        $response = $client->request('put', $url, $array);
        dump($response->getStatusCode());   #打印响应信息
        dump(json_decode($response->getBody()->getContents()));   #输出结果
    }

    public static  function delete()
    {
        $client = new \GuzzleHttp\Client();

        $url = 'api-chr.com/admin/users/nationalities/6';
        $array = [
            'json' => [],
            'query' => [],
            'http_errors' => false
        ];
        $response = $client->request('delete', $url, $array);
        dump($response->getStatusCode());   #打印响应信息
        dump(json_decode($response->getBody()->getContents()));   #输出结果
    }

 public static  function upload()
    {
        $client = new \GuzzleHttp\Client();
        $body = fopen('/home/summer/图片/dog1.jpg', 'r');
        $response = $client->request('POST', 'http://account.chr.test/upload',
            [
                'multipart' => [
                    [
                        'name' => 'body',
                        'contents' => fopen('/home/summer/图片/dog1.jpg', 'r')
                    ],
                ]
            ]
        );
        dump($response->getStatusCode());   #打印响应信息
        dump($response->getBody());
        dump(json_decode($response->getBody()->getContents()));   #输出结果
    }
}

官网文档 http://docs.guzzlephp.org/en/latest/index.html

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 2

dump(json_decode($response->getBody()->getContents())); #输出结果
直接转json
dump($response->json()); #输出结果

5年前 评论

@lovecn Call to undefined method GuzzleHttp\Psr7\Response::json()

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!