Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从URL检索信息_Php_Url - Fatal编程技术网

Php 从URL检索信息

Php 从URL检索信息,php,url,Php,Url,我正在用PHP创建一个API,但在使用URL时遇到了困难,它将此URL提供给我http://localhost/api/index/Peoples/3 我想使用Peoples类,我想得到代码为3的人,有没有办法得到Peoples和数字3 我的想法是这样的,我从浏览器中检索URL,然后我将在上面爆炸。虽然我不需要通过url传递参数,但这非常有效 public function getClass(){ $params = explode("/api/index/", $this-&

我正在用PHP创建一个API,但在使用URL时遇到了困难,它将此URL提供给我
http://localhost/api/index/Peoples/3

我想使用Peoples类,我想得到代码为3的人,有没有办法得到Peoples和数字3

我的想法是这样的,我从浏览器中检索URL,然后我将在上面爆炸。虽然我不需要通过url传递参数,但这非常有效

public function getClass(){

        $params = explode("/api/index/", $this->currentUrl);

        if(count($params) == 1)
            return "no have class !";
        else
        { 
            $params = explode('/', $params[1]);

            $this->callClass($params[0]);
            return "Classe : ".$params[0];
        }

    }
有了这段代码,我可以恢复人,然后知道我将使用哪个类。现在我想传递代码的参数,如示例3所示。我可以按如下方式通过../Peoples?id=3,但我必须断开更多的字符串,是否有更好的方法


传递../Peoples?id=3或../Peoples/3与如何恢复有什么区别?

您可以尝试类似的方法

public function getClass()
{      
  $path = explode('/', parse_url($this->currentUrl, PHP_URL_PATH));

  if(count($path) !== 5){
      return 'not valid url';
  }

  $id    = array_pop($path);
  $class = array_pop($path);

  //$this->callClass($class);

  $newPath = $class.'?id='.$id;

 return $newPath;
}
关于你关于/Peoples?id=3或../Peoples/3的问题 如果您使用的是symfony或laravel之类的框架,在这些框架中可以定义控制器,那么第二个更容易,第一个框架可以使用$_GET['id']轻松地从URL获取数据


希望这就是你想要的。

你已经试过什么了?请提供所有相关代码。简短的回答是肯定的,有很多方法。到目前为止,你自己都做了些什么来解决这个问题?嗨,谢谢你的反馈!我编辑了我的问题,我能帮你做点什么吗?好的,但是我能检索到人/3等于$_GET['id']吗?希望我正确理解你的问题。当您在Symfony等框架中使用people/3时,该框架将为您提供获取这些部件作为传递给路由的参数的功能。如果您不熟悉该框架,那么我建议使用其他格式people?id=3这正是您的想法,请使用代码3的人员。但例如,框架是否也能做到这一点?它是否恢复了url并在其中爆炸?或者有一些更简单的方法来恢复/3而不必爆炸我建议你阅读这篇文章以获得更多的理解