Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to convert command line (Linux) CURL to PHP CURL with request data payload (" -d ")

Tags:

linux

php

curl

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.

like image 552
Sudhir Vadodariya Avatar asked Feb 25 '15 05:02

Sudhir Vadodariya


People also ask

How to convert PHP code to curl?

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); ?>

What is curl D option?

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.

What is curl command Linux?

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.

How do you hit post request on curl?

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.


1 Answers

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;
like image 144
hindmost Avatar answered Oct 06 '22 00:10

hindmost