英文:
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的经验
(Note: The code you provided has HTML entities like <
and "
. 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:
<?PHP
// The full URL for the specific artifact and its "properties"
$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,
));
// Execute the request to retrieve existing properties
$response = curl_exec($ch);
echo "<pre>";
var_dump($response);
?>
This will dump something like this:
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"
}"
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) "{
"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"
}"
I have of course tried various solutions like:
// Define the new key/value pair
$newProperty = array(
'key' => "MyKey",
'value' => "False"
);
// Prepare the JSON payload
$payload = json_encode(array("properties" => array($newProperty)));
// Initialize cURL to update the properties
$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);
// Execute the request to update the properties
$response = curl_exec($ch);
echo "<pre>";
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) "{
"errors" : [ {
"status" : 400,
"message" : "Properties value cannot be empty."
} ]
}"
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
答案1
得分: 0
实际上,我自己找到了解决方法,这只是一个愚蠢/简单的错误。在找到正确的帮助页面后,我很容易解决了这个问题 - 我应该在一开始就投入更多时间来查找这个问题
首先让我展示解决方案:
<?PHP
// 特定工件的完整 URL 和要创建/更新的属性
$api = "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0/?properties=MyKey=False";
// 初始化 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 请求
$response = curl_exec($ch);
// 输出结果
echo "<pre>";
var_dump($response);
// 检查请求是否成功
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($httpCode);
// 关闭 cURL 会话
curl_close($ch);
?>
这段代码将导致 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
Let me show the solution first:
<?PHP
// Full URL for specific artifact and the property to create/update
$api = "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0/?properties=MyKey=False";
// Initialize cURL to create/update the properties
$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
));
// Execute the cURL request
$response = curl_exec($ch);
// Dump the result
echo "<pre>";
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);
?>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论