>我在一个名为 $json 的变量中有 JSON 编码的数据,它看起来像——
string(1243) "{"screenShareCode":"882919360", "appletHtml":", "presenterParams":"aUsEN5gjxX/3NMrlIEGpk0=", "viewerUrl":"http://api.screenleap.com/v2/viewer/882919360?accountid=mynet", "origin":"API"}" }
我需要将此 JSON 数据传递到 JavaScript 函数中,请参阅下文
script type="text/javascript" src="http://api.screenleap.com/js/screenleap.js">/script> 脚本类型="文本/JavaScript"> window.onload = function() { var screenShareData = '?php echo $json;?>'; screenleap.startSharing('DEFAULT', screenShareData); }; /脚本>
当我尝试运行此代码时,它给我一个错误,说"缺少强制性屏幕共享数据"。
如何解决此错误?
i am following "https://www.screenleap.com/api/presenter"
看起来
$json
是一个字符串,你需要传入一个json对象。 请尝试以下操作:
window.onload = function() {
var screenShareData = '?php echo $json;?>';
screenleap.startSharing('DEFAULT', JSON.parse(screenShareData));
};
这是您根据文档实现它的方式
https://www.screenleap.com/api/presenter
<?php
// Config
$authtoken = '';
$accountid = '';
// 1. Make CURL Request
$url = 'https://api.screenleap.com/v2/screen-shares';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('authtoken:<authtoken>'));
curl_setopt($ch, CURLOPT_POSTFIELDS, 'accountid=<accountid>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data, true);
?>
<!-- 2. Launch the Presenter App -->
<script type="text/javascript" src="http://api.screenleap.com/js/screenleap.js"></script>
<script type="text/javascript">
window.onload = function() {
screenleap.startSharing('DEFAULT', JSON.parse('<?php echo $json; ?>'));
};
</script>
如果这不起作用,您必须将其报告给屏幕跳跃。
如果要访问值,则只需实际解析 JSON。否则,只需将响应数据直接传递到 startSharing 函数中,如下所示:
<?php
$url = 'https://api.screenleap.com/v2/screen-shares';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('authtoken:<your authtoken>'));
curl_setopt($ch, CURLOPT_POSTFIELDS, 'accountid=<your accountid>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data, true);
?>
<script type="text/javascript" src="http://api.screenleap.com/js/screenleap.js"></script>
<script type="text/javascript">
window.onload = function() {
screenleap.startSharing('DEFAULT', <?php echo $data; ?>);
};
</script>
如果您只插入自己的帐户ID和身份验证令牌(不带前导空格),那应该适合您。