Lumen 使用 Guzzle 替代 curl

summer-1994發表於2019-01-24

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 協議》,轉載必須註明作者和本文連結

相關文章