pass parameters into the middle of a url - php

ok, I have a url that calls a webservice, lets say that url is:
http://url.com/products/[productid]?Lanaugae=english$username=username&token=1234
What I'm doing is calling the webservice inside a function, grab the product ID from a get and pass it in and then fire off the function that does all the curl goodness.
The troublesome bit is the productid is in the middle of the url, which I declared globally, and the curl bit fires from another function. I think in .net you could say {0} in the place of [productid] then when you call the variable tell it what should go into [productid], can I do something similar in PHP?
So at the top of my class I want:
$this->url = 'http://url.com/products/{0}?Lanaugae=engish$username=username&token=1234';
this in my function I want to do something like:
$productid = $_GET["productid"];
$responseData = $this->curl_dat_thing($this->url, $productid);
...This possible/make sense?

Try using sprintf() like this:
$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));

I'm not sure what's inside curl_dat_thing() but you should do this:
$this->url = 'http://url.com/products/'.$productid.'?Lanaugae=engish&username=username&token=1234';

Related

Laravel: Get query params of an generated URI

On the API I'm working on, a previous request generates a link that goes like this:
https://api.example.com/example/v1/individuals?$expand=emails%2Cphones&$skip=30
I need to get this "skip" param and send the data back to my backend, to process a request on the whole link plus the skip param, that changes along with the system.
Any tip on how to get this "skip" param?
remove $ sign from query param and try $value = request('skip') to get skip value
// Your api controller
public function someMethod(Request $request)
{
$request->query('skip', 10); // returns 30 for https://api.example.com/example/v1/individuals?expand=emails%2Cphones&skip=30 or 10 if skip is not set.
}
If you received the url from 3rd API and you don't wanna change it, you can do the following:
$urlFromApi = 'https://api.example.com/example/v1/individuals?$expand=emails%2Cphones&$skip=30';
//Remember to use single quote if you wanna paste string with '$' as a character with PHP.
$url = urlencode($urlFromApi);
$parts = parse_url($url);
parse_str($parts["path"], $a);
preg_match('/%24skip\%3D(\d+)/', $parts["path"], $matches);
$skip = $matches[1];
echo $skip;

Alternative for eval() -PHP

i am trying to implement URL mapping in PHP. I have a json file which stores the url and functions which is to execute when that link is requested. I was using eval() but then i came across this
Kepp the following Quote in mind:
If eval() is the answer, you're almost certainly asking the wrong
question. -- Rasmus Lerdorf, BDFL of PHP
now i am thinking is their any other(better) way to do it.
My json file looks like this.
{
"bw/":"main()",
"bw/login":"login()"
}
and my loadPage function look like this.
function loadPage($url){ //$url = 'bw/'
$str = file_get_contents('urls.json');
$this->link = json_decode($str, true);
$url = ltrim($url,"/");
$key = $this->link[$url];
eval("$key;");
}
EDIT:
i defined $this->link in my code
A slight tweak to your JSON to allow you to call the function dynamically would make it easier, just remove the brackets so it would look like...
{
"bw/":"main",
"bw/login":"login"
}
and then call it using...
function loadPage($url){ //$url = 'bw/'
$url = ltrim($url,"/");
$key = $this->link[$url];
$key();
}
A little better way is changing eval() to:
if (function_exists($key)) {
return $key();
}
return default();
and you might create a function "default" to show an error 404 or default page when function doesn't exists.

Add params to query string in a clean way in php

I'd like to find a clean if possibile (without too much string manipulation preg_*)
I know that to replace a parameter I would do
$_GET['info'] = "newinfo";
and to remove a parameter:
unset($_GET['info']);
so is there something like that that I can use?
of course after I've "unset" or "set" I'm building a new query.
(http_build_query).
At the end I'm trying to make this:
/index.php?foo=bar
to
/index.php?foo=bar&info=newinfo
Just do this:
$get = $_GET;
$get['new'] = 'some value';
function getPath()
{
// Stolen from https://stackoverflow.com/a/8775529/3578036
$request = parse_url($_SERVER['REQUEST_URI']);
$path = $request["path"];
return rtrim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');
}
header("Location: " . getPage() . http_build_query($get));
The above code will create a query string and append it to the current URL and redirect to that location. Obviously, you can change the location that you redirect to by replacing the getPage() function result and putting your own result there, this just demonstrates the premise of the answer.
The docs for http_build_query are a very good place to start.
Effectively, what it will do is convert an associative array into an HTTP query string.

$GET query to directory?

i want to fetch youtube videos from the above script but the above code is getting keyword from GET parameter example.com/s=keyword and i want it to get from a example.com/HERE
i mean you can see there is a $_GET['s']
So this function works like this
example.com/s=keyword
and i want it to work like this
example/page/keyword
sorry for my bad english
$keyword = $_GET['s'];
file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&q=$keyword&type=video&key=abcdefg&maxResults=5");
Have a look at $_SERVER[REQUEST_URI]
This will return you the current url. Then process it using simple string or array functions to get the params, like
$current_url = $_SERVER['REQUEST_URI'];
$url_arr = explode("/", $current_url);
Then access the parameters using the array indexes
like $page = $url_arr[0];

multiple vars via url question

I have a method
function checkin($var1){
$newVar1 = $var1;
....
...
}
I am calling it via Restful and I am passing it like this
$url = 'http://mydomain.com/controller/checkin/'.$var1;
Now i want to pass two variables but I am not sure how would it pick the second one
I guess I can do this
$url = 'http://mydomain.com/controller/checkin/'.$var1.'/'.$var2;
not sure what would I do on receiving end to make sure it knows what var to use where.
thanks
On the other end, you have to change your action method signature to
function checkin($var1, $var2){
// (...)
}
Another option is using Cake's named parameters. That would require a change in both the url and the action:
URL
$url = 'http://mydomain.com/controller/checkin/var1:'.$var1.'/var2:'.$var2;
Action method
function checkin(){
$var1 = $this->params['named']['var1'];
$var2 = $this->params['named']['var2'];
}

Categories