Home > Backend Development > PHP Tutorial > Use PHP code to implement the request proxy and reverse proxy of Baidu Wenxinyiyan API interface

Use PHP code to implement the request proxy and reverse proxy of Baidu Wenxinyiyan API interface

WBOY
Release: 2023-08-13 08:10:02
Original
1240 people have browsed it

Use PHP code to implement the request proxy and reverse proxy of Baidu Wenxinyiyan API interface

Use PHP code to implement the request proxy and reverse proxy of Baidu Wenxin Yiyan API interface

Overview:
Baidu Wenxin Yiyan is a provider that provides API interfaces for various random sentences and celebrity quotes can be used in websites, apps and other applications. But for some reasons, we may need to call the API with the help of a proxy or reverse proxy to avoid some limitations or better control the requests.

Request proxy:
Request proxy refers to sending our request to an intermediate server (proxy server), and then the proxy server sends it to the Baidu Wenxin Yiyan API interface on its behalf. This method can hide our real IP address, disguise the request for first-class information, and improve the success rate of the request.

First, we need to create a proxy server. You can use PHP to create a simple HTTP server that listens to a specified port, such as 8000. The following is a simple sample code:

<?php
// 创建一个代理服务器
$proxy = stream_socket_server('tcp://127.0.0.1:8000', $errno, $errstr);
if (!$proxy) {
    die("创建代理服务器失败:$errstr ($errno)");
}

while (true) {
    // 接受客户端连接请求
    $client = stream_socket_accept($proxy);
    if ($client) {
        // 从客户端读取请求
        $request = fread($client, 8192);

        // 修改请求头,伪装为百度文心一言API的请求
        $request = str_replace(
            'Host: localhost:8000',
            'Host: api.lovelive.tools', 
            $request
        );

        // 创建与API接口的连接
        $api = stream_socket_client('tcp://api.lovelive.tools:80', $errno, $errstr, 30);
        if ($api) {
            // 向API接口发送请求
            fwrite($api, $request);

            // 获取API的响应并返回给客户端
            while (!feof($api)) {
                fwrite($client, fread($api, 8192));
            }
            fclose($api);
        } else {
            fclose($client);
        }
    }
}
fclose($proxy);
Copy after login

Save the above code as a proxy.php file and run it through the command line:

php proxy.php
Copy after login

At this time, our proxy server is running on 127.0. 0.1: Port 8000 is up.

Next, we can send the request to the Baidu Wenxin Yiyan API interface by making a request to the proxy server and obtain the response result. For example, we can use the curl command line tool to send a request:

curl -x localhost:8000 https://api.lovelive.tools/api/SweetNothings/1
Copy after login

Reverse proxy:
Reverse proxy refers to configuring the server to forward the request to another server when the client sends a request. and returns the server's response to the client. This approach can achieve load balancing and high availability.

First, we need to configure the web server to forward all requests to Baidu Wenxin Yiyan API to our reverse proxy server. The following is a sample Nginx configuration file:

server {
    listen 80;
    server_name api.mydomain.com;
    
    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host api.lovelive.tools;
    }
}
Copy after login

The above configuration forwards the request to the proxy server we created above (running on localhost:8000).

In the reverse proxy server, our job is to accept the client's request, forward it to the Baidu Wenxin Yiyan API interface, and return its response to the client. The following is a simple sample code:

<?php
// 创建与API接口的连接
$api = stream_socket_client('tcp://api.lovelive.tools:80', $errno, $errstr, 30);
if ($api) {
    // 从客户端读取请求
    $request = file_get_contents('php://input');

    // 向API接口发送请求
    fwrite($api, $request);

    // 获取API的响应并返回给客户端
    while (!feof($api)) {
        echo fread($api, 8192);
    }
    fclose($api);
} else {
    header('HTTP/1.1 500 Internal Server Error');
    echo "与API接口连接失败";
}
Copy after login

Save the above code as reverse_proxy.php file and run it through the command line:

php -S localhost:8000 reverse_proxy.php
Copy after login

At this time, our reverse proxy server is running localhost:8000 port is up.

Next, the client can directly send a request to api.mydomain.com, and then the reverse proxy server will forward the request to the Baidu Wenxin Yiyan API interface and return its response to the client.

Summary:
The above is the method of using PHP code to implement the request proxy and reverse proxy of Baidu Wenxin Yiyan API interface. Through proxies and reverse proxies, we can better control requests while protecting and optimizing our applications. Whether it is a request proxy or a reverse proxy, it can be configured and modified according to actual needs to meet different business needs.

The above is the detailed content of Use PHP code to implement the request proxy and reverse proxy of Baidu Wenxinyiyan API interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template