【问题标题】:pass parameters into the middle of a url将参数传递到 url 的中间
【发布时间】:2014-11-26 14:10:25
【问题描述】:

好的,我有一个调用 web 服务的 url,假设 url 是:

http://url.com/products/[productid]?Lanaugae=english$username=username&token=1234

我正在做的是在一个函数中调用 web 服务,从 get 中获取产品 ID 并将其传入,然后启动执行所有 curl 优点的函数。

麻烦的一点是 productid 在我全局声明的 url 的中间,而 curl 位从另一个函数触发。我认为在 .net 中你可以说 {0} 代替 [productid] 然后当你调用变量时告诉它应该进入 [productid] 的内容,我可以在 PHP 中做类似的事情吗?

所以我想要在班级中名列前茅:

$this->url = 'http://url.com/products/{0}?Lanaugae=engish$username=username&token=1234';

在我的函数中我想做类似的事情:

$productid = $_GET["productid"];
$responseData = $this->curl_dat_thing($this->url, $productid);

...这可能/有意义吗?

【问题讨论】:

  • 让我们说这没有意义。你不能简单地说:$newUrl = $this->url.$myvars?这样您就可以连接您的值。
  • 也就是说,获取值不是位置的,您可以将其添加到字符串的末尾,例如 =)
  • 即使你让那个部分正常工作,$username=username 也会把你搞砸。像使用 &token 一样使用 &username=username

标签: php json class parameters get


【解决方案1】:

尝试像这样使用sprintf()

$this->url = 'http://url.com/products/%s?Lanaugae=engish$username=username&token=1234';

$productid = $_GET["productid"];
$responseData = $this->curl_dat_thing(sprintf($this->url, $productid));

【讨论】:

    【解决方案2】:

    我不确定 curl_dat_thing() 里面有什么,但你应该这样做:

    $this->url = 'http://url.com/products/'.$productid.'?Lanaugae=engish&username=username&token=1234';
    

    【讨论】:

      猜你喜欢
      • 2014-01-05
      • 1970-01-01
      • 2023-03-21
      • 2016-03-05
      • 1970-01-01
      • 2018-06-07
      • 2013-10-09
      • 2011-02-19
      • 1970-01-01
      相关资源
      最近更新 更多