I am trying to send request to rest API using curl as following :
$headers = [
'Content-Type:application/json',
'tranDateTime: $tranDateTime ',
'applicationID: 123456',
'requestSignature: 12337777'
];
I want to send the current date with along with header data. I defined a variable $tranDateTime
that holds the current date and time but it appears as text as following
REQUEST HEADERS
[
x-forwarded-proto: "xxxxxxxxx"
host: "eea0d7e6-e1b7-4031-9486-a126bc29ab5d.mock.pstmn.io"
x-real-ip: "xxxxxxxxx"
x-forwarded-for: "xxxxxxxxx"
x-request-start: "t=1653479444.950"
content-length: "150"
x-forwarded-port: "xxxxxxxxx"
x-amzn-trace-id: "Root=1-628e1814-4076172a36d527b30ae483c5"
accept: "*/*"
content-type: "application/json"
trandatetime: "$tranDateTime" ---------------------------->>
applicationid: "123456"
]
How can I insert the value of the variable?
My Answer:
To insert the value of the variable $tranDateTime in the header data, you need to concatenate the variable within the header array. Here's how you can do it:
php
$tranDateTime = date('Y-m-d H:i:s'); // Get current date and time
$headers = [
'Content-Type: application/json',
'tranDateTime: ' . $tranDateTime,
'applicationID: 123456',
'requestSignature: 12337777'
];
// Now you can use $headers array in your curl request
By concatenating the variable $tranDateTime with the header string, you will insert the current date and time in the header data when making the curl request.
Rate this post
5 of 5 based on 9600 votesComments