简体   繁体   中英

How can I pass an object with multiple values as parameters in an url with PHP

I am using an API to make payments.

They want me to pass the productid and quantity on my order page. I am using file_get_contents to get a result from them that I can then use.

But this is where I get stuck: I need to be able to pass multiple products and I don't know how to pass them in an URL as parameters.

This is the layout of the API:

saleData array Array with product information

  • orderData array Array with ordered products

    -- productId integer id of the product

    -- quantity integer quantity of ordered product with id above

This is how I am passing the information to my php script:

productobject:[{"productId":"10","quantity":"1"},{"productId":"11","quantity":"1"},{"productId":"2","quantity":"1"}]

Then in my PHP script I do the following:

$productobj = $_POST['productobject'];
$productobject = json_decode($productobj);

foreach($productobject as $product){
  $producturl .= 'saleData[orderData]productId='.$product->productId.'&saleData[orderData]quantity='.$product->quantity.'';
}

Then I create the api url like this:

$paynltransaction = file_get_contents("https://rest-api.pay.nl/v7/Transaction/start/json/?token=myapitoken&serviceId=SL-9697-8091&amount=".$bedrag."&ipAddress=".get_ip($ip)."&finishUrl=http://www.website.nl/_extern/web/status&paymentOptionId=".$betaalid."".$idealbank."&transaction[description]=BadenInclusief&enduser[initials]=".$voornaam."&enduser[lastName]=".$achternaamnospace."&enduser[emailAddress]=".$mail."&".$producturl."");

It doesn't give me an error but when I check the result later on this is what is stored in the response that contains the product info:

[saleData] => stdClass Object
        (
            [orderData] => Array
                (
                    [0] => 1
                )

            [invoiceDate] => 
            [deliveryDate] => 
        )

It only adds the last quantity and loses the key, it just says [0] instead of [quantity].

What am I doing wrong?

This is what I get with the answer from Fransesco:

 [saleData] => stdClass Object
        (
            [orderData] => Array
                (
                    [0] => stdClass Object
                        (
                            [productId] => 000000
                            [productType] => ROUNDING
                            [description] => Afronding
                            [price] => 330000
                            [quantity] => 1
                            [vatCode] => N
                            [vatPercentage] => 0
                            [discount] => 0
                        )

                )

            [invoiceDate] => 
            [deliveryDate] => 
        )

Updated code:

foreach($productobject as $product){
  $producturl .= 'saleData[orderData][productId]='.$product->productId.'&saleData[orderData][quantity]='.$product->quantity.'';
}

Every key of every [sub]array needs to be between square brackets:

saleData[orderData][quantity]

As a side note, building a monster of an URL isn't the best way, you have limits on the amount of data you can pass via GET request... see this explanation: What is the maximum length of a URL in different browsers?

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