PHP cURL body elements
Get the solution ↓↓↓I am trying to connect our invoice service API to send e-invoices. I have API instructions, but I have no idea how to put all relevant fields to cURL body in the right way. I am using PHP form like this:
$curl = curl_init($url);
// Set the CURLOPT_RETURNTRANSFER option to true
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set the CURLOPT_POST option to true for POST request
curl_setopt($curl, CURLOPT_POST, true);
// Set the CURLOPT_INSECURE to disable certificate
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// Set the request data as JSON using json_encode function
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_INFILESIZE, $fileSize);
// Set custom headers for RapidAPI Auth and Content-Type header
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'x-api-mandator-uuid: mandator-api-id',
'x-api-key: mandator-api-key',
'Content-Type: application/json'
]);
// Execute cURL request with all previous settings
$response = curl_exec($curl);
// Close cURL session
curl_close($curl);
This is how body should be formatted:
curl --location --request POST 'https://api.address.here' \
--header 'Content-Type: application/json' \
--header 'x-api-key: API KEY HERE' \
--header 'x-api-mandator-uuid: MANDATOR ID HERE' \
--data-raw '{
"routingInstructions": {
"primaryDeliveryChannel": 3,
"sentUsingChannel": 0,
"eInvoiceNumber": "$customer-e-invoice address",
"eInvoiceOperator": "$eoperator"
},
"debtors": [
{
"debtorID": "$id",
"debtorType": 2,
"businessName": "$customername",
"businessID": "$businessid",
"businessOffice": "",
"personFirstName": "",
"personLastName": "",
"co": "",
"contactPerson": "",
"personSSN": "",
"postalAddress": {
"countryName": "Suomi",
"countryCode": "FI",
"streets": [
"$customer-street-addr"
],
"city": "$customer-city",
"zip": "$customer-zip"
},
"emails": [
"$customer-email"
],
"phoneNumbers": [
"$customer-phone"
]
}
],
"invoiceFile": {
"data": "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZ....",
"filename": "invoice - einlasku.xml"
},
"attachments": [],
"setAssignmentInStatus": 1
I have invoice file in xml-format and it should be Base64 coded. Can somebody guide me how to put all this info to cURL body in correct way?
Answer
Solution:
In the PHP Curl documentation, there isCURLOPT_POSTFIELDS
which accepts key=>value array so in your case something like
curl_setopt($curl, CURLOPT_POSTFIELDS, [
'routingInstructions' => [
'primaryDeliveryChannel' => 3
...
],
'debtors' => []
...
]);
However I can see you need to send file, which can be sent using CURLFile.
I am finding easier to use library such as Guzzle for advanced HTTP Post.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: gd library extension not available with this php installation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.