asminog/yii2-proxy HTTP proxy action extension for the Yii framework

HTTP Proxy Extension for Yii 2

  1. Installation
  2. Usage on domain.com
  3. Example request through proxy on domain.com

This is a simple proxy for Yii2 framework. This extension provides the HTTP proxy action for the Yii framework 2.0.

For license information check the LICENSE-file.

Build Status Build Status

GitHub repo file count GitHub code size in bytes

Installation

composer require asminog/yii2-proxy

Usage on domain.com

use asminog\proxy\ProxyAction;

class SiteController extends Controller
{
    public function actions()
    {
        return [
            'proxy' => [
                'class' => ProxyAction::class,
                // 'accessToken' => 'your-access-token', // - set access token for secure requests
                // 'throw404Exception' => true, // - show 404 error if access token is not valid or request url is not valid
                // 'proxyHeaders' => ['User-Agent', 'Content-Type'], // - set headers for proxy request
                'proxyHeaders' => ['Authorization', 'Content-Type'], // - set headers for chatgpt proxy request
                // 'proxyCookies' => ['cookie1', 'cookie2'], // - set cookies for proxy request
            ],
        ];
    }
}

Example request through proxy on domain.com


        $this->client = new Client([
            'transport' => CurlTransport::class,
            'baseUrl' => 'https://domain.com/site/proxy', // - set url to your proxy action
            'requestConfig' => [
                'format' => Client::FORMAT_JSON,
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                    'Content-Type' => 'application/json',
                    'X-Proxy-Url' => 'https://api.openai.com/v1/chat/completions', // - set url to your api
//                    'X-Access-Token' => 'your-access-token' // - set access token for secure requests
                ],
            ],
        ]);

        $response = $this->client->post('', [
            'model' => 'gpt-3.5-turbo',
            'messages' => [
                [
                    'role' => 'user',
                    'content' => 'Hello, how are you?',
                ],
            ],
        ]);

        if ($response->isOk) {
            $data = $response->data;
            // - do something with response data
        } else {
            // - handle error
        }
        $this->client->close();
0 0
1 follower
22 downloads
Yii Version: Unknown
License: MIT
Category: Others
Tags: action, proxy
Developed by: asminog
Created on: Apr 2, 2024
Last updated: (not set)
Packagist Profile
Github Repository

Related Extensions