6

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.

Sudhir Vadodariya
  • 391
  • 1
  • 2
  • 11
  • This is a duplicate of : [http://stackoverflow.com/questions/1939609/convert-command-line-curl-to-php-curl?rq=1][1] [1]: http://stackoverflow.com/questions/1939609/convert-command-line-curl-to-php-curl?rq=1 – Damian Nikodem Feb 25 '15 at 06:10
  • No duplicate of that "Damian Nikodem". I need to know that how can i convert the data request payload (" -d ") in PHP. I need to send data in PHP CURL call like same as " -d " argument in above command line CURL. – Sudhir Vadodariya Feb 25 '15 at 06:27
  • ummm.... yes it is, if you read the question and its answers you will see that CURLOPT_POSTFIELDS is set (which is the equivilent of -d) – Damian Nikodem Feb 25 '15 at 06:33

1 Answers1

1

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;
hindmost
  • 7,125
  • 3
  • 27
  • 39