I have searched many hours trying to figure out what --data-urlencode is in php curl.
I tried this, but I dont think its right.
$xmlpost .= "object1[file]=@https://www.lob.com/goblue.pdf";
In the documentation it is:
--data-urlencode "object1[file]=https://xxx.pdf" \
But what is it in CURL?
curl_setopt($cpt, CURLOPT_??)
Full API DOCS says i need to call this
curl https://api.lob.com/v1/jobs \
-u test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc: \
-d "name=Demo Quick Print Job" \
-d "to[name]=Harry Zhang" \
-d "to[address_line1]=123 Test Street" \
-d "to[address_city]=Mountain View" \
-d "to[address_state]=CA" \
-d "to[address_zip]=94085" \
-d "to[address_country]=US" \
-d "from[name]=Leore Avidar" \
-d "from[address_line1]=123 Test Street." \
-d "from[address_line2]=Apt 155" \
-d "from[address_city]=Sunnyvale" \
-d "from[address_state]=CA" \
-d "from[address_zip]=94085" \
-d "from[address_country]=US" \
-d "object1[name]=testobject" \
--data-urlencode "object1[file]=https://www.lob.com/goblue.pdf" \
-d "object1[setting_id]=100"
I am therefore trying:
<?php
$xmlpost = array(
"name" => "PostalAddressConfirmation",
"to" => array(
"name" => "Testing",
"address_line1" => "testvej",
"address_city" => "test",
"address_state" => "test",
"address_zip" => "5000",
"address_country" => "DK"
),
"from" => array(
"name" => "test ApS",
"address_line1" => "testvej 25",
"address_city" => "test",
"address_state" => "test",
"address_zip" => "5000",
"address_country" => "DK"
),
"object1" => array(
"name" => "test Object",
"setting_id" => "100",
"file" => "https://lob.com/goblue.pdf"
)
);
$cpt = curl_init("https://api.lob.com/v1/job");
curl_setopt_array($cpt, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => $xmlpost,
CURLOPT_USERPWD => 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc:',
));
$result = curl_exec($cpt);
Why do we need to encode? URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL.
-d, --data <data> (HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.
The %2C means , comma in URL. when you add the String "abc,defg" in the url as parameter then that comma in the string which is abc , defg is changed to abc%2Cdefg . There is no need to worry about it.
urlencode() function. This is a convenience function which takes a dictionary of key value pairs or a sequence of two-element tuples and uses the quote_plus() function to encode every value. The resulting string is a series of key=value pairs separated by & character.
From the man page you get
--data-urlencode
<data>
(HTTP) This posts data, similar to the other --data options with the exception that this performs URL-encoding.
For information the --data
option is described as follow :
-d, --data
<data>
This will cause curl to pass the data to the server using the content-type
application/x-www-form-urlencoded
.
The answer is that you don't even need to think about it; consider the following code:
$xmlpost = [
"name" => "PostalAddressConfirmation",
// Lav modtager
"to" => [
"name" => "Jespern",
"address_line1" => "hejvej",
"address_city" => "Odense",
"address_state" => "Syddanmark",
// etc ...
],
// Lav afsender
"from" => [
"name" => "test",
// etc.
],
//Dokument der skal sendes
"object1" => [
"name" => "AddressConfirmation",
"setting_id" => 100,
"file" => "https://www.lob.com/goblue.pdf",
"quantity" => 1,
],
];
$cpt = curl_init("https://api.lob.com/v1/job");
curl_setopt_array($cpt, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERPWD => 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc:',
CURLOPT_POSTFIELDS => http_build_query($xmlpost),
]);
$result = curl_exec($cpt);
The http_build_query()
function will build the appropriate string to post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With