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.

3
  • This is a duplicate of : [stackoverflow.com/questions/1939609/… [1]: stackoverflow.com/questions/1939609/… Commented Feb 25, 2015 at 6: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. Commented Feb 25, 2015 at 6: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) Commented Feb 25, 2015 at 6:33

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.