【问题标题】:How would I turn this curl request into a jquery request?我如何将此 curl 请求转换为 jquery 请求?
【发布时间】:2016-04-12 13:12:05
【问题描述】:

我正在尝试使用 API,但他们的示例是 curl,而不是我最熟悉的 javascript。这是他们的例子:

$ curl http://visearch.visenze.com/uploadsearch \
   -d im_url=http://example.com/example-0.jpg \
   -d box=0,0,20,20 \
   -u access_key:secret_key

这是我对等效 jquery 请求的尝试,但它不起作用。

$.get("http://visearch.visenze.com/uploadsearch/im_url=http://example.com/example-0.jpg/box=0,0,20,20/u access :secret_key", function(data){...}); 

【问题讨论】:

  • -d 在 POST 中作为数据发送,因此您至少需要一个 $.post
  • “它不工作” 这不是一个问题陈述吗?
  • 您应该确保CORS 没有问题。如果您尝试访问他们域之外的 API这很可能是个问题。我认为如果他们一开始没有指定 JavaScript 作为替代方案,那将是一个问题。
  • 我有点担心你正在尝试使用带有 accesssecret 参数的 jQuery 的 api,这意味着它在一个网站上,这意味着你会泄露这些价值观……我希望这个网站是短暂的,不能从公共互联网上获得!
  • 话虽如此,但不太清楚这个问题的所有反对意见是什么。

标签: javascript php jquery curl


【解决方案1】:

带有-d 选项的cURL 请求将请求作为POST 请求发送(除非您为GET 请求指定G 修饰符),因此您需要使用该格式。您还需要在 beforeSend 方法中设置标题:

$.ajax(
  'http://visearch.visenze.com/uploadsearch',
  type: 'post',
  data: {
    im_url: 'http://example.com/example-0.jpg',
    box: '0,0,20,20'
  },
  beforeSend: function (xhr) {
    xhr.setRequestHeader("Authorization", "Basic ");
  }
);

【讨论】:

    【解决方案2】:

    如果您改用$.ajax()$.get() 是简写形式),您可以使用use the username and password parameters (jQuery 1.7.2+) 进行基本身份验证。如果需要该请求方法,则需要传递一个数据对象并指定 POST。

    $.ajax(
        url: 'http://visearch.visenze.com/uploadsearch',
        data: {
            im_url: 'http://example.com/example-0.jpg',
            box: '0,0,20,20',
        },
        username: 'access_key',
        password: 'secret_key',
        type: 'POST',
        success: function(data) {
            // ... your callback
        }
    );
    

    由于您已在此问题中标记了 PHP,因此我将展示一个示例,说明您可能希望如何在后端包装器中隐藏您的访问密钥等,正如 Visenze API FAQs 建议的那样:

    1。创建一个 PHP 文件

    <?php
    
    $accessKey = 'access_key';
    $secretKey = 'secret_key';
    
    if (isset($_POST['im_url'], $_POST['box'])) {
        // Initialize the cURL request to ViSenze
        $ch = curl_init('http://visearch.visenze.com/uploadsearch');
    
        // We want to RETURN the result not output it
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        // Set up the basic authentication settings
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, "$accessKey:$secretKey");
    
        // Define the post fields
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, [
            'im_url' => $_POST['im_url'],
            'box'    => $_POST['box']
        ]);
    
        // Do the request
        $result = curl_exec();
        curl_close($ch);
    
        // Output the results
        echo $result;
        exit;
    }
    
    echo 'Nothing posted to this script.';
    exit;
    

    2。用 jQuery 调用该文件

    $.post(
        'http://visearch.visenze.com/uploadsearch',
        {
            im_url: 'http://example.com/example-0.jpg',
            box: '0,0,20,20',
        },
        function(data) {
            // ... your callback
        }
    );
    

    这样,您的 API 凭据将存储在您的 PHP 代码中,因此在您查看页面源代码时不可见。

    【讨论】:

    • 谢谢@RobbieAverill。
    猜你喜欢
    • 2018-01-30
    • 2017-10-31
    • 2020-04-28
    • 1970-01-01
    • 2021-07-08
    • 2020-02-26
    • 2019-06-28
    • 2018-02-16
    • 2018-04-16
    相关资源
    最近更新 更多