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);
Related
I have a problem here, I want to change the query string that I received to the json form as follows
{"id":"89"}
here I try to use json_encode but the one it produces is
""?id=89""
here is my code
$coba = str_replace($request->url(), '',$request->fullUrl());
if (empty($coba)) {
$url = "{}";
} else {
$url = $coba;
}
$json_body = json_encode($url);
there I also want to change if there is no query string then the result is {}
This should do it for you for both conditions:
json_encode($request->query(), JSON_FORCE_OBJECT);
PHP Manual - JSON Functions - json_encode
Laravel 5.8 Docs - Requests - Retrieving Input - Retrieving Input From The Query String query
//get Parameters
$array = [
'id' => 20,
'name' => 'john',
];
//getting the current url from Request
$baseUrl = $request->url();
//we are encoding the array because
//u said that the get parms in json format
//so that ...
$json = json_encode($array);
//now building the query based on the data
//from json by decoding it
$query = (http_build_query(json_decode($json)));
//at the end of the url you need '?' mark so...
$url = \Illuminate\Support\Str::finish($baseUrl,'?');
//now Merging it
$fullUrl = $url.$query;
dump($fullUrl);
For any issues kindly comment
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']);
?>
I am trying to pass the value to a function to get the following url:
search?channelIds=1,2,3&streamName=wew&page=0&size=10
code inside function:
$this->request->set("url","broadcasts/pubic/search");
$this->request->set_fields(array(
'channelIds'=>$channel_ids,
'streamName'=>$stream_name,
'page'=>$page,
'size'=>$size
));
$result = $this->request->send();
Here i pass the values in this format.How will give pass value for channelIds=1,2,3? i use http_build_query for url formation. $channel_ids=array("channelIds"=>1, "channelIds"=>2, "channelIds"=>3); $stream_name="wew"; $page=0; $size=10; $getlivebroadcast = $api-searchBroadcast($channel_ids,$stream_name,$page,$size);
How will i set the value?
You can use the urlencode function to encode the channel ids. Your function may look like:
$this->request->set("url","broadcasts/pubic/search");
$this->request->set_fields(array(
'channelIds'=>urlencode($channel_ids),
'streamName'=>$stream_name,
'page'=>$page,
'size'=>$size
));
$result = $this->request->send();
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]));
}
}
?>
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'];