如何使用PHP和cURL在JFrog Artifactory中创建/更新键/值对?

huangapple go评论81阅读模式
英文:

How to create/update key/value pair in JFrog Artifactory with PHP and cURL?

问题

我正在使用PHP 8.2来读取我们的JFrog Artifactory API,这运行得很好。现在我需要要么创建要么更新一个artifact的属性 - 所以如果它不存在则创建它,如果它存在则更新它。

例如,我可以使用以下代码读取特定Artifactory的所有属性

<?php

// 特定artifact及其“属性”的完整URL
$api = "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0/?properties";

$ch = curl_init($api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "X-JFrog-Art-Api: " . $artifactoryKey,
));

// 执行请求以检索现有属性
$response = curl_exec($ch);
echo "<pre>";
var_dump($response);

?>

这将显示类似于以下内容的结果:

string(123) "{
    "properties" : {
        "ComponentName" : [ "TestComponent" ],
        "ContactEmail" : [ "me@nowhere.com" ],
        "ContactName" : [ "John Doe" ],
        "VersionNumber" : [ "1.0.0" ]
    },
    "uri" : "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0"
}"

现在我想在属性中创建一个新的键/值对。
例如,如果我想要创建一个名为MyKey的新键,其值为False,然后我希望看到以下结果:

string(123) "{
    "properties" : {
        "ComponentName" : [ "TestComponent" ],
        "ContactEmail" : [ "me@nowhere.com" ],
        "ContactName" : [ "John Doe" ],
        "VersionNumber" : [ "1.0.0" ],
        "MyKey" : [ "False" ]
    },
    "uri" : "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0"
}"

我当然尝试了各种解决方案,比如:

// 定义新的键/值对
$newProperty = array(
    'key' => "MyKey",
    'value' => "False"
);

// 准备JSON负载
$payload = json_encode(array("properties" => array($newProperty)));

// 初始化cURL以更新属性
$ch = curl_init($api);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'X-JFrog-Art-Api: ' . $artifactoryKey
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

// 执行请求以更新属性
$response = curl_exec($ch);
echo "<pre>";
var_dump($response);

...以及对此的各种调整,但我无法让它工作。执行上述解决方案将导致以下错误:

string(98) "{
    "errors" : [ {
        "status" : 400,
        "message" : "Properties value cannot be empty."
    } ]
}"

我帐户应该有足够的访问权限来写入/修改(我不是管理员,但被告知如此),但错误消息似乎与权限无关,所以对此的任何帮助将不胜感激 - 我期望这可能是一个简单的修复,因为我没有处理Artifactory的经验 如何使用PHP和cURL在JFrog Artifactory中创建/更新键/值对?

(Note: The code you provided has HTML entities like &lt; and &quot;. These entities are used to represent "<" and double quotes in HTML. If you are using this code in a PHP script, you may want to replace these entities with their actual characters for it to work properly.)

英文:

I am using PHP 8.2 to read our JFrog Artifactory API and this works fine. Now I am in the need to either create or update some of the properties on an artifact - so either create it if it does not exists, and update it if it does exists.

For example I can read all properties for a specific artifactory with this code:

&lt;?PHP

// The full URL for the specific artifact and its &quot;properties&quot;
$api = &quot;https://mysrv/api/storage/TestProduct/TestComponent/1.0.0/?properties&quot;;

$ch = curl_init($api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    &quot;X-JFrog-Art-Api: &quot;. $artifactoryKey,
));

// Execute the request to retrieve existing properties
$response = curl_exec($ch);
echo &quot;&lt;pre&gt;&quot;;
var_dump($response);

?&gt;

This will dump something like this:

string(123) &quot;{
    &quot;properties&quot; : {
        &quot;ComponentName&quot; : [ &quot;TestComponent&quot; ],
        &quot;ContactEmail&quot; : [ &quot;me@nowhere.com&quot; ],
        &quot;ContactName&quot; : [ &quot;John Doe&quot; ],
        &quot;VersionNumber&quot; : [ &quot;1.0.0&quot; ]
    },
    &quot;uri&quot; : &quot;https://mysrv/api/storage/TestProduct/TestComponent/1.0.0&quot;
}&quot;

Now I want to create a new key/value pair inside the properties.
For example if I want to create a new key named MyKey with a value of False then I would like to see this result:

string(123) &quot;{
    &quot;properties&quot; : {
        &quot;ComponentName&quot; : [ &quot;TestComponent&quot; ],
        &quot;ContactEmail&quot; : [ &quot;me@nowhere.com&quot; ],
        &quot;ContactName&quot; : [ &quot;John Doe&quot; ],
        &quot;VersionNumber&quot; : [ &quot;1.0.0&quot; ]
        &quot;MyKey&quot; : [ &quot;False&quot; ]
    },
    &quot;uri&quot; : &quot;https://mysrv/api/storage/TestProduct/TestComponent/1.0.0&quot;
}&quot;

I have of course tried various solutions like:

// Define the new key/value pair
$newProperty = array(
    &#39;key&#39; =&gt; &quot;MyKey&quot;,
    &#39;value&#39; =&gt; &quot;False&quot;
);

// Prepare the JSON payload
$payload = json_encode(array(&quot;properties&quot; =&gt; array($newProperty)));

// Initialize cURL to update the properties
$ch = curl_init($api);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, &#39;PUT&#39;);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    &#39;Content-Type: application/json&#39;,
    &#39;X-JFrog-Art-Api: &#39; . $artifactoryKey
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

// Execute the request to update the properties
$response = curl_exec($ch);
echo &quot;&lt;pre&gt;&quot;;
var_dump($response);

... and various tweaks to this but I cannot get it to work. Doing the above solution will give me this error:

string(98) &quot;{
    &quot;errors&quot; : [ {
        &quot;status&quot; : 400,
        &quot;message&quot; : &quot;Properties value cannot be empty.&quot;
    } ]
}&quot;

My account should have anough access to write/modify (I am not the admin but have been told so) but the error message does not look like it is related to permissions either, so any help on this would be appreciated - I expect this may be a simple fix as I have not much experience with handling Artifactory stuff 如何使用PHP和cURL在JFrog Artifactory中创建/更新键/值对?

答案1

得分: 0

实际上,我自己找到了解决方法,这只是一个愚蠢/简单的错误。在找到正确的帮助页面后,我很容易解决了这个问题 - 我应该在一开始就投入更多时间来查找这个问题 如何使用PHP和cURL在JFrog Artifactory中创建/更新键/值对?

首先让我展示解决方案:

&lt;?PHP

// 特定工件的完整 URL 和要创建/更新的属性
$api = &quot;https://mysrv/api/storage/TestProduct/TestComponent/1.0.0/?properties=MyKey=False&quot;;

// 初始化 cURL 来创建/更新属性
$ch = curl_init($api);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, &quot;PUT&quot;);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    &quot;Content-Type: application/json&quot;,
    &quot;X-JFrog-Art-Api: &quot;. $artifactoryKey
));

// 执行 cURL 请求
$response = curl_exec($ch);

// 输出结果
echo &quot;&lt;pre&gt;&quot;;
var_dump($response);

// 检查请求是否成功
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($httpCode);

// 关闭 cURL 会话
curl_close($ch);

?&gt;

这段代码将导致 HTTP 状态 204 (无内容),这表示成功,并且将创建名为 MyKey 值为 False 的新属性。cURL 的 PUT 命令将适用于创建更新该值。

在这里找到了相关的帮助页面:https://jfrog.com/help/r/jfrog-rest-apis/set-item-properties

英文:

Actually I did figure out this myself and it was a stupid/simple mistake. I easily solved this after finding the right help page - I should probably have invested more time in finding this in the first place 如何使用PHP和cURL在JFrog Artifactory中创建/更新键/值对?

Let me show the solution first:

&lt;?PHP

// Full URL for specific artifact and the property to create/update
$api = &quot;https://mysrv/api/storage/TestProduct/TestComponent/1.0.0/?properties=MyKey=False&quot;;

// Initialize cURL to create/update the properties
$ch = curl_init($api);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, &quot;PUT&quot;);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    &quot;Content-Type: application/json&quot;,
    &quot;X-JFrog-Art-Api: &quot;. $artifactoryKey
));

// Execute the cURL request
$response = curl_exec($ch);

// Dump the result
echo &quot;&lt;pre&gt;&quot;;
var_dump($response);

// Check if the request was successful
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($httpCode);

// Close the cURL session
curl_close($ch);

?&gt;

This code will result in a HTTP Status 204 (No Content) which equals a success and it will create the new propery named MyKey with the value of False. The cURL PUT command will work for both creating it or updating the value.

Found the help for it here, https://jfrog.com/help/r/jfrog-rest-apis/set-item-properties

huangapple
  • 本文由 发表于 2023年6月1日 21:12:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定