web-proxy-script icon indicating copy to clipboard operation
web-proxy-script copied to clipboard

How to load images

Open oleteacher opened this issue 4 years ago • 2 comments

Really cool script, can tell a lot of thought has gone into it.

Is there a way to allow images to load on site that is proxied?

oleteacher avatar Apr 08 '21 12:04 oleteacher

相对地址和绝对地址的问题?

iam7cn avatar Jan 14 '22 08:01 iam7cn

相对地址和绝对地址的问题?

It seems more like a lack-of-English problem.

hopeseekr avatar Apr 21 '24 12:04 hopeseekr

I am sorry, I do not speak / read / write Chinese. I barely speak my native English:)

"The problem with relative and absolute addresses?"

I solved my issues with a few mods.

First, when using PHP 8.2, had to modify code to remove error:

Deprecated: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities, or mb_encode_numericentity/mb_decode_numericentity instead in /home/wwwroot/qikserver.com/testbed/php-projects/php-proxy-1/demo.php on line 227

My mod:

// Attempt to normalize character encoding to UTF-8
if (mb_detect_encoding($responseBody, 'UTF-8', true) === false) {
    $responseBody = mb_convert_encoding($responseBody, 'HTML-ENTITIES', mb_detect_encoding($responseBody));
} else {
    $responseBody = mb_convert_encoding($responseBody, 'UTF-8');
}

Second, to fix image issue I was experiencing, modified the function makeRequest:

function makeRequest($url) {
    // Tell cURL to make the request using the browser's user-agent if there is one, or a fallback user-agent otherwise.
    $user_agent = $_SERVER["HTTP_USER_AGENT"];
    if (empty($user_agent)) {
        $user_agent = "Mozilla/5.0 (compatible; nrird.xyz/proxy)";
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

    // Proxy the browser's request headers.
    $browserRequestHeaders = getallheaders();
    unset($browserRequestHeaders["Host"]);
    unset($browserRequestHeaders["Content-Length"]);
    unset($browserRequestHeaders["Accept-Encoding"]); // this ensures cURL can negotiate encoding on its own
    curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function($name, $value) {
        return "$name: $value";
    }, array_keys($browserRequestHeaders), $browserRequestHeaders));

    // Proxy any received GET/POST/PUT data (same logic as you've already written)

    // Other cURL options.
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    
    // Set the request URL.
    curl_setopt($ch, CURLOPT_URL, $url);

    // Make the request.
    $response = curl_exec($ch);
    $responseInfo = curl_getinfo($ch);
    $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    curl_close($ch);

    // Separate the headers and the body
    $responseHeaders = substr($response, 0, $headerSize);
    $responseBody = substr($response, $headerSize);

    // Check if the Content-Encoding header is present and remove it if so
    if (stripos($responseHeaders, 'Content-Encoding') !== false) {
        header_remove('Content-Encoding'); // Remove any Content-Encoding header to prevent issues
    }

    return array("headers" => $responseHeaders, "body" => $responseBody, "responseInfo" => $responseInfo);
}

You're welcome to incorporate my changes if so desired.

oleteacher avatar Aug 07 '24 15:08 oleteacher