如何在JSON-RPC结构中构造PHP中的CURL?

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

最近我使用一个 API 需要以 JSON-RPC 格式发送数据。

我就是这样构造的

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

问题是,与正确的结构相比,它创建了不正确的 json 结构。

请求我的版本的负载

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


------WebKitFormBoundary8VABz8KuNRE8Hepd--

请求正确版本的负载

 ------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--

以及发送数据的正确网址,您可以进入表格进行测试

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

以及 API 参考(创建视频)

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

非常感谢您的帮助。

他们的 API 参考文档(第二个 link)指出:

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=

所以,只需替换:

curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

与:

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

然后它应该可以工作了。通过替换上面的代码,我得到错误 "invalid token" 而不是 "Could not find JSON-RPC."。