繁体   English   中英

API需要编码的API密钥,我该如何使用curl发送它?

[英]API requires a encoded API key, how do i send this with curl?

我正在使用需要在curl调用的基本身份验证中发送api密钥的api。

    $json = "somedata";
    $url = "http.server.com";
    $key = "someKey";

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERPWD, $key);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/2.0");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = curl_exec($ch);
    curl_close($ch);

我的回答是:

    "Server error","data":{"details":"Invalid API key. 

我知道密钥本身是正确的,当发送不使用php的curl请求时,它可以工作。

我假设USERPWD不是正确使用的curlopt,哪个是?

从文档:

Base64编码的字符串,由组合的username:password序列生成。

在我们的例子中,API密钥设置为用户名,密码设置为空字符串。

例如,如果API密钥等于N8KzwcqVUxAI1RoPi5jyFJPkPlkDl9vF,

应该对以下字符串执行Base64编码:N8KzwcqVUxAI1RoPi5jyFJPkPlkDl9vF:

在这种情况下,发送到授权标头的内容是Basic TjhLendjcVZVeEFJMVJvUGk1anlGSlBrUGxrRGw5dkY6。

API文件

USERPWD是正确的,但您可能需要发送

$apiuser:$apikey

看起来像现在,您只是在发送密钥。

我调整了发送标题的方式:

  $headers = array( 
        "POST ".$url." HTTP/1.0", 
        "Content-type: text/xml;charset=\"utf-8\"", 
        "Accept: text/xml", 
        "Cache-Control: no-cache", 
        "Pragma: no-cache", 
        "SOAPAction: \"run\"", 
        "Content-length: ".strlen($xml_data), 
        "Authorization: Basic " . $key 
    ); 

并添加

     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM