Di bawah ini adalah implementasi dasar dari web proxy/anonymizer di PHP:

<?php

// Function to fetch the content from the target URL and return it
function fetchURL($url, $headers, $method, $data = null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    // Set method and data for POST requests
    if ($method === 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

// Check if URL parameter is provided
if (isset($_GET['url'])) {
    $url = $_GET['url'];

    // Forward request headers
    $headers = getallheaders();
    
    // Forward request method
    $method = $_SERVER['REQUEST_METHOD'];

    // Forward request data for POST requests
    $data = null;
    if ($method === 'POST') {
        $data = file_get_contents('php://input');
    }

    // Fetch content from the target URL
    $targetResponse = fetchURL($url, $headers, $method, $data);

    // Extract response headers and content
    list($targetHeaders, $targetContent) = explode("\r\n\r\n", $targetResponse, 2);

    // Forward response headers to the client
    $headerLines = explode("\r\n", $targetHeaders);
    foreach ($headerLines as $header) {
        header($header);
    }

    // Output the content to the client
    echo $targetContent;
} else {
    // URL parameter is missing
    http_response_code(400);
    echo "Missing URL parameter";
}

?>

Skrip PHP ini bertindak sebagai proxy/anonimizer web dasar. Itu mendengarkan permintaan dan meneruskannya ke URL yang ditentukan. URL yang diminta harus diberikan sebagai nama parameter kueri url.

Untuk menggunakan proxy ini, simpan skrip di server Anda dan akses melalui browser atau kirim permintaan HTTP ke sana secara terprogram. Misalnya:

GET http://example.com/proxy.php?url=https://example.com

Ini akan mengambil konten dari https://example.com melalui proksi. Perhatikan bahwa ini adalah implementasi sederhana dan mungkin tidak menangani semua jenis permintaan atau respons. Selain itu, ini tidak menyertakan fitur seperti caching atau anonimisasi.

Anda dapat menambahkan fitur caching dan anonimisasi dasar ke skrip proxy PHP. Ini versi terbarunya:

<?php

// Function to fetch the content from the target URL and return it
function fetchURL($url, $headers, $method, $data = null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    // Set method and data for POST requests
    if ($method === 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

// Function to cache response content
function cacheContent($url, $content) {
    // Implement caching logic here
    // For simplicity, you can use file-based caching
    $cacheFileName = md5($url) . '.cache';
    file_put_contents($cacheFileName, $content);
}

// Function to retrieve cached content
function getCachedContent($url) {
    // Implement caching logic here
    $cacheFileName = md5($url) . '.cache';
    if (file_exists($cacheFileName)) {
        return file_get_contents($cacheFileName);
    }
    return null;
}

// Check if URL parameter is provided
if (isset($_GET['url'])) {
    $url = $_GET['url'];

    // Check if content is cached
    $cachedContent = getCachedContent($url);
    if ($cachedContent !== null) {
        // Output cached content
        echo $cachedContent;
        exit();
    }

    // Forward request headers
    $headers = getallheaders();
    
    // Forward request method
    $method = $_SERVER['REQUEST_METHOD'];

    // Forward request data for POST requests
    $data = null;
    if ($method === 'POST') {
        $data = file_get_contents('php://input');
    }

    // Fetch content from the target URL
    $targetResponse = fetchURL($url, $headers, $method, $data);

    // Extract response headers and content
    list($targetHeaders, $targetContent) = explode("\r\n\r\n", $targetResponse, 2);

    // Forward response headers to the client
    $headerLines = explode("\r\n", $targetHeaders);
    foreach ($headerLines as $header) {
        header($header);
    }

    // Output the content to the client
    echo $targetContent;

    // Cache the response content
    cacheContent($url, $targetContent);
} else {
    // URL parameter is missing
    http_response_code(400);
    echo "Missing URL parameter";
}

?>

Dalam versi yang diperbarui ini, saya telah menambahkan fungsionalitas caching dasar menggunakan caching berbasis file. Respons yang di-cache disimpan dalam file yang diberi nama sesuai hash MD5 URL. Selain itu, skrip memeriksa konten yang di-cache sebelum mengambilnya dari URL target, dan jika konten ditemukan di cache, konten tersebut langsung disajikan ke klien.

Komentar (0)

Belum ada komentar di sini, Anda bisa menjadi yang pertama!

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *


Pilih dan Beli Proxy

Proksi Pusat Data

Memutar Proxy

Proksi UDP

Dipercaya Oleh 10.000+ Pelanggan di Seluruh Dunia

Pelanggan Proksi
Pelanggan Proksi
Pelanggan Proksi flowch.ai
Pelanggan Proksi
Pelanggan Proksi
Pelanggan Proksi