नीचे 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";
}
?>
यह PHP स्क्रिप्ट एक बुनियादी वेब प्रॉक्सी/अनामाइज़र के रूप में कार्य करती है। यह अनुरोधों को सुनता है और उन्हें निर्दिष्ट URL पर अग्रेषित करता है। अनुरोधित URL को क्वेरी पैरामीटर के रूप में प्रदान किया जाना चाहिए जिसका नाम है url
.
इस प्रॉक्सी का उपयोग करने के लिए, स्क्रिप्ट को अपने सर्वर पर सेव करें और ब्राउज़र के माध्यम से इसे एक्सेस करें या प्रोग्रामेटिक रूप से HTTP अनुरोध भेजें। उदाहरण के लिए:
GET http://example.com/proxy.php?url=https://example.com
इससे सामग्री पुनः प्राप्त होगी https://example.com
प्रॉक्सी के ज़रिए। ध्यान दें कि यह एक सरल कार्यान्वयन है और सभी प्रकार के अनुरोधों या प्रतिक्रियाओं को संभाल नहीं सकता है। इसके अतिरिक्त, इसमें कैशिंग या अनामीकरण जैसी सुविधाएँ शामिल नहीं हैं।
आप 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;
}
// 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";
}
?>
इस अपडेटेड वर्शन में, मैंने फ़ाइल-आधारित कैशिंग का उपयोग करके बुनियादी कैशिंग कार्यक्षमता जोड़ी है। कैश की गई प्रतिक्रियाएँ URL के MD5 हैश के नाम पर फ़ाइलों में संग्रहीत की जाती हैं। इसके अतिरिक्त, स्क्रिप्ट लक्ष्य URL से इसे प्राप्त करने से पहले कैश की गई सामग्री की जाँच करती है, और यदि सामग्री कैश में पाई जाती है, तो इसे सीधे क्लाइंट को दिया जाता है।
टिप्पणियाँ (0)
यहां अभी तक कोई टिप्पणी नहीं है, आप पहले हो सकते हैं!