user782104
user782104

Reputation: 13555

How to construct the CURL in JSON-RPC structure in PHP?

Recently I work with an API that require send the data in JSON-RPC format.

And I construct like that

function create_video($files) {
    $api = "http://api.brightcove.com/services/post";

    $local_file_list = $files['file']['tmp_name'];

    foreach ($local_file_list as $local_file) {
        try {
            $ch = curl_init();

            if (FALSE === $ch) {
                throw new Exception('failed to initialize');
            }

            $params = array(
                "encode_to" => "MP4",
                "create_multiple_renditions" => "True",
                "token" => WRITE_TOKEN,
                "file" => @$local_file
            );

            $request = json_encode(array('jsonrpc' => '2.0', 'method' => 'create_video', 'params' => $params));

            curl_setopt($ch, CURLOPT_URL, $api);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            $content = curl_exec($ch);

            if (FALSE === $content) {
                throw new Exception(curl_error($ch), curl_errno($ch));
            }

            die(var_dump(json_decode($content)));

            return json_decode($content);
        } catch (Exception $e) {
            trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
        }
    }
}

The problem is , it create the incorrect json structure comparing to the correct one.

Request Payload of my version

------WebKitFormBoundary8VABz8KuNRE8Hepd
Content-Disposition: form-data; name="file[0]"; filename="big_buck_bunny.mp4"
Content-Type: video/mp4


------WebKitFormBoundary8VABz8KuNRE8Hepd--

Request Payload of correct version

 ------WebKitFormBoundaryCAB6WEANBJxoB3Op
    Content-Disposition: form-data; name="JSONRPC"

    {"params":{"video":{"name":"test","shortDescription":"test","startDate":1432282741000,"endDate":null},"encode_to":"MP4","create_multiple_renditions":"True","token":"VyocgALDnxU8HPvmnSnckgmXjoPlYWomc2La5Tn-evuAfsnSPJJoow.."},"method":"create_video"}
    ------WebKitFormBoundaryCAB6WEANBJxoB3Op
    Content-Disposition: form-data; name="filePath"; filename="big_buck_bunny.mp4"
    Content-Type: video/mp4


    ------WebKitFormBoundaryCAB6WEANBJxoB3Op

        Content-Disposition: form-data; name="JSONView"

        {"params":{"video":{"name":"test","shortDescription":"test","startDate":1432282741000,"endDate":null},"encode_to":"MP4","create_multiple_renditions":"True","token":"VyocgALDnxU8HPvmnSnckgmXjoPlYWomc2La5Tn-evuAfsnSPJJoow.."},"method":"create_video"}
        ------WebKitFormBoundaryCAB6WEANBJxoB3Op--

And the correct website that send data, you may enter the form to test

http://docs.brightcove.com/en/video-cloud/media/samples/create_video.html#request

And the API reference(create video)

https://docs.brightcove.com/en/video-cloud/media/references/reference.html#Video_Write

Thanks a lot for helping.

Upvotes: 2

Views: 989

Answers (1)

VolenD
VolenD

Reputation: 3692

The documentation of their API reference (the second link) states:

For write requests, he data submitted to the media write API should be in JSON format, encoded as a form parameter named "json". The JSON document should include a "method" field and a "params" field, and the examples you publish should show the whole body of the POST request: json=

So, just replace:

curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

with:

curl_setopt($ch, CURLOPT_POSTFIELDS, 'json=' . $request);

and it should be working then. I got error "invalid token" instead of "Could not find JSON-RPC." by replacing the code above.

Upvotes: 1

Related Questions