Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

php - How to have a good url?

I trying to develop a new bank module. The bank has an API and when I send the following URL as a parameter in a request made to the bank's API:

https://www.example.net:443/demo/index.php?order&bank&success&ho

it then returns a response URL containing the outcome of the request as URL parameters, however the bank's API assumes that the URL provided does not already have a query string and simply appends a new complete query string including the ? to the input URL (for example) ?TPE=monet14&date=12%2f07%2f2018%5fa%5f11%3a32%3a28&monta, making the final URL:

https://www.example.net:443/demo/index.php?order&bank&success&ho?TPE=monet14&date=12%2f07%2f2018%5fa%5f11%3a32%3a28&monta

However due to the extra ? character, when I redirect a user to this URL, my server interprets $_GET['ho?TPE'] as a single variable "monet14" instead of $_GET['ho'] = "" and $_GET['TPE'] = "monet14". When I redirect the remote user to this URL, they are then redirected to index.php instead of the desired script.

Here is the output of var_dump($_GET) after redirecting the user:

array(23) { 
    ["order"]=> string(0) "" 
    ["desjardins"]=> string(0) "" 
    ["success"]=> string(0) "" 
    ["ho?TPE"]=> string(7) "monet14" 
    ["date"]=> string(21) "12/07/2018_a_11:32:28" 
    ["montant"]=> string(5) "53EUR"
}

Instead I would like the user to be redirected to:

 https://www.example.net:443/demo/index.php?order&bank&success&ho&TPE=monet14

How can I change the extra ? to an & in the URL returned by my bank's API before redirecting the user?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I got you right, you're probably looking for http_build_query, parse_str and parse_url. Here's an example of how to parse, manipulate and combine two queries to one query:

// These are the two URLs to parse
$url1 = 'https://www.example.net:443/demo/index.php?order&bank&success&ho?TPE=monet14&date=12%2f07%2f2018%5fa%5f11%3a32%3a28&monta...'
$url2 = '?TPE=monet14&date=12%2f07%2f2018%5fa%5f11%3a32%3a28&monta';

// Get the part after the "?"
$queryString1 = parse_url($url1)['query'] ?? ''; // 'query' may be undefined
$queryString2 = parse_url($url2)['query'] ?? ''; // 'query' may be undefined

// Split the parts into usable arrays and save the result into defined variables
$queryArray1 = $queryArray2 = [];
parse_str($queryString1, $queryArray1);
parse_str($queryString2, $queryArray2);

// Now you can manipulate the arrays as you wish;
// after you have done that, you can combine them together:
$resultQueryString = http_build_query(array_merge($queryArray1, $queryArray2));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...