在php中将API密钥添加到Curl的位置

qxsslcnc  于 2022-12-25  发布在  PHP
关注(0)|答案(4)|浏览(162)

我试图利用铁砧API的PDF。
这是他们的样品要求。

curl \
  -X POST \
  -u YOUR_API_KEY: \
  -H 'Content-Type: application/json' \
  -d '{ "data": { "someKey": "some data" } }' \
  https://app.useanvil.com/api/v1/fill/{pdfTemplateID}.pdf > test.pdf

我的问题是在哪里添加API KEY。我尝试将其添加到标头,但它抛出错误{"name":"AuthorizationError","message":"Not logged in."}
下面是目前为止的代码

$url2="https://app.useanvil.com/api/v1/fill/first.pdf";
$ch2 = curl_init();
curl_setopt($ch2,CURLOPT_URL, $url2);

$apiKey ='my api key goes here';

$post_data ='
{

  "data": {
    "someName": "Bobby",
    "someDate": "2018-10-31",
    "anAddress": {
      "street1": "123 Main St",
      "city": "San Francisco",
      "state": "CA",
      "zip": "94106"
    }
  }
}';

curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
//'Content-Type:application/json'
'Authorization: ' . $apiKey
));  

curl_setopt($ch2,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch2,CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch2,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch2,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch2,CURLOPT_RETURNTRANSFER, true);
echo $response2 = curl_exec($ch2);

curl_close($ch2);
xqk2d5yq

xqk2d5yq1#

您提供的curl命令具有选项-u,它需要来自curl man的username:password数据

  • u/--user user:password指定用于服务器身份验证的用户和密码。如果多次使用此选项,将使用最后一个选项。
    在PHP中,你必须发送如下的头:
CURLOPT_HTTPHEADER => [
        'Authorization: Basic ' . $apiKey . ':'
    ],

或与

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":");

related thread
编辑:从您的链接在评论,他们期待您的请求的原始数据,你可以通过发送它作为放置请求完成:curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'PUT');
或带有文本/纯标题

izkcnapc

izkcnapc2#

**
        
         - ***UPDATED***
        
        **
    are you encoding the API key as "base64" ?? 
    
    
        $YOUR_API_KEY =  base64_encode("YOUR_API_KEY");

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://app.useanvil.com/api/v1/fill/XnuTZKVZg1Mljsu999od.pdf');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"title\": \"Hello\", \"data\": [ { \"label\": \"Hello World\", \"content\": \"I like turtles\" } ] }");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $YOUR_API_KEY . ':' . '');

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
bq3bfh9z

bq3bfh9z3#

在curl请求中添加API密钥的最简单方法如下

// Collection object
    $ch = curl_init($url);
    
 $headers = array(
         "APIKEY: PUT_HERE_API_KEY",
         "Content-type: text/xml;charset=\"utf-8\"",
         "Accept: text/xml"
     );
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_TIMEOUT, 60);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlreq);
    $result = curl_exec($ch); // execute
    $result;
   //show response
    curl_close($ch);
7d7tgy0s

7d7tgy0s4#

curl -X GET -k -H 'Content-Type: application/json' -H 'X-ApiKey : YOUR_APIKEYHERE' -i 'YOUR API RNDPOINT URL HERE'

X-ApiKey是API密钥的名称。

相关问题