【问题标题】:Cloudinary how generate signature in phpCloudinary如何在php中生成签名
【发布时间】:2017-02-05 14:46:58
【问题描述】:

这是我关于通过小部件进行云上传的代码,不幸的是我收到上传错误...我在本地主机上工作,放置此代码的页面是 add.php 页面

<script src="//widget.cloudinary.com/global/all.js" type="text/javascript"></script>
<script type="text/javascript">
  var generateSignature = function(callback, params_to_sign){
    $.ajax({
      url     : "http://localhost/add.php?public_id=sample_image&timestamp=1315060510",
      type    : "GET",
      dataType: "text",
      data    : { data: params_to_sign},
      complete: function() {console.log("complete")},
      success : function(signature, textStatus, xhr) { callback(signature); },
      error   : function(xhr, status, error) { console.log(xhr, status, error); }
    });
  }
</script>


    <script type="text/javascript">  
      $('#upload_widget_opener').cloudinary_upload_widget(
        { cloud_name: 'dammiunparere', api_key: '189XXXX42445355',
          upload_signature: generateSignature},

        function(error, result) { console.log(error, result) });
    </script>

【问题讨论】:

  • 这是您看到的“无效签名”错误吗?我看到除了params_to_sign 之外,您还在url 中传递了一些查询字符串参数。请注意,Cloudinary 希望您签署params_to_sign 中的内容。

标签: javascript php jquery ajax cloudinary


【解决方案1】:

最近尝试解决这个问题。这是一个简单的解决方案:

它需要在您的服务器上有脚本来生成签名,在这种情况下它是名为“add.php”的文件。它应该包含以下内容:

<?php
    define('CINCDIR', './inc/cloudinary/'); // path to cloudinary lib folder

    require_once(CINCDIR . 'Cloudinary.php');
    require_once(CINCDIR . 'Api.php');

    if(isset($_GET['data']))
    {
        echo \Cloudinary::api_sign_request($_GET['data'], '[PUT CLOUDINARY API SECRET HERE]');
    }
?>

[PUT CLOUDINARY API SECRET HERE] 替换为您的云 API 密码。

此脚本需要 cloudinary 的 PHP 集成库。您应该可以从以下链接下载它: https://github.com/cloudinary/cloudinary_php/tarball/master。 将 src 文件夹中的所有文件复制到您的 PHP 项目中,并在您的代码中包含 Cloudinary 的 PHP 类:

require_once(CINCDIR . 'Cloudinary.php');
require_once(CINCDIR . 'Api.php');

此外,关于上面示例中的 JavaScript,您不需要在此处传递预定义的参数:

url     : "http://localhost/add.php?public_id=sample_image&timestamp=1315060510"

只是脚本的 URL,在本例中为“http://localhost/add.php”:

url     : "http://localhost/add.php"

在调用 generateSignature 函数时自动添加参数。

【讨论】:

    猜你喜欢
    • 2019-11-03
    • 2017-02-25
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多