Here is command line CURL code:-
curl -X POST "http://{$HOST}/api/1/videos.json" \
-H "Content-type: application/json" \
-H "X-Reseller-Email: $RESELLER" \
-H "X-Reseller-Token: $TOKEN" \
-H "X-User-Email: $USER" \
-d '{"video":{
"title": "My video from API",
"description": "Description from API",
"video_template_id": "16",
"track_id": "26",
"texts": [
{
"layer": "VLN_txt_01",
"value": "My text 1"
}
],
"images": [
{
"layer": "VLN_icon_01",
"source": "icon",
"icon_id": "1593"
}
]
}}'
Please help me to convert this into PHP CURL call. Also, i need this call with POST and PUT methods. I have founded that but does not able to convert the data payload in PHP.
I just need to know that how can i write the " -d " (data) in PHP which affect same as command line CURL call in PHP.
PHP Curl Example php $url = "https://reqbin.com/echo"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $resp = curl_exec($curl); curl_close($curl); var_dump($resp); ?>
The curl -d command. To send data in the body of a request message using Curl, you need to pass the data to Curl using the -d or --data command line switch. The -d option is usually followed by the -H option, which specifies the type of data in the body of the request message.
curl is a command-line tool to transfer data to or from a server, using any of the supported protocols (HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP, or FILE). curl is powered by Libcurl.
Sending a POST Request with CurlWe first need to specify the HTTP method using the -X parameter. In this case, it's the POST method. Next, we need to specify the Content-type using the -H parameter. That enables the server to identify the data type of the request body and process it accordingly.
You have to use CURLOPT_POSTFIELDS
option in conjuction with CURLOPT_HTTPHEADER
. CURLOPT_POSTFIELDS
option must be set to JSON string, and CURLOPT_HTTPHEADER
- array containing all needed HTTP headers (including Content-type
).
So the code should look like this:
$json = '{"video":{
"title": "My video from API",
"description": "Description from API",
"video_template_id": "16",
"track_id": "26",
"texts": [
{
"layer": "VLN_txt_01",
"value": "My text 1"
}
],
"images": [
{
"layer": "VLN_icon_01",
"source": "icon",
"icon_id": "1593"
}
]
}}';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "http://{$HOST}/api/1/videos.json",
CURLOPT_NOBODY => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
"Content-type: application/json",
"X-Reseller-Email: $RESELLER",
"X-Reseller-Token: $TOKEN",
"X-User-Email: $USER",
),
));
$response = curl_exec($ch);
if ($response && curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200)
echo $response;
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