简体   繁体   中英

PHP - Get indexed URL query string parameters from URL

I wonder if someone can help me please. I am sending a webhook from Segment to my back-end. This is the final result of the destination URL including the parameters:

https://example.com/page.php?cid=123456&eventid=965254&trackerid=2523654&amount1=233.55&amount2=156.99&catgory1=clothing&category2=accessories&itemsku1=01235654&itemsku2=525124&quantity1=1&quantity2=3

The standalone parmameters like cid is easy to get, but the indexed query string parameters like category1 and category2 is where I am stuck. I tried this approach How to get multiple parameters with same name from a URL in PHP but it is not really the right solution for my case.

I also have the option of sending the webhook in JSON format, if that would make more sense, but still, not sure how to get the indexed parameters like itemsku1 and itemsku2.

I also forgot to mention that the idexed parameters can change depending on the amounts of products purchased, so I do not know beforehand how many there will be in the URL.

Thanks in advance!

Try something like this

<?php

$param_names = [
    'amount',
    'category',
    'itemsku',
    'quantity',
];

$data = [];

foreach ($_GET as $key => $val) {

    foreach ($param_names as $param_name) {

        if (strpos($key, $param_name) === 0) {
            $idx = substr($key, strlen($param_name), 1);

            $data[$idx][$param_name] = $val;
        }
    }

}

var_dump($data);

This is the result

array (size=2)
  1 => 
    array (size=3)
      'amount' => string '233.55' (length=6)
      'category' => string 'clothing' (length=8)
      'itemsku' => string '01235654' (length=8)
      'quantity' => string '1' (length=1)
  2 => 
    array (size=4)
      'amount' => string '156.99' (length=6)
      'category' => string 'accessories' (length=11)
      'itemsku' => string '525124' (length=6)
      'quantity' => string '3' (length=1)

I'm not sure I understand you correctly, but couldn't you use:

$category1 = filter_input(INPUT_GET,"category1",FILTER_SANITIZE_STRING);
$category2 = filter_input(INPUT_GET,"category2",FILTER_SANITIZE_STRING);
$sku1 = filter_input(INPUT_GET,"itemsku1",FILTER_SANITIZE_STRING);
$sku2 = filter_input(INPUT_GET,"itemsku2",FILTER_SANITIZE_STRING);

And so on... Unless you do not know the exact number of indexed parameters there are, you could use the following, which stores the parameters in an array.

$query  = explode('&', $_SERVER['QUERY_STRING']);
$params = array();
foreach( $query as $param )
{
  list($name, $value) = explode('=', $param, 2);
  $params[urldecode($name)][] = urldecode($value);
}

Say you were to then visit the link https://example.com/page.php?itemsku=test1&itemsku=test2

$params['itemsku'][0] would return: test1 $params['itemsku'][1] would return: test2

And to loop through the itemsku parameter array, just do:

foreach($params['itemsku'] as $sku){
   echo $sku . "\n";
}

Would output:

test1
test2

Im not sure if I understand you correctly, but you can use parse_url . In your case:

$url = 'https://example.com/page.php?cid=123456&eventid=965254&trackerid=2523654&amount1=233.55&amount2=156.99&catgory1=clothing&category2=accessories&itemsku1=01235654&itemsku2=525124&quantity1=1&quantity2=3';

$temp_array = [];
parse_str(parse_url($url, PHP_URL_QUERY), $temp_array);
var_dump($temp_array);

This will produce an array with all the query parameters. You can then access each parameter such as:

echo $temp_array['cid'];
echo $temp_array['catgory1'];
echo $temp_array['catgory2'];
echo $temp_array['itemsku1'];
echo $temp_array['itemsku2'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM