I have one URL string like
'http://example.com/glamour/url.php?ad_id=[ad_id]&pubid=[pubid]&click_id=[click_id]'
So I want to fetch value of all parameters which mentioned in [].
Like ad_id,pubid,clickid
How can I fetch values of parameters?
<?php
echo $_GET['ad_id']; // ad_id is the name of parameter.
?>
This code will give you the value of parameter
For String of URL try this :
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['ad_id'];
You could use this command.
print_r($_GET);
And then you could see the all parameters in the url.
Use this simple code to get all parts of query:
<?php
/* Parse url */
$url = 'http://example.com/url.php?ad_id=[ad_id]&pubid=[pubid]';
parse_str( parse_url( $url, PHP_URL_QUERY ), $query_data );
/* Results */
var_dump($query_data['ad_id']);
var_dump($query_data['pubid']);
?>
Related
I need to know how to get the $_GET parameter from requested url. If I use for example
$url = $_SERVER['REQUEST_URI'];
$parse_url = parse_url($url, PHP_URL_QUERY);
echo $parse_url; will output query=1, but I need only the parameter not parameter AND value to check if parameter is in array.
Based on your example, simply exploding the = might quickly suits your need.
$url = $_SERVER['REQUEST_URI'];
$parse_url = parse_url($url, PHP_URL_QUERY);
$queryparam = explode("=", $parse_url);
echo $queryparam[0];
/* OUTPUT query */
if (in_array($queryparam[0], $array_of_params)){ ... }
But you can simply achieve the same thing like this:
if (#$_GET["query"] != ""){ ... }
Something like:
// Example URL: http://somewhere.org?id=42&name=bob
// An array to store the parameters
$params = array();
parse_str($_SERVER['QUERY_STRING'], $params);
// Access what you need like this:
echo 'ID: ' .$params['id'];
The Arry $_GET contains all needed informations. $ _SERVER ['REQUEST_URI'] and parse_url not needed.
As an example I have the following browser line:
http://localhost/php/test.php?par&query=1
Let's see what $ _GET contains
echo '<pre>';
var_export($_GET);
Output
array (
'par' => '',
'query' => '1',
)
With array_keys() I get all keys.
$keys = array_keys($_GET);
var_export($keys);
Output:
array (
0 => 'par',
1 => 'query',
)
The first key I get with (see also comment from #bobble bubble)
echo $keys[0]; //par
or
echo key($_GET);
With array_key_exists () I can check whether a key is available.
if(array_key_exists('query',$_GET)){
echo 'query exists';
}
You can use basename() too, it Returns trailing name component of path. For example:
$lastPart=explode("=",basename('http://yourdomain.com/path/query=1'));
echo $lastPart[0];
Output
query
I'm trying to generate a URL similar to
https://website.com/oltp-‐web/processTransaction?REQUEST_TYPE=2&MID=5
I'm using PHP's http_build_query function but it's not generating a proper URL.
Code sample:
<?php
$parameters =array(
'https://pguat.paytm.com/oltp-‐web/processTransaction?',
'REQUEST_TYPE'=>'2',
'MID'=>'5');
$url = http_build_query($parameters);
echo $url;
?>
Your first array value is not a parameter. It's the URL you want to add the query string to. http_build_query() builds query strings, not entire URLs. So remove that value and then append the results of http_build_query() to it:
$parameters =array(
'REQUEST_TYPE'=>'2',
'MID'=>'5'
);
$url = 'https://pguat.paytm.com/oltp-‐web/processTransaction?' . http_build_query($parameters);
Encoded URL
www.example.com/apps/?bmFtZTE9QUhTRU4mbmFtZTI9TUFFREEmcGVyY2VudD02NQ
Decoded URL
www.example.com/apps/?name1=AHSEN&name2=MAEDA&percent=65
now i want to get params from encoded URL
ob_start();
$name1 = $_GET['name1'];
You probably have the decoded url somewhere in a variable. If you extract the query part from it (the part after the ?), you can feed that query string to the function parse_str.
If you use parse_str with just the query string as argument, it sets the appropriate variables automatically. For example
// whereever you get the query part of your decoded url from
$my_query_string = "name1=AHSEN&name2=MAEDA&percent=65";
// feed it into parse_str
parse_str($my_query_string);
would set the global variables
$name1
$name2
$percent
But I'd advise to make use of the second parameter that parse_str offers, because it provides you with better control. For that, provide parse_str with an array as second parameter. Then the function will set the variables from your query string as entries in that array, analogous to what you know from $_GET.
// whereever you get the query part of your decoded url from
$my_query_string = "name1=AHSEN&name2=MAEDA&percent=65";
// provide parse_str with the query string *and* an array you want the results in
parse_str($my_query_string, $query_vars);
echo $query_vars['name1']; // should print out "AHSEN"
UPDATE: To split your complete decoded url, you can use parse_url.
You can use the function urldecode.
This should get you started...
<?php
$query = "my=apples&are=green+and+red";
foreach (explode('&', $query) as $chunk) {
$param = explode("=", $chunk);
if ($param) {
printf("Value for parameter \"%s\" is \"%s\"<br/>\n",
urldecode($param[0]), urldecode($param[1]));
}
}
?>
I want to fetch the value of until from following url,
https://graph.facebook.com/v2.1/168820059842589/feed?fields=id,from,message,comments%7Bfrom,id,created_time,message,attachment,comments%7Bfrom,id,created_time,message,attachment%7D%7D,type,created_time,updated_time,picture,name,caption,link,source,description,likes,story,place,story_tags&limit=250&include_hidden=true&until=1394190620&__paging_token=enc_AeyIp_kAzWE_u3avDbSjlT69EtTXCEOSDLyp0xSOTptJHfmlHKARfuTm197SkZAPDhKsXAtfu9G7jqzn75ymXK60UwG3nR2itFLk-IhY_hdOzQ
Thanks
You can use parse_str():
$url = 'https://graph.facebook.com/v2.1/168820059842589/feed?fields=id,from,message,comments%7Bfrom,id,created_time,message,attachment,comments%7Bfrom,id,created_time,message,attachment%7D%7D,type,created_time,updated_time,picture,name,caption,link,source,description,likes,story,place,story_tags&limit=250&include_hidden=true&until=1394190620&__paging_token=enc_AeyIp_kAzWE_u3avDbSjlT69EtTXCEOSDLyp0xSOTptJHfmlHKARfuTm197SkZAPDhKsXAtfu9G7jqzn75ymXK60UwG3nR2itFLk-IhY_hdOzQ';
$arr = array();
parse_str($url,$arr);
echo $arr['until'];
//outputs 1394190620
If I have a url that looks like this, what's the best way to read the value
http://www.domain.com/compute?value=2838
I tried parse_url() but it gives me value=2838 not 2838
Edit: please note I'm talking about a string, not an actual url. I have the url stored in a string.
You can use parse_url and then parse_str on the query.
<?php
$url = "http://www.domain.com/compute?value=2838";
$query = parse_url($url, PHP_URL_QUERY);
$vars = array();
parse_str($query, $vars);
print_r($vars);
?>
Prints:
Array
(
[value] => 2838
)
For http://www.domain.com/compute?value=2838 you would use $_GET['value'] to return 2838
$uri = parse_url($uri);
parse_str($uri['query'], $params = array());
Be careful if you use parse_str() without a second parameter. This may overwrite variables in your script!
parse_str($url) will give you $value = 2838.
See http://php.net/manual/en/function.parse-str.php
You should use GET method e.g
echo "value = ".$_GET['value'];