【问题标题】:How to construct the CURL in JSON-RPC structure in PHP?如何在 PHP 中构造 JSON-RPC 结构中的 CURL?
【发布时间】:2015-05-26 07:24:36
【问题描述】:

最近我使用的 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

非常感谢您的帮助。

【问题讨论】:

    标签: php json post curl json-rpc


    【解决方案1】:

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

    对于写入请求,提交给媒体写入 API 的数据应该是 JSON 格式,编码为名为“json”的表单参数。 JSON 文档应该包括一个“方法”字段和一个“参数”字段,并且 您发布的示例应显示 POST 请求的整个主体: json=

    所以,只需替换:

    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    

    与:

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

    然后它应该可以工作了。我收到错误“无效令牌”而不是“找不到 JSON-RPC”。通过替换上面的代码。

    【讨论】:

      猜你喜欢
      • 2013-03-29
      • 2011-08-26
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      相关资源
      最近更新 更多