【问题标题】:How do I save everything after / into a variable using PHP?如何使用 PHP 将 / 之后的所有内容保存到变量中?
【发布时间】:2020-04-16 07:02:53
【问题描述】:

目前我有一个用 PHP 制作的代理,它的工作原理如下:https://proxyurl/url= 但我想摆脱url=,只需将 url 放在斜杠之后。然后我想将斜杠后输入的所有内容保存到 PHP 内的 $url 变量中。我该怎么做呢?

我当前的代码:

<?php
    header("Access-Control-Allow-Origin: *");
    header('Content-Type: application/json');

    $url = $_GET['url'];

    $ch = curl_init();

    //Set the URL that you want to GET by using the CURLOPT_URL option.
    curl_setopt($ch, CURLOPT_URL, $url);

    //Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //Execute the request.
    $response = curl_exec($ch);

    //Close the cURL handle.
    curl_close($ch);

    echo $response;

?>

例如:https://proxyurl/https://google.com - 其中https://google.com$url

https://proxyurl/google.com,其中google.com$url

【问题讨论】:

  • 您可以使用 .htaccess mod_rewrite 执行此操作,您可以在其中定义 proxyurl.com/www.google.com 等于 proxyurl.com/?url=www.google.com

标签: php variables url proxy


【解决方案1】:

我猜你正在使用类似这样的东西:https://proxyurl/?url=google.com。您不必对您的 php 代码进行任何更改。

正如 elMeroMero 所说,您需要在任何 Apache 配置文件中使用 mode_rewrite。您可以将它与根目录中的 .htaccess 文件一起使用。你必须有这样的东西:

RewriteEngine On
RewriteRule (.*) ?url=$1 [L]

我不是 mode_rewrite 专家,也许其他人会修改我的建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多